Risk-based lot size you can trust in MQL5: three measurements and a cross-check

Risk-based lot size you can trust in MQL5: three measurements and a cross-check

30 November 2026, 14:00
Cristian Camilo Gomez Diaz
0
12

Introduction

I write risk tools for MetaTrader 5: a prop-firm guard, a trade manager, a drawing toolkit that shows the lot size next to a long/short box. All of them end in the same line of code. Risk money divided by the money one lot loses at the stop. Everybody writes that line, and most of the time it works. The trouble is that when it does not work, nothing tells you. The order goes out with 0.04 lots instead of 1.00, or with a few percent more risk than the panel promised, and you find out when the stop is hit, if at all.

I paid for this lesson myself. I had two prop-firm challenges running at the same time, at two different firms, and traded the S&P 500 on both. On the first one I was used to a fairly large volume, because there one point of the index was worth little per lot. On the second one I entered the same volume out of habit. Same index, similar symbol name, but a much bigger contract behind it: every point was worth far more, the losses came several times faster than I expected, and I lost the account. The volume was a number I knew by heart. The loss per lot on that account was not. Since then, nothing I build sends an order without computing the loss per lot from the specification of the account it is running on.

So I stopped trusting a single number. MQL5 gives three different ways to get the loss per lot, and they use different data inside the terminal. I wrote a script that computes all three for every symbol in the Market Watch and ran it on a USD demo account with 845 symbols. On 837 of them the methods agree to the cent. On the other eight they do not, for two unrelated reasons, and both are worth knowing before you size a trade on those symbols. Below are the methods, what the account showed, and the include file I now use. The code and the test script are attached.

1. The formula and the properties behind it

For a stop at a price distance D from the entry:

loss per lot = D / SYMBOL_TRADE_TICK_SIZE * SYMBOL_TRADE_TICK_VALUE
lots         = risk money / loss per lot   (then rounded to the volume step)

The properties have similar names and two of them get mixed up all the time:

Property What it is Where it goes wrong
SYMBOL_POINT The last price digit (0.00001 on EURUSD) Used instead of the tick size
SYMBOL_TRADE_TICK_SIZE The smallest step the price can actually make Not equal to the point on some futures-style contracts
SYMBOL_TRADE_TICK_VALUE Money value of one tick for one lot, account currency Recalculated from exchange rates; 0 before the first quote
SYMBOL_TRADE_TICK_VALUE_LOSS / _PROFIT Tick value for a losing / winning position They differ when a conversion rate with a spread is involved
SYMBOL_TRADE_CONTRACT_SIZE Units per lot (100 000 on Forex, 100 on gold) Gives money only when no currency conversion is needed
SYMBOL_TRADE_CALC_MODE How profit and margin are computed Futures modes are not "price times contract"
SYMBOL_VOLUME_MIN / _STEP / _MAX The volume grid Rounding up quietly adds risk

There is nothing wrong with the formula. What goes wrong is which property you read, when you read it, and whether you check it against anything.

2. Three ways to measure the same thing

OrderCalcProfit(). You give it an order type, a volume, an open price and a close price, and it returns the profit in the account currency, using the symbol's calculation mode and whatever conversion rates the terminal has. With the close price at the stop you get the loss. It is the most complete of the three. It is also a trade function, which means you cannot call it from a custom indicator. I ran into this while adding a lot-size label to a drawing indicator.

Ticks times the loss tick value. Count the ticks between entry and stop and multiply by SYMBOL_TRADE_TICK_VALUE_LOSS . Works anywhere, including indicators. It fails when the tick value is not ready yet (right after the terminal starts, or on a symbol that was not in the Market Watch) and when the symbol specification itself reports a wrong value.

Distance times the contract size. Only for linear calculation modes (Forex, CFD, CFD index, CFD leverage, exchange stocks), and only when the profit currency is the account currency: EURUSD, gold or a US index on a USD account, but not GDAXI or USDJPY on the same account. When it applies, no exchange rate is involved at all, which makes it a good referee for the other two.

The function below computes whatever is available, uses OrderCalcProfit if it can, then the ticks, then the contract size, and raises a flag when two available results are too far apart. It also keeps the "point instead of tick size" result, only so the report can show how far off that mistake would be.

