Risk guide

Grid expert advisor: how grid and martingale mechanics add risk

A grid expert advisor places orders at fixed price steps and adds positions as the market moves against the first one. Martingale sizing makes each added position larger, so total exposure can grow fast. This guide explains both mechanics with a worked volume example, shows how margin, Margin Call and Stop Out work, and sets out what to check first.

Trading forex and CFDs involves substantial risk of loss. Automation applies rules; it does not remove trading risk. Read our risk disclosure.

Key takeaways

How a grid expert advisor works

A grid expert advisor divides price into fixed steps around a chosen level. It places an order at each step, and it adds a position each time price reaches a new step against the first one. The basket is closed when price returns far enough, or when the rule the publisher describes says so.

A price line falls through evenly spaced grid levels, with a bar added at each level that is twice the size of the one before. A side panel shows total volume rising, and an orange line marks where the trend continues and the floating loss grows.
Concept diagram, not an app screenshot.

In MT5 a grid can be built from pending orders. The MetaTrader 5 help defines a Buy Limit as “a trade request to buy at the Ask price that is equal to or less than that specified in the order.” A Sell Limit works the other way round, and each grid level can carry one of these orders.

Read the trading bot algorithms overview for how a grid compares with other families.

What a martingale expert advisor adds to the grid

Martingale sizing comes from gambling. The Wikipedia article on the martingale betting system says these betting strategies “originated from and were popular in 18th-century France” and describes the system as a “Gambling strategy where the amount is raised until a person wins or becomes insolvent”.

The same article gives the rule: “The strategy had the gambler double the bet after every loss, so that the first win would recover all previous losses plus win a profit equal to the original stake.” A martingale expert advisor applies that rule to position size, so each added position is larger than the one before by a fixed factor.

The article also states the limit that matters for any real account: “The martingale strategy fails even with unbounded stopping time, as long as there is a limit on earnings or on the bets”. Every trading account has such a limit, so a sizing rule that looks sound on paper can still exhaust the money available to it.

Worked example: how total volume grows with a multiplier of 2

Example (illustrative numbers, mechanics only, not a result or a forecast): the first order is 0.01 lots, and each next order is twice the one before.

Example: volume added at each level with a multiplier of 2. Arithmetic only.
LevelVolume added (lots)Total open volume (lots)
10.010.01
20.020.03
30.040.07
40.080.15
50.160.31

After five levels the total is 0.31 lots, which is 31 times the first order. At the contract size of 100,000 units used in the MT5 margin help for its EURUSD example, 0.31 lots is 31,000 units of exposure. The fifth order alone is 0.16 lots, just over half of the total.

The margin help gives the formula for Forex instruments: “Volume in lots * Contract size / Leverage”. At 1:100 leverage, the margin for 0.31 lots is 0.31 × 100,000 ÷ 100 = 310, in the margin currency. The sixth level adds 0.32 lots, and the total becomes 0.63 lots.

The table shows how volume stacks up, not what a grid earns. Every figure is arithmetic on a chosen example.

Why closed profits can sit beside a large floating loss

MT5 keeps two different totals. The MetaTrader 5 help defines balance as “Balance — money on the account, results of currently open positions not included (deposit).” Equity is different, and the same page says “equity is calculated as Balance + Credit - Commission +/- Floating profit/loss - Blocked.”

In a grid, earlier baskets can close with a profit that moves into balance. Positions from the current basket still carry a floating loss, and under the formula above that loss reduces equity. The same page notes that, depending on the broker’s trading conditions, “the equity value may or may not consider” floating profit and loss. An account can therefore show a run of closed gains and a sharp fall in equity at the same time. Both numbers are correct, because they measure different things.

How margin, Margin Call and Stop Out work

The MT5 help on executing trades defines margin as “Margin — money required to cover open positions and pending orders.” Free Margin is what remains: “Free Margin — the free amount of money that can be used to open positions.” Each grid order uses some of that free margin, so the number of levels an account can carry depends on its size and its leverage.

The same page defines Margin Level as “Margin Level — percentage of the account equity to the margin volume (Equity / Margin * 100).” It also says “The account state line is highlighted with the red color, if the account is in the Margin Call or Stop Out state.” The help does not give the threshold numbers, so check the levels with your broker before any test.

