the tick value is already per pip, so you calculate it like this:
double calcLots(double slDist, double riskPercentage){ double risk = AccountInfoDouble(ACCOUNT_MARGIN_FREE) * riskPercentage / 100; double tickvalue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); double lotstep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP); if(slDist > 0 && tickvalue > 0 && lotstep > 0){ double moneyPerLot = slDist * tickvalue; double lots = risk / moneyPerLot; // ensure the lot size is formatted correctly with the lot step lots = MathFloor(lots / lotstep) * lotstep; lots = MathMin(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX)); lots = MathMax(lots, SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN)); return lots; }else{ return 0; } }
and don't base the lot on Account Balance, it's more proper to base it on the free margin available
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hi
I have a function that calculates lots for opening positions. But it seems to be buying to many lots. I can't seem to find the problem with my logic.
Any help would be appreciated.