In short, I'm writing an Expert Advisor to execute trades on the MT5 platform.
The logic is fine, but I'm struggling with the position size (lot size). Here is the idea:
I open a market buy order at the Current Close Price (Close[0])
Stop loss price = Low[1] - 5 pips (for example)
I want to risk only 5% of my account balance based on that stop loss
Here is my code (simplified)
int risk = 5;
void OnTick()
{
if x = ...
{
double SL = Low[1] - 0.00050; // SL price
double Lot = (BALANCE * risk) / (MathAbs(close[0] - SL) * TickValue * ContractSize);
// Close[0] - SL = stoploss distance
SendOrder(ORDER_TYPE_BUY,Lot,SL,NULL,magicNumber);
}
}
The problem is, when I force it to open a market order, the system will require a "specific volume number", while I can only get that after specifying the open price of the buy order => order cannot be sent without a volume number. So it's basically looping with dependence error
Can anyone help me to get a predefined volume, before it is used for order execution? Much appreciated!