The Strategy Tester help describes the two levels in its margin settings. Of the first it says: “When this level is reached, the account switches to the Margin Call state.” Of the second: “When this level is reached, all orders are canceled and all trading positions are closed.” Those two sentences describe the tester’s own settings. On a live account, Stop Out is processed on the server. The executing-trades help says: “In a normal mode, in which FIFO-based closing is disabled, Stop Out causes the position having the largest loss to be closed first.”

Netting and hedging: how a grid is recorded

The account type decides how grid positions are stored. The MetaTrader 5 help says “The system used depends on the account and is set by the broker.” Confirm which system your account uses before you test anything.

On a netting account the basic principles page says “With this system, you can have only one common position for a symbol at the same time”. Each new grid order then joins that one position, and the open price is averaged. The executing-trades help says of a second deal in the same direction: “The open price is recalculated in this case — the weighted average open price is calculated”.

On a hedging account, the basic principles page says: “With this system, you can have multiple open positions of one and the same symbol, including opposite positions.” Each grid level then appears as its own position. The EA can be told apart from other programs by its magic number, which the executing-trades help describes as “Magic — the value specified by a trading robot when opening orders and positions (Expert Advisor ID)”. The margin help adds that “Oppositely directed open positions of the same symbol are considered hedged or covered”, and that “The calculation method is determined by the broker.”

What to read before running a grid or martingale expert advisor

Read the publisher’s own page before any test. Check each point below, and if the page does not state one, treat it as unknown:

Turn each lot figure into units with the forex lot size calculator before you run a test. Use only an EA you wrote, bought or licensed. Rules for prop-firm accounts are separate from broker rules, so read the prop firm bots guide and your firm’s own terms.

How to test a grid or martingale expert advisor

  1. Test in the Strategy Tester in the “Every tick based on real ticks” mode where your broker’s history supports it. The Strategy Tester help says of this mode: “Testing is performed using real ticks of financial instruments, accumulated by brokers.”
  2. Include long periods and at least one strong trend. A test in a sideways range shows only the calm case.
  3. Set leverage and margin in the tester to match your broker. The help says leverage “influences the amount of funds reserved on the account as the margin on positions and orders”.
  4. Do not use the Math calculations mode for a grid. The help says “Please note that margin control is not performed in this mode”, and it suggests that mode only for quick and rough estimates.
  5. Record the number of open levels, total volume and margin at each step, and compare those figures with the forward period.

YO4X is a Windows desktop workspace for supported MetaTrader 5 strategies, bot configuration and trading journals. It runs on 64-bit Windows 10 and Windows 11, and the current desktop workflow is gated to supported MT5 demo accounts. YO4X does not execute EX5 files or arbitrary uploaded code. See the download page for the app. Browse the EX5 research directory for listings, and read what an expert advisor is if the terms are new.

Frequently asked questions

What is a grid expert advisor?

A grid expert advisor places orders at fixed price intervals around a level. It adds positions as price moves against the first one, so the number of open positions depends on how far price travels.

Is a martingale expert advisor the same as a grid expert advisor?

No. A grid sets where orders are placed, and martingale sizing sets how large each order is. The two can be combined, or a grid can use a fixed size at each level. The publisher’s page should say which approach applies.

Does a grid expert advisor use a stop loss?

Not always. In MT5, a Stop Loss is attached to a position or a pending order, and a grid opens many of them. Ask the publisher how any stop, step cap or basket exit works. If the page is silent, treat the protection as unknown.

Why does margin matter so much for a grid?

Margin is the money required to cover open positions and pending orders, so every added level reserves more of it. When free margin runs out, no further level can be opened, and at the Stop Out level the server starts closing positions. The MQL5 reference lists the Margin Call and Stop Out levels among the properties of an account, so check them with your broker before any test.

Can a grid expert advisor be tested safely?

A test shows how the rules behaved on past data, not what will happen live. Use real ticks where the history supports them, and include long periods and strong trends. Use only an EA you wrote, bought or licensed.

Keep reading

Sources: Executing Trades - Trading Operations - MetaTrader 5 Help · Margin Calculation: Retail Forex, Futures - For Advanced Users - Trading Operations - MetaTrader 5 Help · Basic Principles - Trading Operations - MetaTrader 5 Help · Strategy Testing - Algorithmic Trading, Trading Robots - MetaTrader 5 Help · Account Properties - MQL5 Reference · Martingale (betting system) - Wikipedia

Trading forex and CFDs involves substantial risk of loss. Past performance and backtests do not guarantee future results. Read our risk disclosure before you trade.