Order execution model
Every Matana Labs backtest walks history one second at a time, with one price and one traded volume per second. Orders fill from that price and that volume, by the rules below; every default value on this page is read from the code when the site is built.
One second at a time
A second is labelled by its end: 09:30:01 is the second from 09:30:00 to 09:30:01. Its price is the price trading actually had in that second (see data quality); a second without trades carries the previous price, is flagged, and nothing fills in it. Inside every second the engine works in a fixed order:
- protective exits of open positions: trailing stop, then stop, then target;
- resting orders: limit and stop orders, and orders that waited for a second with trades;
- the strategy’s rules, which read the state after steps 1 and 2 (see no look-ahead);
- the rules’ market orders, at this second’s price;
- in intraday mode, the day-end close.
So a position opened in a second is first checked against its stop in the next second, and a rule never sees a fill that has not happened yet.
Order types and their fill prices
| Order | Fills when | At what price |
|---|---|---|
| Market | In the second the rule fires; if that second had no trades, in the next second with trades | That second’s price, plus slippage (and the ask or bid with the spread model) |
| Limit | A second’s price is at the level or better (with the spread model: the ask for a buy, the bid for a sell) | The level, or the better price if the market jumped past it |
| Stop (entry or exit) | A second’s price reaches the level | That second’s price: a jump past the level fills worse than the level |
| Protective stop | A second’s price is at or beyond the stop | That second’s price; never capped by volume |
| Target | The second’s price (the bid for a long, the ask for a short) reaches the target | That second’s price |
| Trailing stop | The price retreats from the best price since entry by the distance | That second’s price |
Stops, targets and trails take a distance in percent, in dollars or as a multiple of ATR, or an exact level. A limit or stop order lives until the end of the day by default; it can also have a lifetime (ttl 10m), stay overnight until filled (gtc) or live for one second only. A newly placed order is first checked in the next second; the fillSameSecond setting (off by default) lets an order whose price is already reached fill in the second it was placed.
Two settings change how strict a limit is, both off by default: limitCross fills a limit at its level when the price jumped over it within one second, and limitThrough fills only when the price traded through the level, not just touched it, which is closer to how a queue works.
Stop-limit, MIT and OCO
The demo account and the prop challenge practice in Prop Lab also take stop-limit orders (the stop price arms a limit; the fill is never worse than the limit), market-if-touched orders (a touch of the level fills at market, with slippage against you) and one-cancels-other groups. A level the market gapped through fills at the first price after the gap, and when in doubt the fill is the worse one.
Volume, queue ahead and partial fills
A trade can take only part of each second’s volume:
available = (volume of the second − queue ahead) × participation
| Setting | Default | What it does |
|---|---|---|
Queue ahead (firstOrders) | 3,700 shares per second | Volume taken by other traders’ orders standing ahead of yours |
| Participation | 10% of what is left (new strategies) | Your share of the second’s remaining volume |
Minimum volume (minVolumeToTrade) | 100 shares | Seconds with less volume are skipped for new trades |
- A limit order that gets part of its size keeps the rest at the same level and fills it in the following seconds while it is alive.
- A market order from a rule takes what the second has; the rest is dropped and shown in the run’s execution statistics. “Close all” keeps closing in the next seconds until the position is flat.
- Exits that must happen: protective stops, the day-end close, the emergency and daily-loss guards and margin liquidation, are not limited by volume.
- With
sharedVolumeon, all trades in one second share its volume; off, each one sees the whole second. - Futures volume is counted in contracts and crypto volume in dollars, converted to the position’s units by the server.
- Switching the volume cap off makes every order fill in full: useful to test logic, too optimistic for results.
Gaps
When the price jumps past an order’s level without trading at it, overnight or across a stretch of seconds without trades, the order fills at the real price after the jump, not at your level. That is the default. A setting (gapAtOrderPrice) fills the built-in mechanisms’ resting orders at their level instead, which is the optimistic choice and is labelled as such in the app. A limit order the market jumped through in your favour fills at the better real price.
Forced exits are the end-of-day close, the emergency, virtual and daily stops, the end of the backtest range, a margin liquidation and a regime exit. When one falls in a second without trades, by default the day-end close and the end of the range fill at once at the carried price, as they always have, and so do the emergency and virtual stops of a long position; the other forced exits are limited by the second’s volume, which is zero there, so they sell nothing in that second (a margin call is tried again every second; a daily stop or regime exit leaves the position to the next exit, usually the day-end close). The setting exitFill set to nextTrade makes it wait for the next second with a real trade, like any other order. If the session ends with no trade, the exit moves to the first trade of the next day in the backtest: the position is held overnight and takes the gap. If the backtest ends first, the exit fills in the last second at the last known price. The results card says which way was used and how many exits waited, how many were held overnight and how many reached the end.
Sessions: intraday or continuous
- Intraday closes every position 5 minutes (300 seconds) before the session ends, by default, and takes no new entries from that second until the next session.
- Continuous keeps positions from one day to the next. Interest on borrowed money is charged per calendar day and forex positions pay or earn swaps (see costs).
- A trading window (from, to) limits when rules may enter. Stock and ETF sessions follow the exchange calendar; forex and spot metals trade around the clock on weekdays.
Execution delay, margin and liquidation
- An execution delay of 0 to 3,600 seconds, fixed or random with a seed, makes rule orders arrive later and fill at the price and volume of that later second. The Execution delay check uses it.
- Margin call. Liquidation is on by default, with maintenance margin 25% of the gross position (longs and shorts together, every symbol of the run; a futures contract counts its margin). Every second, after the strategy has traded, the engine checks equity: below the maintenance requirement, or at zero or below, it closes at market, with the exit reason “margin liquidation”, the same share of every position, just enough for equity to cover the maintenance requirement again (share = 1 − equity ÷ (maintenance × gross position), quantities rounded up). Trading then goes on: there is no pause until the end of the day. A new entry is placed only if, after it, equity covers the maintenance requirement plus an opening buffer (and it fits the leverage you set); otherwise it is skipped. The buffer works like a broker’s initial margin: opening asks for more than holding, so a fresh entry is not margin-called by the next tick and refilled again. It is on by default at 10% of the maintenance margin (25% → 27.5% of the gross position) and can be changed or turned off in the strategy settings (“Buffer when opening a position”). There is no pattern-day-trader rule. The close is limited by the second’s volume like other forced exits, so a large position can be cut in several steps; whatever is still below the requirement is closed in the next seconds.
- Account wiped out. Whole-account equity (cash plus the open result of every position, longs and shorts, every symbol) at zero or below wipes the account out, with liquidation on or off: everything open is closed at market (shorts are bought back, with the exit reason “account wiped out” when there was no margin call), and the run goes on to the end of the range with no trading. A hedge or a pair losing on one leg does not trigger it while the whole account is above zero. A gap can leave equity below zero; the result shows that loss, it is not cut at zero. After the wipe-out no more interest on borrowed cash is charged. With “capital from the start of the period” the next period starts again from the starting capital and trades normally.
- How it is shown. A run with a margin call opens its results with a banner: the date and second, equity against the requirement and the position, the trades closed and their result, and a link to that moment on the chart; the day is marked on the equity curve and the closed trades carry the reason “margin liquidation”. A wiped-out account says so first: “You lost all your capital”, the date and time it was zeroed and that all positions were closed, with a mark at that moment on the price chart. Interest on borrowed cash (if a margin rate is set) is charged on any cash below zero at the day’s close, also after a margin call, but not after the account is wiped out.
- With liquidation switched off there is no margin call: the strategy keeps trading, limited by its buying power, until whole-account equity reaches zero, which wipes the account out as above. Prop Lab accounts have no margin call: a prop challenge ends at the firm’s daily and total loss limits, checked against equity including open positions; buying power comes from the firm’s leverage for the asset class and is counted from equity, and nothing can be bought at equity zero or below.
- Futures fill on the contract’s tick grid in whole contracts, and buying power is the margin, not the full value.