Position sizing with OrderCalcProfit when tick value is not trustworthy

The approach that has been reliable for me is the one Fernando Carreiro described years ago: ask OrderCalcProfit() for a known volume, then scale to the money you are willing to lose. I use the symbol's maximum volume as that known size, because 1.0 lot is not always a valid volume.


OrderCalcProfit already returns the result in the account currency, including the contract size and the profit-currency conversion, so the same function works for FX, metals and indices.


Two pitfalls that are easy to miss:


1. Round down to SYMBOL_VOLUME_STEP, not to the nearest step. Nearest can push the real stop loss above the risk budget. If the floored size is below SYMBOL_VOLUME_MIN, I skip the trade rather than silently taking a larger risk than the input says. If you want "use min lot anyway, but only up to X percent", that should be a separate, visible input.


2. A failed OrderCalcProfit or a zero tick value is not a reason to fall back to a hard-coded pip value. It usually means the symbol is not ready yet (no quote, not selected). Retry later, or skip.


Here is a compact version I compiled and use as a script. profit from OrderCalcProfit is negative when the close price is a loss, which is what we want for a stop.


double VolumeDigits(const double step)
  {
   if(step <= 0.0)
      return 0;
   return MathMax(0, MathCeil(-MathLog10(step) - 1e-12));
  }

double FloorToStep(const double volume, const double step)
  {
   return MathFloor(volume / step + 1e-8) * step;
  }

bool LotsForRisk(const string symbol,
                 const ENUM_ORDER_TYPE type,
                 const double open_price,
                 const double sl_price,
                 const double risk_money,
                 double &lots)
  {
   lots = 0.0;
   if(type != ORDER_TYPE_BUY && type != ORDER_TYPE_SELL)
      return false;

   double vmin = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);
   double vmax = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX);
   double step = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);
   if(vmin <= 0.0 || vmax < vmin || step <= 0.0 || risk_money <= 0.0)
      return false;

   double profit = 0.0;
   ResetLastError();
   if(!OrderCalcProfit(type, symbol, vmax, open_price, sl_price, profit))
     {
      PrintFormat("OrderCalcProfit failed, error %d", GetLastError());
      return false;
     }
   if(profit >= 0.0)
     {
      Print("SL is not on the loss side of the open price.");
      return false;
     }

   lots = risk_money * vmax / (-profit);
   lots = FloorToStep(lots, step);
   if(lots < vmin)
     {
      lots = 0.0;
      return false;
     }
   if(lots > vmax)
      lots = vmax;
   lots = NormalizeDouble(lots, (int)VolumeDigits(step));
   return true;
  }



I would be interested in how other people handle the "min lot already exceeds the budget" case on gold with a small account, and whether anyone still prefers TICK_VALUE for anything other than a quick estimate.