# Mql5 function to calculate position size

#### Poker_player

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

//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=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. It loses about 80 usd. 2 % from 10k is 200.

Backtest shows it has lost about 75 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

Can it be that mql gives wrong tick value? On this table it shows 1 pip value is 1000 yens

1000 yens is 9.66 dollars

so if I use pip value 9.66 usd, now formula matches position size calculator .

10000*0.02/(4*9.66) = 5.18 losing about 500 with 5.18 lots, which is 2 % from 10k.

So for my function it means I need to calculate or find another function for pip/tick value.

#### Enivid

Staff member
@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);`

• Poker_player

#### Poker_player

@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 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.

Replies
12
Views
7K
Replies
16
Views
6K
Replies
2
Views
2K
Replies
11
Views
5K
Replies
4
Views
3K