Orders and fills: how a backtest turns actions into trades
How a backtest turns actions into fills: market and pending orders, slippage, stops and targets, gaps, trailing stops, netting and volume rounding.
Solo in inglese per ora.Apri la pagina in inglese
This page states, rule by rule, how a backtest turns your strategy's actions into orders, fills and closed trades: which price, which side, at which moment, with which rounding. Where tick mode and bar mode differ, both rules are given. On a live account your broker executes the orders: what changes is on Live execution.
Terms used on this page
Tick mode, bar mode, the close side, a point, the primary symbol, and hedged and netting accounts are defined in the Glossary.
The run settings that shape your fills
Three run settings shape fills: the mode (tick or bar), the slippage and the leverage (margin); so does the account type (hedged or netting), which is the strategy's own, not the run's. Their defaults and allowed values are on Costs and the account → account settings.
Market orders
Fill price and timing
A buy fills at the ask, a sell at the bid, then slippage is applied (below).
Tick mode: the order fills on the same tick on which its rule fired, at that tick's quote. No latency is simulated.
Bar mode: the rule is decided on the bar's close and the order fills at the next bar's open of its symbol: the bid is that bar's open price, the ask is that open plus the bar's spread (below). Closes, partial closes, position modifies, and placing, cancelling and modifying a pending order are delayed the same way, in action order.
Bar mode, levels: the stop loss, take profit, volume and trailing values of a delayed order are computed at the fill (the next bar's open), from the data the decision read: an On Candle Close order never reads the bar it fills into. For an On Tick or On Timer rule, a higher-timeframe value is the bar as it has developed up to the fill.
Bar mode, spread: the ask is the bar price plus the bar's effective spread (spread).
Bar mode, timestamps: every event of a bar — a delayed fill, a pending fill, a stop or target exit, an expiry — is stamped with the bar's open time (whole minute). Tick mode stamps the tick's time.
When an On Candle Close rule fires in tick mode
- In a run that loads more than one symbol, the rule can fire on another symbol's tick (when candle closes fire). Each order then fills at its own symbol's latest quote at that moment — for the primary symbol, possibly its last quote of the bar that just closed.
Orders on a second symbol
- Tick mode: an order on another symbol fills at that symbol's last tick, which can be older than the tick that fired the rule. If that symbol had no 1-minute bar in the minute before (its market paused or closed), the order waits and fills at its next tick instead, the close of one of its positions included (paused symbols). An On Tick rule runs only on ticks of the symbol named in its action; a close or cancel action without a symbol runs on every symbol's ticks.
- Bar mode: a second symbol's 1-minute bars are read one bar behind the primary symbol's at a candle close (read conventions), but its delayed orders fill at the open of that symbol's own next 1-minute bar, and its stops, targets and pending orders are tested on its own bars.
- See also bar mode with several traded symbols.
Slippage
Slippage is a fixed number of points per fill, set per run (default 0; a negative value is refused). It is the same whatever the order's size, the volatility, the time of day or the size of a gap, and it is not random. Whether a stop, a target or a pending order is reached is decided on the unslipped price; only the fill moves.
The slippage setting, in points, is applied adversely to every simulated fill: market opens, pending fills, stop-loss and take-profit exits (trailing and break-even exits included), close and partial-close actions, stop-outs and the end-of-run close. A buy fills higher by slippage × point, a sell lower. It is never favourable. Take-profit and limit fills are slipped too.
A virtual symbol can set its own slippage for one instrument; it replaces the run value, and 0 means no slippage on that instrument.
The margin check is made on the price before slippage.
Commission on a fill
- Commission is charged on each leg, on that leg's slipped fill price; how much, and when it reaches the balance, is on Costs and the account → commission.
A rule that stays true
- Nothing limits an action to one order per signal. Every time an open rule is evaluated with its conditions true, it sends a new order: an On Tick rule on every tick, an On Candle Close rule once per bar. On a hedged account each one is a new position; on a netting account it adds to the position. Gate entries yourself, for example with an open-positions count.
- Two open rules gated on "no open position" can both open when they are decided together: in bar mode, and live, but not in tick mode (two actions in one evaluation).
Stop loss and take profit
How they are set
- A stop loss and a take profit are absolute prices, not distances. Each is optional; a missing value or 0 means none. Computed prices are rounded to the instrument's digits before use.
A stop or target on the wrong side of the market
- A market order whose stop or target is on the wrong side of the price the position would close at is refused, in the backtest and live alike, as MetaTrader 5 and brokers refuse it (comparison). The stop is measured on the closing side: a buy's stop must be below the bid and its target above it; a sell's stop above the ask and its target below it. So a buy whose stop sits inside the spread — above the bid, below the ask — is refused too, instead of opening and stopping out on its first tick.
- A pending order is refused when its price is on the wrong side of the market, and also when its stop or target is on the wrong side of its own price (a buy order's stop at or above its price, or its target at or below it).
- The stops level. When your virtual symbol (or the broker preset it comes from) states the broker's minimum stop distance, in points, a stop or target nearer the closing price than that distance is refused as well, a stop the trailing ratchet moves is kept at least that far from the price, and a modify's stop is moved out to that distance, as live does with the broker's own. With no stops level stated, no minimum applies.
- A refused order sends nothing and leaves no trade; the rule is evaluated again at its next tick or bar. (after a refusal).
Tick mode: check and fill
Checked on every tick of the position's symbol. A long is tested against the bid, a short against the ask. Stop: bid ≤ stop (long), ask ≥ stop (short). Target: bid ≥ target (long), ask ≤ target (short). An exact touch triggers. The stop is tested before the target.
The exit fills at the breaching tick's close-side price, not at the level: a stop the market gaps through fills worse than the level, a target gapped through fills better. Slippage is then applied.
This rule was checked against MetaTrader 5's tester on the same ticks: 12 of 12 sampled stop-loss closes and 10 of 10 take-profit closes equal the breaching tick's close-side price.
Bar mode: check and fill
Checked once per bar, on the bar's high and low. Long: low ≤ stop closes at the stop; otherwise high ≥ target closes at the target. Short (it closes at the ask, so the bar's prices are raised by the bar's spread): high + spread ≥ stop closes at the stop; otherwise low + spread ≤ target closes at the target.
Stop before target: when one bar's range covers both levels, the stop wins, whatever the real path inside the minute. This is deliberately pessimistic. MetaTrader 5's "1 minute OHLC" mode replays four prices per bar and can reach the target first (comparison).
A position opened in the bar: a delayed market fill happens at the top of the bar, so the new position is exposed to that whole bar's range and can be stopped in the same bar (open time = close time = the bar's open time). A position closed at the top of the bar escapes the bar's range. A position a pending order opened inside the bar meets a stop there at the stop's level.
Each symbol on its own bar: a position's stop and target are tested on its own symbol's 1-minute bar, at a minute where that symbol has one.
Bar mode knows only the four prices of a bar. When bar mode and tick mode disagree, tick mode is the reference.
When the price gaps through a level
There are no synthetic prices over a gap (a weekend, a holiday, missing data): the first price after it is simply the next step (bars).
Tick mode: stops, targets and pending orders are checked on the first tick after the gap. If that tick is beyond the level, the exit or the order fills at that tick's price — after a weekend, the reopen price. A stop loss or a stop entry pays the gap; a take profit or a limit entry gets the better price.
Bar mode: a stop loss or a stop order that the bar opens beyond fills at the bar's open, the first price there was (for example a Monday open far below Friday's stop), so the gap is paid as in tick mode. A take profit or a limit order keeps its level, which is pessimistic on such gaps. A level the bar reaches from inside fills at the level.
In both modes, the slippage you set is added on top.
Netting accounts
- A netting position has one stop and one target. A hit closes the whole net position, one closed-trade row per opening fill still in it.
Trailing stop and break-even
Units: the trailing activation, the trailing distance ("step" in the builder) and the break-even activation are price distances (0.0020 = 20 pips on EURUSD), not points and not prices. 0 switches a mechanism off.
Trailing: once the profit on the close side (bid − entry for a long, entry − ask for a short) reaches the activation distance, the stop is set to bid − distance (long) or ask + distance (short), rounded to the instrument's digits. It only ever tightens. There is no minimum increment: it moves whenever the new level is at least one price step tighter. In bar mode the bar's high (long) or low + spread (short) is used.
Break-even: once the close-side profit reaches the break-even activation, the stop moves to the entry price (the slipped fill price), with no offset. It can create a stop on a position opened without one. With both mechanisms on, the stop is the tightest of the current stop, the entry and the trailed level.
Check, then move: on each tick (bar) the stop is first checked as it stood, then moved for the next tick (bar). A stop moved on a tick cannot fire on that same tick; in bar mode the trail follows one bar behind, except that a stop moved beyond the bar's close was crossed inside the bar, so the position closes in that bar at the stop's level.
The distances are read once, when the order is sent (bar mode: at the fill), and change only through a modify action.
A trailing distance smaller than half a price step rounds the stop onto the market price itself, so the next check closes the position.
Positions opened by pending orders trail too: the distances travel with the order.
Netting: the trailing and break-even settings are those of the order that created the net position; later orders that add to it do not change them. Profit is measured from the position's volume-weighted average entry.
A trailing or break-even exit is reported with the exit reason Stop loss: the trade list cannot tell it apart from an original stop.
Pending orders
Types
- Buy limit, buy stop, sell limit, sell stop. There are no stop-limit, OCO or trailing pending orders. A pending order needs a symbol, a volume and an order price; stop loss, take profit, expiration and the trailing/break-even distances are optional.
Checks at placement
- In this order: the numbers must be usable (see Every reason an order is refused), with an order price above 0; the order must rest on the correct side of the current price — a buy stop above the ask, a buy limit below the ask, a sell stop below the bid, a sell limit above the bid (equal is refused); the volume must fit the volume rules. A refused placement leaves no order.
- In bar mode the order is placed at the next bar's open, and "the current price" is that open (ask = open + spread).
- No margin is reserved at placement; margin is checked when the order triggers (below).
Trigger and fill — tick mode
- Checked on every tick of the order's symbol. Buy limit: ask ≤ price. Buy stop: ask ≥ price. Sell limit: bid ≥ price. Sell stop: bid ≤ price. An exact touch triggers.
- The order fills at the triggering tick's ask (buy) or bid (sell), then slippage: a limit fills at its price or better, a stop takes any gap.
Trigger and fill — bar mode
- Checked once per bar of the order's symbol, before the stop/target check. Buy limit: low + spread ≤ price. Buy stop: high + spread ≥ price. Sell limit: high ≥ price. Sell stop: low ≤ price.
- A limit order fills at its order price, then slippage. A stop order fills at its order price, or at the bar's open when the bar opened beyond it (a buy stop at the open's ask), then slippage. The new position is then checked against the same bar's stop and target.
Expiry
An order expires only if its Order expiration time returns a date; without one it stays until it fills, you cancel it, it is cancelled on margin at its trigger, a periodic drawdown limit cancels it, or the run ends.
Expiry is checked before the fill, against the current time (≥ expiration): an order whose expiry has passed on the tick that would fill it expires instead. In bar mode the current time is the bar's open, so an order can still fill during the minute in which it expires.
There is no time-in-force setting (no "end of day" or "specified day" order): only the expiration time expires an order, in the backtest and live.
Cancelling and modifying
A cancel action ("Buy (cancel limit)" and the like) removes every resting order of that kind on every symbol — only the parent action's orders when the cancel has a parent. It acts at once in tick mode, and at the next bar's open in bar mode.
A modify action ("Buy (modify limit)" and the like) sets any of price, expiration, stop loss, take profit and the trailing/break-even distances on every resting order of that kind on the action's symbol (only the parent's when it has one). A value left out is unchanged. The whole modify is refused if a number is unusable, the new price would make the order marketable, or the new expiration is not after the current time. The order keeps its place. It acts at once in tick mode, and at the next bar's open in bar mode (checked against that market).
Margin when the order triggers
- When an order triggers, the margin it would add — valued at the trigger price before slippage — must fit the free margin. If it does not, the order is cancelled and no position opens. On a netting account an order that reduces, closes or flips the position adds no margin (or only the surplus of a flip), so it always fills.
Same-step rules
- Pending orders are checked first on each tick (bar), before any of your rules run — as a real broker fills a resting order before your code sees the tick. A cancel rule therefore cannot stop an order that fills on the same tick, for example at the weekend reopen.
- An order never fills on the tick that placed it. In bar mode an order is placed at the open of the bar after the one that decided it, and can fill inside that bar.
- When several orders trigger together they fill newest first, and each later one is checked against the margin the earlier ones took.
Changing open positions
"Buy (modify position)" / "Sell (modify position)" set the stop loss, take profit and trailing/break-even distances of every open position of that side on the action's symbol, or only the parent's positions. A value left out is unchanged; 0 removes a stop or target or switches a mechanism off; a stop may be loosened. Prices are rounded to the instrument's digits.
The whole modify is refused if a value is unusable or would put the stop or target on the wrong side of the closing price (a buy's stop at or above the bid, or its target at or below it; a sell's mirrored on the ask).
Bar mode delays it to the next bar's open, together with the market orders, in action order.
On a netting account it acts on the whole net position; a parent scope is refused when the strategy is saved.
Closing positions
Close actions act on every symbol
- "Buy (close)" closes every open buy position on every symbol; "Sell (close)" every sell position. The symbol set on the close action decides only when an On Tick close runs (on that symbol's ticks), not what it closes. With a parent action, only the parent's positions are closed.
- A long closes at the bid, a short at the ask, plus slippage (bar mode: at the next bar's open). Exit reason Strategy signal. A position on a symbol that is paused when the close is sent closes at that symbol's next quote (paused symbols).
Closing one specific position (hedged only)
- A close can select the Nth open position of its side (and parent scope) by placement order: 0 = the oldest placed. A pending order that filled keeps its placement slot. The numbering shifts as positions close. An index beyond the open positions closes nothing. A netting strategy cannot use the selector (refused when saved).
Partial close
A partial close is written in the strategy's text form (
closePartial buy/closePartial sellwith acloseVolume), by you or by an AI app you connect; the visual builder has no partial-close action. It must carry a close volume, in lots.Like a close, it acts on every symbol and on every matching position: each position of the side (and parent scope) loses the close volume. "Close 0.5 lots" with three open buys closes 0.5 lots of each.
Per position: a close volume at or above the position's volume closes it fully; a smaller one is rounded down to the volume step; one that rounds down to 0 leaves that position untouched.
The opening commission and swap are shared out pro rata between the closed part and the remainder.
Trades in the report
- Each closed slice of a position is one row of the trades list: a partial close adds a row with the same ticket on a hedged account, and a netting reduce adds one row per fill it consumes (one trade is one closed slice).
- On a hedged account a pending order keeps its ticket when it fills, and the position's open time is the fill time.
- A trade's profit is converted to the account currency at the close (conversion).
Hedged and netting accounts
Hedged: every order opens its own position with its own stop, target and lifetime; buys and sells on one symbol coexist.
Netting: one position per symbol. An order in the same direction adds to it; an opposite order reduces it, closes it or flips it.
Adding (netting): a non-zero stop or target on the new order replaces the position's (last write wins); 0 keeps the existing one. The break-even and trailing settings follow the same rule, each on its own: a non-zero value on the new order replaces the position's, 0 keeps it. Each fill keeps its own entry price; the position's entry is their volume-weighted average.
Reducing (netting): an opposite order closes the oldest fills first (FIFO) up to its volume, one trade row per fill slice, at the order's fill price, exit reason Strategy signal. Larger than the position, the remainder opens a new position the other way at the same price, with the new order's stop, target and trailing settings.
When an opposite order is smaller than the position, it only reduces it: its stop and target are ignored, and the position that remains keeps its own (a sell's stop sits above the price, so on the remaining long it would be on the wrong side). Live, the remaining position likewise keeps its levels.
A close with a parent action closes only that parent's fills, oldest first.
Volume
- A computed size (risk %, fixed money, exposure, one lot every N) is rounded down to the volume step and then kept between the minimum and maximum volume: a positive size below the minimum is raised to the minimum, so on a small account a risk-based rule can risk more than its percentage; a size above the maximum is cut to the maximum; a size of 0 or less stays 0 and no order is sent.
- A fixed lot size is used as written and the order is refused if it is below the minimum, above the maximum or not a whole number of steps.
- Minimum, maximum and step come from the instrument catalog, replaced by your virtual symbol's values when it sets them; sizing and the order check use the same values. A catalog instrument with no stated maximum has none. A minimum above the maximum stops the run with an error naming the instrument.
- There is no cap on positions per symbol; only the engine limits below apply.
Refused orders
How much margin an order needs, and when the account is stopped out, are on Costs and the account → margin and stop-out. This section lists every reason an order is refused.
Every reason an order is refused
| Order | Refused when |
|---|---|
| Market open | a number is not a finite value (volume, stop, target, trailing or break-even values); a price, stop, target or distance is negative; a stop or target on the wrong side of the closing price, or nearer it than the stops level; the volume breaks the volume rules; the margin does not fit |
| Pending placement | the same number rules, and an order price of 0; the order price on the wrong side of the market; a stop or target on the wrong side of the order price, or nearer it than the stops level; the volume rules |
| Pending trigger | the margin does not fit (the order is cancelled) |
| Position modify | an unusable or negative value; a stop or target on the wrong side of the closing price (whole modify refused) |
| Order modify | as above, plus a price of 0 or less, a marketable price, an expiration not after now |
| Close / partial close | never refused; a selector beyond the open positions, or a close volume rounding to 0, does nothing |
A value that is not a number typically comes from an indicator that has no value yet, or from a symbol's price before its first quote, in a backtest or after a live deployment's start (a symbol with no price yet). 0 is legal for a stop, target or distance and means "none".
Before the run starts, a strategy is refused if an open has no symbol or volume, a pending order has no order price, a partial close has no close volume, or a modify sets no level.
After a refusal
Nothing stops. The refused order leaves no trade and no balance change; the rule is evaluated again at its next tick or bar and may send again. A pending order cancelled on margin does not come back.
A run without a replay recording keeps no trace of refusals: the report has no refused-orders count. With Record a replay on, the replay shows each refused market or pending order.
End of run, drawdown limits and engine limits
- End of run: resting orders are cancelled and every open position is closed with exit reason Forced close; these trades count in every figure of the report. The exact prices and time stamps are on the execution model → the end of a run.
- Drawdown limits in a backtest: a timed breach cancels every resting order, closes every position and pauses the rules until its window passes; an Overall breach cancels and closes the same way and stops the run. These closes are stamped at the breaching tick or bar and carry the exit reason Drawdown limit. Exact rules: Drawdown limits → in backtests.
- Engine limits: a run that exceeds the closed-trade cap or the cap on open positions and resting orders fails with an error instead of finishing (limits that can stop a run).
Order of operations
The full order of events inside one tick and one bar is on the execution model → order of operations. For fills it means: pending orders are processed before your rules; an On Candle Close entry meets its first stop/target check on the same tick, an On Tick entry on the symbol's next tick; and in bar mode, everything decided on a bar executes at the next bar's open. When several actions fire together, closes, deletes and modifies come before opens (several actions in one evaluation).
What you can see afterwards
- The trades list, the CSV export and the exit reasons are defined on Report charts, trades list and Compare → the trades list.
- Pending orders that never filled, refused orders and the moves of a stop or target are not in the report; they appear only in a replay recorded with Record a replay, which is off by default (what a replay records).
Bar mode with more than one traded symbol
In bar mode, stops, targets, pending orders and trailing stops are tested against their own symbol's 1-minute bar, and orders fill at that symbol's own next open. A minute where only a second traded symbol has a bar is processed for that symbol too. Details: the execution model.
Live orders
On a live account the same rules decide whether to send an order; your broker executes it. How live orders are sent, filled and confirmed, where your stop loss and take profit live, the broker's minimum stop distance, refusals and the pause before a refused open is retried, partial-close rounding, pause and stop are on Live execution → orders, stops and fills; the complete list of differences is the table on Live execution → live versus a backtest.
Not yet documented
- Which side of the price (bid or ask) the broker uses to trigger your live stop loss and take profit (the platform leaves the broker's default).
Differences from the MetaTrader 5 tester
Every difference from the MetaTrader 5 Strategy Tester that can change a fill — bar mode against "1 minute OHLC", slippage, the stops level, margin, stop-out, pending-order types, close actions, time stamps, multi-symbol strategies — is on Compared with the MT5 Strategy Tester. Tick mode is the mode whose fills were checked against the MT5 tester trade for trade.
Not yet documented
- What MetaTrader 5 does with the stop and target of a netting order that only reduces a position, so whether our rule matches MT5 there.
Domande?support@algotradinghub.io