Stop and target inside one bar
A bar that touches both your stop and your target cannot say which came first, so a bar-based backtest has to guess. Matana Labs never decides from the bar: the engine walks the seconds inside it, one price per second, and the level the price reaches first is the one that fills.
The problem with bars
A 5-minute bar keeps four prices: open, high, low and close. If the high is above your target and the low below your stop, the bar is consistent with both orders: target first, or stop first. Bar backtesters pick a rule (often “the stop first”, sometimes “whichever is nearer the open”) and apply it to every such bar. Our study Stop loss or take profit first? How often bars can't tell measures how often this happens and how much the guess changes results.
How the engine decides
- Bars are only for indicators. Fills are decided on the second-level prices, in time order, whatever bar size the strategy uses.
- Each second has one price. A long position’s stop is below the entry and its target above, so one price cannot reach both: the first second that reaches a level decides.
- Inside a second the engine checks the trailing stop, then the stop, then the target (see the order of work). The stop and target of a new position are checked from the next second.
- What happens within a single second cannot be seen in second-level data; this is the remaining limit, one second instead of one bar.
At what price
A stop fills at the price of the second that reached it, not at the stop level: if the price stepped from above the stop to below it, the fill is the lower price. A target fills at the price of the second that reached it, at the level or better. A limit order fills at its level or better, when the second has volume left for it.
How much that matters, from our engine validation on QQQ: in “SMA cross with stop and target” every one of the 98 stop exits filled at the second’s price; filled at the stop level instead, the same trades would have shown $436.51 more net profit; in “RSI(2) mean reversion” every one of the 56 stop exits filled at the second’s price; filled at the stop level instead, the same trades would have shown $248.72 more net profit.
See it on your own trades
Open any trade of a backtest in Backtest Lab and play the day: the chart shows the seconds around the entry and the exit, with the stop and target lines. Analysis Lab compares the same strategy with fills on bars and fills by the second.