Lot size from risk % on gold, indices and synthetics: OrderCalcProfit or TICK_VALUE?

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:

  1. SYMBOL_TRADE_TICK_VALUE can return 0 right after the terminal starts, or for a symbol that is not yet in Market Watch.
  2. On some CFDs the tick value depends on the profit currency and the exchange rate, so a value cached in OnInit() is stale a few hours later.
  3. SYMBOL_TRADE_TICK_SIZE is not always equal to _Point (for example indices with a tick size of 0.25 or 0.1), so mixing points and ticks can give a 10x error.

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:

  • Do you use OrderCalcProfit() for this, or TICK_VALUE with your own currency conversion? Have you seen brokers where OrderCalcProfit() returns wrong values?
  • When the minimum lot already exceeds the risk, do you skip the trade, or warn the user and allow it?
  • Any special cases you have seen on synthetic indices (Deriv etc.) or on accounts in a currency other than USD?

Thanks in advance. I am happy to share my test results across different brokers once I have more data.

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 ?