bool RlLossPerLot(const RlSpec &s, const bool isBuy, const double entry, const double stop,
                  const bool allowCalc, RlLoss &r)
  {
   r.byCalc = r.byTicks = r.byContract = r.byPoint = r.perLot = 0.0;
   r.method = RL_NONE;
   r.disagree = false;
   r.note = "";
   double dist = MathAbs(entry - stop);
   if(dist <= 0.0)
     {
      r.note = "entry equals stop";
      return false;
     }
   if(isBuy && stop >= entry)
     {
      r.note = "stop above entry for a buy";
      return false;
     }
   if(!isBuy && stop <= entry)
     {
      r.note = "stop below entry for a sell";
      return false;
     }

//--- 1. the terminal's own profit calculation (handles calculation mode and currency conversion)
   if(allowCalc)
     {
      double p = 0.0;
      ResetLastError();
      if(OrderCalcProfit(isBuy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL, s.symbol, 1.0, entry, stop, p) && p < 0.0)
         r.byCalc = -p;
     }

//--- 2. ticks x tick value. The LOSS tick value is the one to use for a stop
   double tv = (s.tickValueLoss > 0.0) ? s.tickValueLoss : s.tickValue;
   if(s.tickSize > 0.0 && tv > 0.0)
      r.byTicks = dist / s.tickSize * tv;

//--- 3. contract size, only when no conversion is needed and the mode is linear
   bool linear = (s.calcMode == SYMBOL_CALC_MODE_FOREX_NO_LEVERAGE || s.calcMode == SYMBOL_CALC_MODE_CFD ||
                  s.calcMode == SYMBOL_CALC_MODE_CFDINDEX || s.calcMode == SYMBOL_CALC_MODE_CFDLEVERAGE ||
                  s.calcMode == SYMBOL_CALC_MODE_FOREX || s.calcMode == SYMBOL_CALC_MODE_EXCH_STOCKS);
   if(linear && s.contractSize > 0.0 && s.profitCcy == s.accountCcy && s.accountCcy != "")
      r.byContract = dist * s.contractSize;

//--- the common mistake, kept for comparison: POINT used where TICK SIZE belongs
   if(s.point > 0.0 && tv > 0.0)
      r.byPoint = dist / s.point * tv;

//--- choose: OrderCalcProfit first, then ticks, then contract size
   if(r.byCalc > 0.0)
     { r.perLot = r.byCalc; r.method = RL_CALC; }
   else if(r.byTicks > 0.0)
     { r.perLot = r.byTicks; r.method = RL_TICKS; }
   else if(r.byContract > 0.0)
     { r.perLot = r.byContract; r.method = RL_CONTRACT; }

//--- cross-check every pair of available values (the tolerance is explained in section 3)
   double tol = MathMax(RL_TOLERANCE, 1.5 * dist / MathMax(entry, 1e-12));
   double v[3];
   v[0] = r.byCalc;
   v[1] = r.byTicks;
   v[2] = r.byContract;
   for(int i = 0; i < 3; i++)
      for(int j = i + 1; j < 3; j++)
         if(v[i] > 0.0 && v[j] > 0.0 && RlRelDiff(v[i], v[j]) > tol)
            r.disagree = true;
   ...
   return (r.method != RL_NONE);
  }

The specification is read into a structure by RlReadSpec() . It puts the symbol in the Market Watch first, because the properties of a hidden symbol are not kept up to date, and it gives up with "no quote received yet" while there is no price. A tick value of 0 at startup then becomes a skipped signal instead of a division by zero.

3. What the demo account showed

The script RiskLotsReport.mq5 takes a buy with the stop 0.5% below the current price on every symbol, computes the loss per lot with each method and the lot size for a risk of 100, and writes everything to a CSV. The account had 38 Forex pairs, 2 metals, 2 energies, 10 indices and 793 stock CFDs. Some rows:

Symbol Profit currency OrderCalcProfit Ticks x tick value Lots for 100 USD Real risk
EURUSD USD 549.00 549.00 0.18 98.82
USDJPY JPY 502.23 499.74 0.19 95.42
EURJPY JPY 548.71 548.71 0.18 98.77
XAUUSD USD 1785.00 1785.00 0.05 89.25
GDAXI EUR 1385.94 1385.94 0.07 97.02
NI225 JPY 181.44 28 900.00 0.5 90.72
SPA35 EUR 1049.06 1049.06 0.1 (minimum) 104.91
AAPL USD 1.78 1.78 56 99.68

Figure 1. Loss per lot at a 0.5% stop by two methods, log scale. NI225 is the outlier.

NI225: a tick value that is not in dollars. The account reports a tick size of 1 and a tick value of 100 for NI225. A 289-point stop would then cost 28 900 per lot. OrderCalcProfit says 181.44 USD. The ratio is 159.3, which was the USDJPY rate at that moment, so the reported tick value is the value of one tick in yen. The seven other symbols with profit in JPY on the same account, EURJPY and USDJPY included, are converted correctly. Only this one is not. A sizing routine that reads only the tick value would trade NI225 159 times too small. Here the error goes in the harmless direction. If it went the other way, the same routine would multiply the risk by 159, and with one number there is no way to tell. With two numbers it is obvious.

