Appearance
Personalize your experience
Book Gravity Reversion
Admin's ChoiceFreeFades stretches from the book's average centre of gravity (order book bars only)
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
Fades stretches away from the order book's average centre of gravity (20-bar EMA): when the close is more than the threshold (in ATR) below the size-weighted centre of the top levels and the book still leans to the bids, buy for a return to the centre; mirror for shorts. Stop at a multiple of ATR. Needs the order book (L2): trades only on bars with a recorded book.
Settings
Levels per side: 10
Stretch (ATR): 1
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
Source Code
Edit in IDE| 1 | strategy "Book Gravity Reversion" { |
| 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 | /// Fades stretches away from the order book's average centre of gravity (20-bar EMA): when the close is more than the |
| 11 | /// threshold (in ATR) below the size-weighted centre of the top levels and the book still leans to |
| 12 | /// the bids, buy for a return to the centre; mirror for shorts. Stop at a multiple of ATR. |
| 13 | /// Needs the order book (L2): trades only on bars with a recorded book. |
| 14 | levels = param.int(10, title = "Levels per side", min = 1, max = 200) |
| 15 | dist = param.float(1.0, title = "Stretch (ATR)", min = 0.1, max = 10, step = 0.1) |
| 16 | stopk = param.float(1.5, title = "Stop (ATR)", min = 0.2, max = 10, step = 0.1) |
| 17 | bv = book.vwap(book.bid_side, levels) |
| 18 | av = book.vwap(book.ask_side, levels) |
| 19 | bq = book.bid_volume(levels) |
| 20 | aq = book.ask_volume(levels) |
| 21 | g_raw = (bv * bq + av * aq) / math.max(bq + aq, 1e-12) |
| 22 | // the gravity of one bar sits at the price; its average over recent bars is the level price returns to |
| 23 | g = ta.ema(g_raw, 20) |
| 24 | a = ta.atr(14) |
| 25 | imb = book.imbalance(levels) |
| 26 | flat = position.size == 0 |
| 27 | long_sig = not math.is_na(a) and not math.is_na(bv) and close < g - dist * a and imb > 0 |
| 28 | short_sig = not math.is_na(a) and not math.is_na(bv) and close > g + dist * a and imb < 0 |
| 29 | if flat and long_sig { |
| 30 | order.market("L", side.long) |
| 31 | exit.bracket("L exit", entry = "L", stop = close - stopk * a, target = g) |
| 32 | } |
| 33 | if flat and short_sig { |
| 34 | order.market("S", side.short) |
| 35 | exit.bracket("S exit", entry = "S", stop = close + stopk * a, target = g) |
| 36 | } |
| 37 | risk.max_drawdown(25) |
| 38 | render.line(g, "Centre of gravity", color = color.violet.300, width = 2) |
| 39 |
Reviews
No reviews yet. Be the first to review this listing.