I suspect I'm getting the wrong results from my code when calculating position sizing.What I want to do is, calculate the number of lots I need to open my order of. They are dependent on the stop loss placement, and the money I would like to lose if that stop loss value is achieved.My main suspect is that I'm missing something regarding the leverage, but I'm not sure.This is my current formula (C++ code):
It seems to be working fine for forex pairs, but when I try with Silver with a risk of 200$, I get an order size of 3.58 lots, which starts in a position of -800$. Of course, if the SL was hit, the loss would be even bigger.
These are the values I get when debugging:
lots = 3.58stoplossDiff = 0.011520tvts = 4780.93 - (tick value: 4.788093, tick size: 0.001)
Any help would be appreciated.Best regards.
You made an error :
stoplossDiff * tvts is 4788.09 * 0.011520 = 55.1578
so 3.58 is 197.468 USD, which is probably correct with the rounding.
Nothing to do with leverage.
All the calculation seems correct (except the typo ? reported by WHRoeder).
When you open a position you have to take into account the spread, I have currently for XAGUSD (silver) a spread of 0.035 (Alpari). That's 3 times your stoploss.
Your stoploss is too tight (incorrectly calculated ?)
lots_calc SILVER,M1: Pair SILVER | Ask 16.106000 | risk 193.243580 | dpl 4782.309282 | sl_diff 0.015000 | lots 2.693867
you open a position you have to take into account the spread, I have
currently for XAGUSD (silver) a spread of 0.035 (Alpari). That's 3 times
I think I'm discovering some (somehow unrelated, somehow related) flaws in my EA due to this.Thank you for your help.Best regards.
Guillermo: SL must be set to a value lower than the Bid value at the time of opening.
No. Should be :
And add to this the remark of WHRoeder about Stop Levels.