Need Help with mql4 Lot size calculation function please

JD_

Trader
Feb 20, 2023
8
0
12
35
Hello,

I'm using this function in my EA to calculate the lot size position according to account balance percentage but the problem is that the calculation is not accurate specially in JPY pairs,

For example if the loss must be 50 usd according to account balance percentage risk most of the time it's more than the desired risk percentage sometimes ( 60 or 70 usd)

Please if someone can help me it will be appreciated i've been trying for so long without success.

Best Regards,

MQL4:
double GetLotSize(string symbol, double risk, double SL)
{
double tickvalue = MarketInfo(Symbol(), MODE_TICKVALUE);
double lotstep = MarketInfo (Symbol(), MODE_LOTSTEP);
double minlot = MarketInfo(Symbol(), MODE_MINLOT);
double maxlot = MarketInfo(Symbol(), MODE_MAXLOT);
double lotsize;
 
 
 if (StringFind(symbol, "jpy") != -1)
 {
     lotsize = ((AccountBalance() * risk) / ( SL * tickvalue ))*Point;   
 }
 else
 {
     lotsize = ((AccountBalance() * risk) / ( SL * tickvalue ))/1000;
 }
 
 // Ensure that the calculated lot size is within the minimum and maximum allowed by the broker
 lotsize = MathMin(lotsize, maxlot);
 lotsize = MathMax(lotsize, minlot);
 
    // Round the lot size to the nearest step size allowed by the broker
    lotsize = MathRound(lotsize/lotstep)*lotstep;
 
 
 double pipval = (AccountBalance() * risk) /  SL /100;
 
 Print( "   tickvalue=", tickvalue, "     lotstep =", lotstep,  "     Lotsize=", lotsize,     "        Pipval=",pipval );
 
 return lotsize;
}
 

JD_

Trader
Feb 20, 2023
8
0
12
35
This might be due to the currency pair being set as a CFD at your broker. Check the symbol specification in your MT4. What does it say under the 'Profit calculation mode'?
Thank you for your reply.

It says " Forex".
 

JD_

Trader
Feb 20, 2023
8
0
12
35
If you try to print the tickvalue variable on those XXX/JPY pairs, is the output correct?
I'm afraid to give you a wrong answer just in case if i'm missing something so i took a screenshot to make it clear for you.

Thanks a lot for the help.

TICKVAL.png
 

JD_

Trader
Feb 20, 2023
8
0
12
35
Thanks for the reply.

I wrote an EA to print the tick value in the comment every tick to check the result and it is 0.733 for JPY Pairs and then i checked 5 other websites to compare the pip value, All of them said 0.733 and i tried to calculate it manually by this formula (0.001/USDJPY Price) the result is 0.733.

Please can you enlighten me how the current USD/JPY is 0.9212 as you posted above.
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,210
1,504
144
Odesa
www.earnforex.com
Thanks for the reply.

I wrote an EA to print the tick value in the comment every tick to check the result and it is 0.733 for JPY Pairs and then i checked 5 other websites to compare the pip value, All of them said 0.733 and i tried to calculate it manually by this formula (0.001/USDJPY Price) the result is 0.733.

Please can you enlighten me how the current USD/JPY is 0.9212 as you posted above.
In your examples, USD/JPY rate is ~108, not the current one ~136. 0.733 is correct for today's trades, but not for the trades of 2019 that you are trying to calculate. Tickvalue won't output correct historical values.
 

JD_

Trader
Feb 20, 2023
8
0
12
35
In your examples, USD/JPY rate is ~108, not the current one ~136. 0.733 is correct for today's trades, but not for the trades of 2019 that you are trying to calculate. Tickvalue won't output correct historical values.
I 'm sorry for the misunderstanding i thought you meant today's value is 0.9212.

In the beginning i thought the problem was from the MT4 tester so i tried it in a demo account for forward trading testing.

But even when i put the EA in demo account for forward trading sometimes the calculation is not correct ( even with non JPY pairs).
Any suggestions?
 

Enivid

Administrator
Staff member
Nov 30, 2008
19,210
1,504
144
Odesa
www.earnforex.com
In the beginning i thought the problem was from the MT4 tester so i tried it in a demo account for forward trading testing.

But even when i put the EA in demo account for forward trading sometimes the calculation is not correct ( even with non JPY pairs).
Any suggestions?
I would have to see the specific examples to suggest anything. That seems to be a completely different case.

In this case, the problem was in trying to get MODE_TICKVALUE in the Strategy Tester, which I didn't realize at first.
 

JD_

Trader
Feb 20, 2023
8
0
12
35
This is an example in EURNZD i noticed a difference in pips for the SL i don't know if it is the case or the spread this is an image and the SL CODE in case you want to check it.

Thank you very much for trying to help me.

MQL4:
double sl_pips=NormalizeDouble(((iATR(NULL,PERIOD_CURRENT,ATRPeriod,1)/Point/10))*SL_ATRMultiplier,1);

EURNZDLOSS.png
 

JD_

Trader
Feb 20, 2023
8
0
12
35
Not sure what you are trying to ask here...
The above example is in demo account not a tester the loss must be 220$ but the result is 302.46 and i noticed that the the stoploss was 13.9 pips while in the journal the stoploss must be 10.3 pips ( i circled them in a red circle)

The tick value is 0.62
lot size is 3.5
pip value is 6.2*3.5= 21.7 USD per pips

if the stoploss was 10.3 as it's printed in the journal the result would be 21.7 *10.3 = 223.5$

but the actual stoploss was 13.9 as you can see in the red circle in the chart so 13.9*21.7=301.63

so the lot size calculation was right but there was 3.6 more pips ( 13.9 - 10.3 = 3.6 ) that resulted in higher loss

do you think the problem is something else other than the lot size calculation function?
 

JD_

Trader
Feb 20, 2023
8
0
12
35
Thank you for your help i will check many other trades to find the problem but now i know that i was looking at the wrong place.

Best Regards,