RISK MANAGEMENT · FREE MQL5 SCRIPT · NOVEMBER 2026

How much can a grid lose? Work it out before it starts.

A grid EA shows you its settings: first lot, multiplier, step, number of orders. None of them is the number you need, which is how much the basket loses if every order fills and price keeps going. This free MQL5 script works it out level by level, with your broker’s own prices and margin rules, before a single order is sent.

BBenjamin SF · Founder Published Nov 20, 2026 Read 7 min

Six orders of 0.05 lots is not a risk number.

A WORKED EXAMPLE, IN ROUND NUMBERS

Take a gold position that went wrong: one BUY of 0.10 lots at 2,000.00. The daily ATR is 30.00, and the grid adds an order every 0.35 ATR (10.50), starting at 0.05 lots and growing 1.5 times per level, up to six orders. The numbers are round on purpose so the arithmetic is easy to follow. It is not today’s gold price and it is not a backtest.

LevelFill priceLotBasket lotsBreak-evenBasket P/L (USD)
11,989.500.050.151,996.50-105.00
21,979.000.080.231,990.41-262.50
31,968.500.110.341,983.32-504.00
41,958.000.170.511,974.88-861.00
51,947.500.250.761,965.88-1,396.50
61,937.000.381.141,956.25-2,194.50
+5 ATR1,787.00–1.141,956.25-19,294.50

When the sixth order fills, the basket is 2,194.50 USD down. That is usually the figure people have in mind when they say their grid “risks about two thousand”. But price has no reason to stop at the last order. If it carries on for five more daily ATRs, the same 1.14 lots are 19,294.50 USD down. About 89% of the worst case happens after the grid has nothing left to add.

On a 10,000 USD account at 1:100 leverage with a 50% stop-out level, it never gets that far. The full grid ties up about 2,230 USD of margin, and the broker closes everything roughly two ATRs past the last order, near 1,878, with about 8,900 USD gone. At that point the account is what limits the loss, not the EA.

What the script reports.

ONE RUN, NOTHING SENT TO THE BROKER

Drop it on a chart and it reads either the positions you already hold on that symbol or a hypothetical BUY or SELL at the current price. Then it walks the grid and prints this to the Experts journal:

  • the fill price, the lot and the basket break-even at every level;
  • the basket P/L at every fill, with the equity and margin level the account would have at that moment;
  • the final loss once price has run N ATRs past the last order, in money, as a share of balance and as a change from your current equity;
  • the margin the full grid needs and the free margin left after it;
  • the first level at which your broker’s stop-out would close the account, if there is one.

A one-line summary also appears in the top-left corner of the chart. The script never opens, changes or closes a position.

How it works, in MQL5.

THE FOUR PARTS WORTH READING

01

The ladder

Each level sits one step further from the basket’s average entry. The step is either a fixed number of points or a fraction of the ATR of the last closed bar, so the result does not shift while the current bar is still forming. It can also grow from level to level (1 means equal spacing). If a level would land at zero or below, the loop stops there.

for(int i = 0; i < InpMaxOrders && !IsStopped(); i++)
{
   distance += step0 * MathPow(InpStepGrowth, i);
   double price = NormalizeDouble(anchor - dir * distance, _Digits);
   if(price <= 0)
   {
      PrintFormat("Level %d would be at a price <= 0: the grid stops at level %d.", i + 1, i);
      break;
   }
   double lot = LotForLevel(i);
   double m = 0.0;
   if(!OrderCalcMargin(type, _Symbol, lot, price, m))
   {
      Print("OrderCalcMargin failed, error ", GetLastError());
      return;
   }
   gridMargin += m;
   int n = ArraySize(legs);
   ArrayResize(legs, n + 1);
   legs[n].lots  = lot;
   legs[n].price = price;
   // ... basket P/L, break-even, equity and margin level at this fill
}
02

The lot progression

Fixed, geometric or Fibonacci. Every lot is rounded to the symbol’s volume step and limits, so the script works with the same sizes the broker would accept, not with 0.075 lots that can never be traded.

double LotForLevel(int level)
{
   double lot = InpFirstLot;
   if(InpLotStep == LOTS_GEOMETRIC)
      lot = InpFirstLot * MathPow(InpLotMult, level);
   else if(InpLotStep == LOTS_FIBONACCI)
   {
      double a = 1.0, b = 1.0;              // 1, 1, 2, 3, 5, 8 ...
      for(int k = 0; k < level; k++)
      {
         double c = a + b;
         a = b;
         b = c;
      }
      lot = InpFirstLot * a;
   }
   if(InpMaxLot > 0)
      lot = MathMin(lot, InpMaxLot);
   return NormalizeLots(lot);
}
03

P/L in your account currency

There is no tick-value multiplication anywhere. OrderCalcProfit asks the terminal for the P/L of each leg at a given price, so a GBPJPY grid on a EUR account comes out in euros at current rates. Margin works the same way through OrderCalcMargin.

bool BasketPnL(const Leg &legs[], int dir, double closePrice, double &pnl)
{
   pnl = 0.0;
   ENUM_ORDER_TYPE type = (dir > 0) ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
   for(int i = 0; i < ArraySize(legs); i++)
   {
      double p = 0.0;
      if(!OrderCalcProfit(type, _Symbol, legs[i].lots, legs[i].price, closePrice, p))
      {
         Print("OrderCalcProfit failed, error ", GetLastError());
         return false;
      }
      pnl += p;
   }
   return true;
}
04

