Lot size from risk % on gold, indices and synthetics: OrderCalcProfit or TICK_VALUE?
Such things have been discussed a lot of times on this forum, with a lot of inputs and ideas. Did you make a search before posting ?
You agree to website policy and terms of use
Hello everyone,
While building a risk / lot size calculator I ran into a problem that I think affects a lot of EAs. The classic formula based on SYMBOL_TRADE_TICK_VALUE works fine on EURUSD, but on some brokers it gives wrong lot sizes on XAUUSD, index CFDs and synthetic indices.
The causes I found so far:
What I am testing now is to let the terminal calculate the loss for 1 lot directly with OrderCalcProfit():
double LossForOneLot(const string sym, const ENUM_ORDER_TYPE type, const double entry, const double sl) { double profit = 0.0; if(!OrderCalcProfit(type, sym, 1.0, entry, sl, profit)) return 0.0; return MathAbs(profit); } double LotForRisk(const string sym, const ENUM_ORDER_TYPE type, const double entry, const double sl, const double riskMoney) { double lossPerLot = LossForOneLot(sym, type, entry, sl); if(lossPerLot <= 0.0) return 0.0; double step = SymbolInfoDouble(sym, SYMBOL_VOLUME_STEP); double minV = SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN); double maxV = SymbolInfoDouble(sym, SYMBOL_VOLUME_MAX); // round DOWN to the volume step so the real risk never exceeds the target double lots = MathFloor(riskMoney / lossPerLot / step + 1e-8) * step; if(lots < minV) return 0.0; // even the minimum lot is too risky -> skip the trade return MathMin(lots, maxV); }Two choices I made on purpose: I round down to the volume step instead of using NormalizeDouble(), so the actual risk is never above the user's setting, and I return 0 instead of forcing the minimum lot when even the minimum lot would risk more than allowed.
My questions for other developers:
Thanks in advance. I am happy to share my test results across different brokers once I have more data.