Mql5 function to calculate position size

Poker_player

Active Trader
Jul 26, 2017
23
3
29
22
Have tried searching and implementing but cannot find correct example. Its weird because it should be needed for lot of people and should exist somewhere.



Can you give a link or code for that?



So far I have done by examples this

MQL5:
 double calculateLotSizeForPips(double slPips, double maxRiskPerTradePercent){          //Calculate the size of the position size
      double LotSize=0;
      //We get the value of a tick
       //double nTickValue=MarketInfo(Symbol(),MODE_TICKVALUE); // not used
      double nTickValue = SYMBOL_TRADE_TICK_VALUE;
 
 
 
      //If the digits are 3 or 5 we normalize multiplying by 10
      /*if(_Digits==3 || _Digits==5){
         nTickValue=nTickValue*10;
      }*/
 
      float balance = AccountInfoDouble(ACCOUNT_BALANCE);
 
      //We apply the formula to calculate the position size and assign the value to the variable
      LotSize=(balance*maxRiskPerTradePercent/100)/(slPips*nTickValue);
      //LotSize=MathRound(LotSize/MarketInfo(Symbol(),MODE_LOTSTEP))*MarketInfo(Symbol(),MODE_LOTSTEP); // not used
 
      double volumeStep = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
      LotSize=MathRound(LotSize/volumeStep)*volumeStep;
      return LotSize;
   }

But it works bad. Testing on USDJPY pair, usd account.

I do not know why in the example was when digits are 3 or 5 - multiply by ten , but it make my position 10 times smaller.

So for 10k account, when I put to formula it is:

Risk 2 percent, pips 4, tick value 26

10000*0.02/(4*26) = 1.92

Code with volume step is not changing the result.

But it is wrong.
1608976801454.png
It loses about 80 usd. 2 % from 10k is 200.

Backtest shows it has lost about 75
1608976818983.png

its more or less what the function calculated. But it is not 200.

It is weird I cannot find a thing which should be used by tons of developers :)
 

Poker_player

Active Trader
Jul 26, 2017
23
3
29
22

Enivid

Administrator
Staff member
Nov 30, 2008
16,041
817
144
Odessa
www.earnforex.com
@Poker_player Why don't you take a look at the CalculateRiskAndPositionSize() function in Position Size Calculator's source code? You are omitting SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE) from your formula. Also, the biggest problem is this line:
MQL5:
double nTickValue = SYMBOL_TRADE_TICK_VALUE;
It should be:
MQL5:
double nTickValue = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE_LOSS);
 
  • Like
Reactions: Poker_player

Poker_player

Active Trader
Jul 26, 2017
23
3
29
22
@Poker_player Why don't you take a look at the CalculateRiskAndPositionSize() function in Position Size Calculator's source code? You are omitting SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_SIZE) from your formula. Also, the biggest problem is this line:
MQL5:
double nTickValue = SYMBOL_TRADE_TICK_VALUE;
It should be:
MQL5:
double nTickValue = SymbolInfoDouble(Symbol(), SYMBOL_TRADE_TICK_VALUE_LOSS);
I took a look but saw thousands of lines and thougth it will take ages to find where it is what I need :D so currently I just made it work with USDJPY, maybe other xxxJPY pairs will work and I am testing only this pair now and focus on my algo.
thanks, later will need to try.