USDJPY and the other USDxxx pairs: a small, steady gap. On all seven pairs, OrderCalcProfit is about 0.5% higher than the tick value. 0.5% is the stop distance, and that is not a coincidence. On a USD account the USDJPY loss is in yen and is converted to dollars with the USDJPY rate itself. The measured ratio shows that OrderCalcProfit converts the loss at the close price of the hypothetical trade, the stop, while the tick value is computed at the current price. A buy has its stop lower, where the same yen buy more dollars:

loss by OrderCalcProfit / loss by tick value = entry / stop
buy, stop 0.5% below:  1 / 0.995 = 1.00503  (measured: 1.00483 ... 1.00498)

Figure 2. Difference between the two methods on USDxxx pairs, and the expected value entry/stop - 1.

When the stop is hit, the yen are converted at roughly the stop price, so OrderCalcProfit is the one that matches the real loss. At 0.5% the gap does not matter. On a swing trade with a 3% stop the tick value understates the risk by 3%, and on a sell the sign flips. EURJPY does not show this because its yen go through USDJPY, not through its own price. It is also the reason the include file has no fixed tolerance: on these pairs two correct methods can differ by up to the stop distance, so the threshold is the larger of 2% and 1.5 times the stop distance in percent.

4. Point versus tick size

On this account the point and the tick size were the same on all 845 symbols, so dividing by the point, a common mistake in lot calculators, happens to give the right answer there. It does not on a contract quoted with more decimals than it trades in. A futures-style index with two decimals that moves in steps of 0.25 has a point of 0.01 and a tick size of 0.25. The test script creates exactly that as a custom symbol, with a tick value of 12.50:

rltest: RL_IDX point=0.01 tick=0.25 tv=12.5000 tvLoss=0.0000 mode=SYMBOL_CALC_MODE_EXCH_FUTURES
rltest: RL_IDX byTicks=500.00 byPoint=12500.00 byContract=0.00
rltest: OK   RL_IDX 10 points = 500 per lot
rltest: OK   RL_IDX point instead of tick size = x25

Ten points cost 500 per lot. Divided by the point it comes out as 12 500, and the lot size is 25 times too small. The contract-size method is skipped, correctly, because the calculation mode is not linear. The report script prints that factor for every symbol where the two values differ, so you see it before you trade the symbol. Prices sent with an order also have to sit on the tick grid. RlToTick() rounds a stop to the nearest tick, and the report marks stop distances that are not a whole number of ticks.

5. Rounding without adding risk

Once the loss per lot is known, the volume still has to fit the grid: a multiple of SYMBOL_VOLUME_STEP , not below SYMBOL_VOLUME_MIN , not above SYMBOL_VOLUME_MAX . I keep three rules:

  • Round down. 0.186 rounded to 0.19 is 2% more risk on every trade. That is why every value in the "Real risk" column is at or below 100.
  • Add a tiny epsilon before MathFloor(). 0.3 / 0.1 is 2.9999999999999996 in double precision, and without it you lose a whole step.
  • Do not hide the minimum volume. If the minimum volume already risks more than asked, the function still returns it, so a strategy that accepts that can trade, but with its own return code and the real risk next to it. SPA35 in the table is that case: 0.1 lots risks 104.91 when 100 was asked.

Testing found one more case. A specification can have a minimum volume that is not on the step grid, or smaller than the step. The custom test symbol ended up with a minimum of 0.00000001 and a step of 0.01, and my first version returned "minimum volume" with 0.00 lots after NormalizeDouble() . Now the smallest tradable volume is taken on the grid:

double RlMinLots(const double volMin, const double volStep)
  {
   if(volStep <= 0.0)
      return volMin;
   double m = MathCeil(volMin / volStep - 1e-9) * volStep;
   return (m < volStep) ? volStep : m;
  }

int RlLots(const double money, const double perLot, const double volMin, const double volMax,
           const double volStep, double &lots, double &riskAt)
  {
   lots = 0.0;
   riskAt = 0.0;
   if(money <= 0.0 || perLot <= 0.0 || volStep <= 0.0 || volMin <= 0.0)
      return RL_LOTS_FAIL;
   int code = RL_LOTS_OK;
   double minLots = RlMinLots(volMin, volStep);
//--- the small epsilon protects 0.3/0.1 = 2.9999999 from losing one step
   lots = MathFloor(money / perLot / volStep + 1e-9) * volStep;
   if(volMax > 0.0 && lots > volMax)
     {
      lots = MathFloor(volMax / volStep + 1e-9) * volStep;
      code = RL_LOTS_MAXCAP;
     }
   if(lots < minLots - 1e-12)
     {
      lots = minLots;
      code = RL_LOTS_MIN;
     }
   lots = NormalizeDouble(lots, RlVolDigits(volStep));
   riskAt = lots * perLot;
   return code;
  }

