Backtesting Limit Entries
A buy limit that works for a few bars, and when an AmiBroker backtest should count it as filled
A buy limit below the market fills only if price comes down to it, and it can stay working for a while before it does. A backtest doesn't see the order book. It sees bars, so it's up to your code to decide when a bar counts as a fill.
Get that wrong and the backtest takes fills you wouldn't have had, mostly on the trades that work best. These four rules keep it honest:
- Freeze the limit at the signal. The order's price is set when it's placed and doesn't move while it works.
- Count a fill only when the low trades below the limit. A low that only touches it doesn't count.
- Fill at the open when the bar opens below the limit. You get the better price, not the limit.
- One fill per order. After it fills, the order is gone.
The example
The examples on this page come from one simple long strategy on 1-minute bars:
- Setup: a stock gapping up 3.5% or more makes a new high of the day, between 5 and 90 minutes after the open. The setup is known when that bar closes.
- Order: a buy limit 1% below the setup bar's close, working for the next 3 bars.
- Exit: at the open 10 bars after the entry.
The exit doesn't matter here. The entry does.
The order in the bar loop
The MabeKit strategy templates walk the bars in a for loop, so the order is a few variables that carry from bar to bar: its limit and how many bars it has left.
// The order's limit, set from the setup bar's close and rounded down to the cent
SetupLimitPrice = floor( C * ( 1 - LimitOffsetPct / 100 ) * 100 + 0.01 ) / 100;
order_limit = 0;
order_bars_left = 0;
for( i = 1; i < BarCount; i++ )
{
// ... exits for an open trade ...
// A working order is a day order: the new session cancels it
if( newday[i] )
order_bars_left = 0;
if( order_bars_left > 0 AND NOT in_trade )
{
// Prices are single precision, so a low within a hundredth of a cent counts as at the limit
at_limit = abs( L[i] - order_limit ) < 0.0001;
if( L[i] < order_limit AND NOT at_limit )
{
Buy[i] = 1;
// A bar that opens below the limit fills at its open
BuyPrice[i] = Min( O[i], order_limit );
in_trade = 1;
trd_entry_bar = i;
order_bars_left = 0;
}
else
order_bars_left--;
}
// A setup bar places the order for the bars after it
if( Setup[i] AND order_bars_left == 0 AND NOT in_trade )
{
order_limit = SetupLimitPrice[i];
order_bars_left = LimitBars;
}
}
Each of the four rules is in there:
- The limit is frozen.
order_limitis copied from the setup bar once and doesn't change while the order works. - The low has to trade below the limit. The fill test is
L[i] < order_limit. - A gap fills at the open. The fill price is
Min( O[i], order_limit ). - One fill per order.
order_bars_left = 0cancels the order when it fills, and a setup while an order is working or a trade is open is ignored.
The order is placed at the end of the setup bar and first checked on the bar after it, so a setup bar can never fill its own order.
Here it is on IONQ:

- 9:40: a setup bar places a buy limit at 39.53 for the next 3 bars.
- 9:42: the low is exactly 39.53. It touches the limit but doesn't trade below it, so no fill is counted. The order expires after 9:43.
- 9:43: another setup places a new limit at 39.65.
- 9:45: the low reaches 39.50, below the limit, so the order fills at 39.65.
Touching the limit isn't a fill
When the low of a bar is exactly your limit, some shares traded at your price. You don't know if any of them were yours. Orders at a price fill in the order they arrived, and a stock that turns right at your price often stops after filling only the orders ahead of you.
The trades where price touches your limit to the cent and turns are the ones you're least likely to get filled on, and they're often the best ones. A backtest that counts a touch as a fill gives you all of them.
Here are two of them on BBAI:

