Lavoro terminato
![](https://c.mql5.com/i/avatar/avatar_na2.png)
![](https://c.mql5.com/i/avatar/avatar_na2.png)
Specifiche
Hi All,
I would like to add to an existing and working MQL5 EA the ability to calculate the lot size based on a risk percentage. Or in other words how many percent of the account I'm willing to lose for each deal.
Here are some key elements of my EA setup:
The base currency is EUR
Trading pair is EURJPY
Leverage is 1/100
SL is fixed at 50 pips
Target risk percentage per deal should be a variable set by default on 5%
The below formula seem to be the one needed to achieve my lot size calculation but I'm not sure.
Lot Size = Amount Risked / Number of Pips x Pip Value
I tried the created the code myself and here is where I stopped:
Basically I need help to compete the "//Calculating Lot Size based on SL and Risk Percentage" part of the code.
Let me know if you have any questions.
Different Variables for the lot size calculation:
input int StopLoss=50; // Stop Loss input double RiskPercentage=5; // Lots to Trade int STP, TKP; double LotSize; double Pips; //--- Let us handle currency pairs with 5 or 3 digit prices instead of 4 STP = StopLoss; TKP = TakeProfit; if(_Digits==5 || _Digits==3) { STP = STP*10; TKP = TKP*10; } return(0); }
My Buying Script
if(Buy_Condition_1 && Buy_Condition_2) { // any opened position? if((Buy_opened)||(Sell_opened)) { //Alert("We already have an open Position. Oportuinity passed."); return; // Don't open a new Buy Position } // Calculating Lot size based on SL and Risk Percentage MqlTick latest_price; SymbolInfoTick(_Symbol,latest_price); Pips = (latest_price.bid - STP)/_Point; double AccountBalance = AccountInfoDouble(ACCOUNT_BALANCE); double TickValue = SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE); LotSize = (AccountBalance * (RiskPercentage/100))/(Pips * TickValue); Print("Pips ", Pips, " Acc Bal: ", AccountBalance," Tick Val ", TickValue," Lot Size ", LotSize); //Executing the Trade ZeroMemory(mrequest); mrequest.action = TRADE_ACTION_DEAL; // immediate order execution mrequest.price = NormalizeDouble(latest_price.ask,_Digits); // latest ask price mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit mrequest.symbol = _Symbol; // currency pair mrequest.volume = LotSize; // number of lots to trade mrequest.magic = EA_Magic; // Order Magic Number mrequest.type = ORDER_TYPE_BUY; // Buy Order mrequest.type_filling = ORDER_FILLING_FOK; // Order execution type mrequest.deviation=100; // Deviation from current price //--- send order OrderSend(mrequest,mresult); // get the result code if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed { Alert("A Buy order has been successfully placed with Ticket#:",mresult.order,"!!"); SendNotification("BBWP added a new Buy order!"); } else { Alert("The Buy order request could not be completed -error:",GetLastError()); ResetLastError(); return; } }