The broker’s stop-out

MT5 tells you how your broker closes accounts: by margin level as a percentage, which is the common case, or by equity in money. The script reads which one applies and checks it at every fill and at the final price.

bool StopOutHit(double equity, double margin)
{
   ENUM_ACCOUNT_STOPOUT_MODE mode = (ENUM_ACCOUNT_STOPOUT_MODE)AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE);
   double level = AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);
   if(mode == ACCOUNT_STOPOUT_MODE_PERCENT)
      return (margin > 0 && equity / margin * 100.0 <= level);
   return (equity <= level);
}

Running it in five minutes.

MT5, METAEDITOR, ONE CHART

  1. Download GridWorstCase-v1.00.mq5 (MQL5 source, about 17 KB).
  2. In MetaTrader 5, open File → Open Data Folder, go to MQL5 → Scripts and copy the file there.
  3. Open it in MetaEditor (F4 from MT5) and press F7 to compile.
  4. Back in MT5, drag GridWorstCase from Navigator → Scripts onto the chart of the symbol you want to check.
  5. Fill in the inputs and read the result in Toolbox → Experts.

Basket to analyse picks your open positions on that symbol (one side only; filter them by magic number, -1 for all, 0 for manual trades) or a hypothetical BUY or SELL with its own lot. The rest of the inputs are your EA’s grid settings: first averaging lot, multiplier, progression, step in points or as a fraction of ATR, step growth and maximum orders. Adverse run past the last add is the stress, five daily ATRs by default. Try it on a demo account first.

Reading the answer.

WHAT TO DO WITH THE NUMBER

If a stop-out shows up before the last level, the grid as configured can never finish on that account. Something has to give: fewer orders, a smaller multiplier, a wider step or more equity behind it.

Then compare the final loss with the most you are willing to lose on one basket, and on a prop account with the firm’s maximum drawdown and daily loss limit. A recovery basket can take its whole loss in a single session, so the daily limit is often the first rule to break. Check the firm’s rules before any of this, though: many prop firms do not allow grid or averaging strategies at all, as the martingale guide explains.

Five ATRs is a stress assumption, not a forecast. Change it, run the script again and watch how quickly the number grows.

WHAT THE NUMBER LEAVES OUT

Spread, commission, swap and slippage are not included, so the real worst case is somewhat worse than the printed one. Fills are assumed at the exact level price, and a weekend gap can skip several levels at once. The levels hang from the basket’s average entry; an EA that spaces its orders from the last fill will place them at slightly different prices. Only one side is analysed: if you hold BUYs and SELLs on the same symbol, filter by magic number or use a hypothetical basket.

Why we wrote it.

THE SAME CHECK RUNS INSIDE SFRECOVERY EA

SFRecovery EA, our recovery manager for MT5, does this calculation before every cycle with its own grid engine. It shows the worst case on the panel, in the journal and in Telegram, adds it up across every chart where it runs, and can refuse to start a cycle when the worst case is above the share of equity you set. For the same inputs, the script and that pre-launch check use the same arithmetic.

The script is the standalone version. It works with any grid EA, ours or not, as long as you copy that EA’s settings into the inputs. If you want the background first, the recovery EA guide covers how recovery grids work and where they fail, and the setup guide goes through every SFRecovery setting.

FAQ

Quick questions.

Email support@signalforge-ai.com if yours isn’t here.

How do I calculate the worst case of a grid EA?

+
Add up the loss of every leg at the price where the last order fills, then again at a price further out to cover the move after the grid is full. Per leg that is lots x (entry - exit) x contract size, converted to your account currency. The script does it with OrderCalcProfit at every level.

Why does the script use 5 ATRs past the last order?

+
Because the grid’s own levels cover only part of the move. Five daily ATRs is the default stress, the same default SFRecovery EA uses. It is an assumption you can change, not a prediction.

Does it work with any grid or martingale EA?

+
Yes, if you copy that EA’s grid settings into the inputs: first lot, multiplier, progression, step and maximum orders. It reads your open positions, but it cannot read another EA’s settings.

Does the script open or close trades?

+
No. It only reads prices, positions and account data, prints the results to the Experts journal and leaves a one-line summary on the chart.

Why could my real loss be bigger than the script says?

+
Spread, commission, swap and slippage are left out, fills are assumed at the exact level price, and gaps can skip levels. Under the same continuation, the real loss can only be equal or larger.
B
Benjamin SF · Founder of SignalForge
TRADER · ALICANTE

Benjamin SF is the founder of SignalForge and an expert in trading algorithm automation. He builds and operates the SignalForge bridge and the SFCloud hosted-MT5 fleet for prop-firm traders.

The worst case, before every cycle

SFRecovery EA works it out at every launch and can refuse to start above your limit. Try the free demo in the MT5 Strategy Tester.

SFRecovery EA is a position manager, not a trading system. The figures on this page are arithmetic for a hypothetical grid, not test results. Trading involves risk of loss.

Free MQL5 script — the worst case before the grid startsDownload →