Both limits, 4.40 and then 4.41, were touched to the cent and never traded below. The stock was higher within minutes both times.
I ran the example on 30 volatile stocks from January through October 2026, once each way:
| Fill rule | Trades | Profit |
|---|---|---|
| Low trades below the limit | 94 | $3,606 |
| Low touches the limit | 103 | $4,467 |
The touch rule adds 15 fills that the stricter rule never takes, worth $937 together. On 6 of them the price never went below the entry, and all 6 were winners. A few others drop out because an earlier fill took their place.
That's the bias in the touch rule: it adds trades you might not get, and those trades are better than average. Use the stricter rule. It will miss a few fills you would have had live, and that's the safe direction to be wrong in.
In the episode on backtests that are too good to be true, Michael says limits "are gonna be okay, unless the rare time that the limit goes right to your thing to the tick and then reverses." The stricter rule is how a backtest handles that rare time.
When a bar opens below the limit
If a bar opens below your limit, the order fills at the open, which is a better price than the limit. Min( O[i], order_limit ) takes care of it.
This hardly ever happens with 1-minute bars and an order that works for a few minutes: in the example it didn't happen once. It matters when an order is left working across a session's open (a swing order on daily bars, for example), or through a trading halt.
Writing it with Hold, as AmiBroker's knowledge base does
AmiBroker's knowledge base article Handling limit orders in the backtester writes the same order with arrays instead of a loop. Its multi-bar version keeps the buy signal alive with Hold:
BuyLimitPrice = ValueWhen( BuySignal, Close ) * 0.99;
Buy = Ref( BuySignal, -1 );
Buy = Hold( Buy, 3 ) AND L < BuyLimitPrice;
BuyPrice = Min( Open, BuyLimitPrice );
It uses the same trade-through test and the same open price as the loop above, and in the article's example it's fine. When I put it on my example's setup, two problems came up.
The limit can come from the bar it fills on. ValueWhen( BuySignal, ... ) includes the current bar. If a new signal comes on a bar while the order is working, that bar's limit is computed from its own close and tested against its own low. The bar dips 1% and then closes at a new high, and the backtest buys the dip at a price you couldn't have known until the bar was over.
The article's signal, a cross of a 100-bar moving average, almost never repeats within 3 bars, so it doesn't show up there. A new high of the day repeats all the time. In my example the Hold version made $8,346 on 145 trades, against $3,606 for the loop, and 37 of those trades were bought on a setup bar. Taking the limit from the bars before fixes it:
BuyLimitPrice = Ref( ValueWhen( Setup, SetupLimitPrice ), -1 );
With that change the Hold version made $3,979 on 132 trades. It still has more trades than the loop because it places orders the loop doesn't: a new setup replaces the working order instead of being ignored, and a setup during a trade still places one. Either way is a reasonable choice, as long as the backtest matches what you'll do live.
One order can fill more than once. Hold keeps Buy true on every bar of the window where the low is below the limit. The backtester ignores a buy while you're in a trade, but if the trade exits while the window is still open, the next qualifying bar buys again. With an exit one bar after the entry, 96 of the Hold version's 248 trades were a second or third fill of the same order. A real order fills once.
The array version is fine when the signal can't repeat while the order works and no trade can exit before the order would have expired. Otherwise use the loop: it handles each order explicitly, so these cases can't happen.
How long to leave it working
How many bars the order should work is part of the strategy, so test it like any other parameter. A short window misses the slow pullbacks. A long one fills on more of the trades that come back to your price and keep going down to your stop.
The episode on missing a trade is about this choice live. If price comes back to your entry later, you're "guaranteed to get all the losers" that were always going to come back, while the winners that never came back are gone.
Two things are worth trying in the backtest:
- A lower limit. Test it against the entry you have now, and look at both how many orders go unfilled and how much better the price is on the ones that fill. That's from the episode on advanced metrics.
- A conservative limit live. A limit close to your entry price will miss some trades, but the missed trades make any gap between the backtest and your live results obvious, where a market order hides it in slippage.
What the backtest can't capture
- Your place in line. A bar that trades a little below your limit may still not have filled all of your shares. Big orders in thin stocks need more than a cent of room.
- Partial fills. The backtest fills the whole order or none of it.
- Cash for every order. In a portfolio backtest, a working limit order ties up buying power whether it fills or not. If you can't afford to place an order for every setup, the backtest needs to choose which ones get one, as the knowledge base article's ranked example does.