Whichever you will use live, because they test different things. A market order always fills and pays the spread and slippage. A limit order saves that cost but fills only when price comes to it, and misses the trades that run away. A backtest of limit entries is honest only if a limit fills when price trades through it, and the trades it missed stay missed.
What each order costs
| Market | Limit | |
|---|---|---|
| Fills | Always, at the next available price | Only if price reaches the limit |
| Price | Ask to buy, bid to sell, plus slippage | The limit or better |
| Hidden cost | Spread and slippage on every trade | Missed trades, often the best ones |
The hidden cost of limits is adverse selection: a buy limit below the market fills when price is falling toward it, and misses when price rises away. The fills you get are tilted toward trades that started badly.
Three ways limit backtests go wrong
- Filling on a touch. A limit at exactly the bar's low is filled in many backtests. Live, there were other orders ahead of yours at that price, and a touch often does not fill. Require price to trade through the limit, at least by the spread.
- Filling at the limit when price gapped through it. A buy limit at $95 when the stock opens at $88 fills near $88. See gaps and overnight fills.
- Leaving the limit open forever. A day-old limit that fills a week later is a different trade. Give it an expiry.
Stops are market orders
A stop becomes a market order when price trades through it, so it pays the spread and slippage and can fill well beyond its price. Bracket take-profits are limits.
How Reamer Research handles it
In Reamer Research, a buy limit fills when the simulated ask reaches it, and a sell limit when the bid does, so with a spread set a limit has to be traded through, and a touch of the bar's low is not enough. tif='GTD' with an expiry cancels it if it does not fill in time. This object runs the same signal with either order type:
import reamer_research as rr
class PullbackEntry:
def __init__(self, use_limit, offset=0.10, bars_alive=3, qty=10):
self.use_limit, self.offset, self.bars_alive, self.qty = use_limit, offset, bars_alive, qty
self.lookback = 25
def on_bar(self, d):
if d.close[0] == 0:
return None
trend = d.close[-1] > d.close.mean()
if d.position() > 0 and not trend:
return rr.close_position()
if d.position() != 0 or not trend or d.open_orders():
return None
if not self.use_limit:
return rr.buy_market(self.qty)
expiry = int(d.ts) + self.bars_alive * 300 # 5-minute bars
return rr.buy_limit(d.close[-1] - self.offset, self.qty,
tif='GTD', expiry_timestamp=expiry)
costs = dict(spread=0.02, slippage=0.005)
for use_limit in (False, True):
r = rr.backtest(PullbackEntry(use_limit), 'data/SPY_5min.bin', **costs)
print('limit ' if use_limit else 'market', r.closed_trades_count, 'trades,',
r.expired_orders, 'expired,', round(r.net_pnl, 2), 'net')
The report counts expired orders, so the trades the limit missed are visible beside the cost it saved. The visual trade diagnostics tool (open_viewer=True) draws resting orders from the order log, so each limit is shown where it waited and whether it filled. The same rule works as a function or a class with class attributes; the object form here lets one run compare both order types.
Limits to know
- Queue position is modelled through the spread. The engine has no order book, so a limit fills once the bid or ask crosses it; with a zero spread that is a touch of the price. Set a realistic
spreadfor limit strategies. - Evaluation Mode runs 30 days or 50,000 bars of one ticker. Enough to compare order types on your own entries.
A $20 month includes the full kit, so you can test your own entries both ways and cancel if it does not fit.