Appearance

Personalize your experience

Mode
Accent Color
Direction
MarketplacestrategiesBook Gravity Reversion
QuaScript

Book Gravity Reversion

Admin's ChoiceFree

Fades stretches from the book's average centre of gravity (order book bars only)

0.0(0)
0 installs 0 0 comments
Q
433 listings

The QuaTick team's QuaScript analyses, run by the native C++ engine.

View Profile
quascriptstrategyorder-bookmean-reversion
Free
Backtest this strategy Open in QuaCode Edit in IDE
Version
Updated
Compatible

Live engine preview

QuaScriptQuaScript

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.

Book Gravity Reversionengine result
Loading the engine result…
quatick-charts · QuaScript engine on synthetic 1-minute bars · drag to pan · scroll to zoom · Run executes the script on the engine (⌘/Ctrl+Enter while editing)

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
      quascript
      1strategy "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.
      14levels = param.int(10, title = "Levels per side", min = 1, max = 200)
      15dist = param.float(1.0, title = "Stretch (ATR)", min = 0.1, max = 10, step = 0.1)
      16stopk = param.float(1.5, title = "Stop (ATR)", min = 0.2, max = 10, step = 0.1)
      17bv = book.vwap(book.bid_side, levels)
      18av = book.vwap(book.ask_side, levels)
      19bq = book.bid_volume(levels)
      20aq = book.ask_volume(levels)
      21g_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
      23g = ta.ema(g_raw, 20)
      24a = ta.atr(14)
      25imb = book.imbalance(levels)
      26flat = position.size == 0
      27long_sig = not math.is_na(a) and not math.is_na(bv) and close < g - dist * a and imb > 0
      28short_sig = not math.is_na(a) and not math.is_na(bv) and close > g + dist * a and imb < 0
      29if flat and long_sig {
      30 order.market("L", side.long)
      31 exit.bracket("L exit", entry = "L", stop = close - stopk * a, target = g)
      32}
      33if flat and short_sig {
      34 order.market("S", side.short)
      35 exit.bracket("S exit", entry = "S", stop = close + stopk * a, target = g)
      36}
      37risk.max_drawdown(25)
      38render.line(g, "Centre of gravity", color = color.violet.300, width = 2)
      39 
      Read-only preview. Open in QuaCode to edit and deploy.

      Reviews

      No reviews yet. Be the first to review this listing.

      Discussion

      Sign in to join the discussion. Your posts and replies show up on your profile.