Appearance
Personalize your experience
Book Imbalance Breakout
Admin's ChoiceFreeDonchian breakouts taken only when the order book leans the same way
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
Range breakouts that the order book agrees with. A Donchian breakout from OHLCV is taken only if the resting book leans the same way at the breakout bar (distance-weighted pressure beyond the threshold) and order-flow imbalance between the last books confirms it, so breakouts into a wall of opposing orders are skipped. Bars without a recorded book never trade (no guessing). Exit: ATR stop and a trailing exit at the opposite Donchian half-channel. Needs the order book (L2): backtests run only where the chart has recorded books.
Settings
Breakout channel: 30
Book pressure threshold: 0.15
Pressure decay (%): 0.25
Stop (ATR): 2
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
- ▸ ATR stop and a trailing exit at the opposite Donchian half-channel
- ▸ Needs the order book (L2): backtests run only where the chart has recorded books
Source Code
Edit in IDE| 1 | strategy "Book Imbalance Breakout" { |
| 2 | overlay = true |
| 3 | capital = 100000 |
| 4 | commission = 0.05 |
| 5 | slippage = 1 |
| 6 | sizing = risk_percent |
| 7 | size = 1 |
| 8 | max_leverage = 1 |
| 9 | } |
| 10 | /// Range breakouts that the order book agrees with. A Donchian breakout from OHLCV is taken only if |
| 11 | /// the resting book leans the same way at the breakout bar (distance-weighted pressure beyond the |
| 12 | /// threshold) and order-flow imbalance between the last books confirms it, so breakouts into a wall |
| 13 | /// of opposing orders are skipped. Bars without a recorded book never trade (no guessing). |
| 14 | /// Exit: ATR stop and a trailing exit at the opposite Donchian half-channel. |
| 15 | /// Needs the order book (L2): backtests run only where the chart has recorded books. |
| 16 | use "viz/premium" as pr |
| 17 | len = param.int(30, title = "Breakout channel", min = 5, max = 500, group = "Breakout") |
| 18 | thr = param.float(0.15, title = "Book pressure threshold", min = 0.01, max = 0.9, step = 0.01, group = "Book") |
| 19 | decay = param.float(0.25, title = "Pressure decay (%)", min = 0.02, max = 2, step = 0.01, group = "Book") |
| 20 | stop_k = param.float(2.0, title = "Stop (ATR)", min = 0.5, max = 10, step = 0.1, group = "Exit") |
| 21 | (mid, upper, lower) = ta.donchian(len) |
| 22 | (_, up_half, dn_half) = ta.donchian(len / 2) |
| 23 | pres = book.pressure(decay) |
| 24 | ofi = orderbook.ofi(5) |
| 25 | a = ta.atr(14) |
| 26 | break_up = close > upper[1] |
| 27 | break_dn = close < lower[1] |
| 28 | long_ok = break_up and pres > thr and math.coalesce(ofi, 0) >= 0 |
| 29 | short_ok = break_dn and pres < -thr and math.coalesce(ofi, 0) <= 0 |
| 30 | flat = position.size == 0 |
| 31 | if flat and long_ok { |
| 32 | order.market("L", side.long) |
| 33 | exit.bracket("L stop", entry = "L", stop = close - stop_k * a) |
| 34 | } |
| 35 | if flat and short_ok { |
| 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 close < dn_half[1] { exit.close_all() } |
| 40 | if position.size < 0 and close > up_half[1] { exit.close_all() } |
| 41 | risk.max_drawdown(25) |
| 42 | pu = render.line(upper, "Upper", color = color.translucent(color.emerald.300, 40), width = 1) |
| 43 | pd = render.line(lower, "Lower", color = color.translucent(color.rose.300, 40), width = 1) |
| 44 | render.band(pu, pd, color = pres > thr ? color.emerald.500 : pres < -thr ? color.rose.500 : color.slate.600, opacity = 0.06, title = "Range (book lean)") |
| 45 | render.mark(break_up and not long_ok, shape = shape.x, at = anchor.above_bar, color = color.amber.300, size = 0.5, title = "Breakout rejected by book") |
| 46 | render.mark(break_dn and not short_ok, shape = shape.x, at = anchor.below_bar, color = color.amber.300, size = 0.5, title = "Breakdown rejected by book") |
| 47 | render.mark(long_ok and flat, shape = shape.triangle_up, at = anchor.below_bar, color = color.emerald.300, size = 0.8, title = "Book-confirmed breakout") |
| 48 | render.mark(short_ok and flat, shape = shape.triangle_down, at = anchor.above_bar, color = color.rose.300, size = 0.8, title = "Book-confirmed breakdown") |
| 49 |
Reviews
No reviews yet. Be the first to review this listing.