Examples
Strategy: join a defended level, with queue fills
An order-by-order strategy. When sellers hit the best bid and that bid has been refilled several times in the last seconds, it joins the bid with a limit order. With fill_model: queue, the simulated order only fills once the size that was ahead of it in the queue has traded or been canceled.
strategy "Join the defended bid" { overlay: true, mbo_from: rth, level_history: 2m, fill_model: queue, latency: 60ms, costs: { commission: 2.5 usd, slippage: 1 tick } }
win = input.duration("Window", 20s)
minRefills = input.int("Min refills", 3, min: 1)
stopTicks = input.int("Stop (ticks)", 6, min: 1)
targetTicks = input.int("Target (ticks)", 6, min: 1)
on match(m) {
if position.flat and position.pending == 0 and mbo.available and m.side == "sell" and m.price == mbo.best_bid {
lv = mbo.level(m.price, window: win)
if lv.replenishments >= minRefills and lv.displayed > 0 {
enter("buy", type: "limit", price: m.price, stop: price(stopTicks), target: price(targetTicks), expire: 30s, tag: "defended bid")
}
}
}
on fill(f) {
label.new(f.time, f.price, "in " + str(f.size), color: #2962ff)
}
on exit(x) {
label.new(x.time, x.price, x.reason + " " + str(x.ticks, "+0.0") + "t", color: x.usd > 0 ? #22c55e : #ef4444)
}What to notice:
mbo_from: rthstarts the order-by-order data at the latest 9:30 New York before launch, a fixed start;- the decision is made at the time of the execution (
on match), not at the bar's close; - in a long backtest over many days, this script runs on each day's MBO replay file with the same rules.