One more number I find useful, especially in the input description of anything that sizes by percent, is the smallest balance at which the minimum volume stays inside the chosen risk. RlMinBalance() returns it. On this account, with a 0.5% stop and 1% risk, it was 549 for EURUSD, 1 785 for XAUUSD and 10 491 for SPA35. Below that balance, "1% per trade" is not what the account is really risking.

6. Using it in an Expert Advisor

I use it as a gate in front of every order. If the specification is not ready or the methods disagree, the EA does not guess. It skips the signal and says why in the journal.

#include <RiskLots\RiskLots.mqh>

input double InpRiskPct = 1.0;   // Risk per trade, % of balance

//--- returns the volume to send, or 0 when the trade must not be opened
double VolumeForStop(const bool isBuy, const double entry, double &stop)
  {
   RlSpec s;
   string why;
   if(!RlReadSpec(_Symbol, s, why))
     {
      PrintFormat("No trade: %s", why);
      return 0.0;
     }
   stop = RlToTick(stop, s.tickSize, s.digits);          // stops must lie on the tick grid
   RlLoss r;
   if(!RlLossPerLot(s, isBuy, entry, stop, true, r) || r.disagree)
     {
      PrintFormat("No trade: %s (calc %.2f, ticks %.2f, contract %.2f)", r.note, r.byCalc, r.byTicks, r.byContract);
      return 0.0;
     }
   double money = AccountInfoDouble(ACCOUNT_BALANCE) * InpRiskPct / 100.0;
   double lots, riskAt;
   int code = RlLots(money, r.perLot, s.volMin, s.volMax, s.volStep, lots, riskAt);
   if(code == RL_LOTS_FAIL)
      return 0.0;
   if(code == RL_LOTS_MIN)
     {
      PrintFormat("No trade: minimum volume risks %.2f, more than %.2f", riskAt, money);
      return 0.0;                                       // or accept it, if the strategy allows
     }
   return lots;
  }

In an indicator, for example a panel that only displays the lot size, call RlLossPerLot() with allowCalc = false . The tick value and the contract size are still compared whenever both exist, and that covers every symbol whose profit currency is the account currency.

7. How it was tested

  • Custom symbols with known answers ( RiskLotsTest.mq5 ). Three temporary symbols: the index-like future above, a gold-like CFD (tick 0.01 worth 1.00 for 100 units) and a broken one with a tick value of 0. The script adds one quote to each and runs 19 checks: loss per lot, the factor 25, a buy with the stop above the entry, the minimum-volume code with its real risk, the minimum balance, 0.3 lots with a 0.1 step, the maximum-volume cap and the broken symbol. All 19 pass. The custom symbols report a loss tick value of 0, so the fallback to SYMBOL_TRADE_TICK_VALUE gets tested too. The symbols are deleted at the end.
  • A real account, with the report script from section 3. It does not know the right answer beforehand. It only shows where independent methods agree and where they do not, and the two cases above were explained from the data.

This does not cover every calculation mode a broker can use, and the numbers in section 3 belong to one account on one day. That is the reason the report script is included. It takes a few seconds on the account where the EA will actually trade.

8. Checklist

  • Tick size, not point. Stops on the tick grid.
  • SYMBOL_TRADE_TICK_VALUE_LOSS for a stop, and only after a quote has arrived.
  • Compare the tick value with OrderCalcProfit or the contract size where you can, and do not trade on a disagreement.
  • On pairs whose own price is the conversion rate, expect a gap equal to the stop distance and size with OrderCalcProfit.
  • Round down, take the minimum volume on the grid, and report what the minimum volume really risks.
  • Run the report on every new account. The same symbol name at another broker or prop firm can hide a different contract.

Conclusion

Position sizing fails without making noise, so one measurement is not enough. On 845 symbols the usual tick-value formula was right on 837, off by the stop distance on seven USDxxx pairs, and off by a factor of 159 on an index whose tick value was never converted to dollars. None of that produces an error message. Comparing independent methods turns it into a warning you can read and explain, and rounding the volume down makes sure whatever is left never adds risk. The files below can be used as they are or dropped into an existing EA.

File Description
MQL5\Include\RiskLots\RiskLots.mqh Specification reader, loss per lot by three methods with cross-check, volume rounding, minimum balance
MQL5\Scripts\RiskLotsReport.mq5 Report for every Market Watch symbol (or a list): journal table and CSV in the common Files folder
MQL5\Scripts\RiskLotsTest.mq5 Self-test on temporary custom symbols with known answers
Files: