Appearance
Personalize your experience
Microprice Momentum
FreeIntraday: microprice lean + order-flow imbalance on a cheap spread
Live engine preview
The QuaScript engine's own output on the chart the terminal uses. Change the settings or edit the code and press Run to recompute it.
About
A short-horizon strategy for intraday charts. It goes with the side the book is about to give way on: the microprice leaning beyond a fraction of the spread, cumulative order-flow imbalance of the top levels positive (negative) over the last bars, and the bar itself closing in its upper (lower) half on rising volume. Trades only when the spread is cheap against the bar range, so the edge is not eaten by crossing the spread. Exit: after a few bars, on an opposite signal, or on an ATR stop. Needs the order book (L1 for the microprice, L2 for order-flow imbalance).
Settings
Microprice edge (spreads): 0.15
OFI bars: 5
Max spread (x ATR): 0.1
Bars held: 5
Stop (ATR): 1.5
Backtest it yourself
Open it on the backtesting page to run it on any symbol, interval and date range, then use Monte Carlo, optimisation, walk-forward and robustness tests.
Strategy Rules
Entry Conditions
Exit Conditions
- ▸ after a few bars, on an opposite signal, or on an ATR stop
- ▸ Needs the order book (L1 for the microprice, L2 for order-flow imbalance)
Source Code
Edit in IDE| 1 | strategy "Microprice Momentum" { |
| 2 | overlay = true |
| 3 | capital = 100000 |
| 4 | commission = 0.02 |
| 5 | slippage = 1 |
| 6 | sizing = percent_equity |
| 7 | size = 20 |
| 8 | max_leverage = 1 |
| 9 | } |
| 10 | /// A short-horizon strategy for intraday charts. It goes with the side the book is about to give |
| 11 | /// way on: the microprice leaning beyond a fraction of the spread, cumulative order-flow imbalance |
| 12 | /// of the top levels positive (negative) over the last bars, and the bar itself closing in its |
| 13 | /// upper (lower) half on rising volume. Trades only when the spread is cheap against the bar range, |
| 14 | /// so the edge is not eaten by crossing the spread. |
| 15 | /// Exit: after a few bars, on an opposite signal, or on an ATR stop. |
| 16 | /// Needs the order book (L1 for the microprice, L2 for order-flow imbalance). |
| 17 | edge_min = param.float(0.15, title = "Microprice edge (spreads)", min = 0.02, max = 0.5, step = 0.01, group = "Book") |
| 18 | ofi_len = param.int(5, title = "OFI bars", min = 1, max = 100, group = "Book") |
| 19 | max_sp = param.float(0.1, title = "Max spread (x ATR)", min = 0.005, max = 1, step = 0.005, group = "Book") |
| 20 | hold = param.int(5, title = "Bars held", min = 1, max = 100, group = "Exit") |
| 21 | stop_k = param.float(1.5, title = "Stop (ATR)", min = 0.2, max = 10, step = 0.1, group = "Exit") |
| 22 | a = ta.atr(14) |
| 23 | edge = (book.microprice - book.mid) / math.max(book.spread, 1e-12) |
| 24 | ofi_sum = stat.sum(math.coalesce(orderbook.ofi(5), 0), ofi_len) |
| 25 | clv = ((close - low) - (high - close)) / math.max(high - low, 1e-12) |
| 26 | cheap = book.spread < max_sp * a |
| 27 | vol_up = volume > ta.sma(volume, 20) |
| 28 | long_sig = cheap and edge > edge_min and ofi_sum > 0 and clv > 0 and vol_up |
| 29 | short_sig = cheap and edge < -edge_min and ofi_sum < 0 and clv < 0 and vol_up |
| 30 | flat = position.size == 0 |
| 31 | if flat and long_sig { |
| 32 | order.market("L", side.long) |
| 33 | exit.bracket("L stop", entry = "L", stop = close - stop_k * a) |
| 34 | } |
| 35 | if flat and short_sig { |
| 36 | order.market("S", side.short) |
| 37 | exit.bracket("S stop", entry = "S", stop = close + stop_k * a) |
| 38 | } |
| 39 | if (position.size > 0 and (short_sig or position.bars_held >= hold)) or (position.size < 0 and (long_sig or position.bars_held >= hold)) { exit.close_all() } |
| 40 | risk.max_drawdown(20) |
| 41 | render.line(book.microprice, "Microprice", color = color.translucent(color.cyan.300, 30), width = 1) |
| 42 | render.mark(long_sig and flat, shape = shape.triangle_up, at = anchor.below_bar, color = color.cyan.300, size = 0.6, title = "Book momentum long") |
| 43 | render.mark(short_sig and flat, shape = shape.triangle_down, at = anchor.above_bar, color = color.pink.300, size = 0.6, title = "Book momentum short") |
| 44 |
Reviews
No reviews yet. Be the first to review this listing.