Appearance

Personalize your experience

Mode
Accent Color
Direction
QuaScript

Cointegration Reversion Monitor

Admin's Choice

Engle-Granger cointegration spread tracker with stationary z-score reversion corridors

StatisticsOwn pane WebGL CanvasHybridDashboardSignals
quascriptstatisticscointegrationspreadpairszscore
Cointegration Reversion Monitorengine 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)
FreeOpen source

Favourites and Admin's Choice analyses appear in the terminal's indicators window under Analyses.

Language
QuaScript
Engine
Native C++, incremental on live ticks
Rendering
WebGL + Canvas
Placement
Own pane
Settings
4
Plotted series
7
Lines of code
52
Source
presets/analysis/stats/cointegration_reversion_monitor.qs

About

A rolling Engle-Granger test between price and its volume-weighted fair value. It answers the question a z-score cannot: is this spread actually mean-reverting right now? The area is the residual z-score; it is drawn in colour only while the Dickey-Fuller statistic of the residual rejects a unit root, and grey when it does not. A stretched spread in a cointegrated window is a reversion setup with a measured half-life; the same stretch in a grey window is a trend leaving fair value behind. To monitor a true pair, replace the anchor with a feed of the second instrument.

7
Plotted series
3
Signal marks
1
Drawings
2
Signal rules
4
Settings
39
Lines of code

What it draws

Own pane · counted from the engine's plot table and the script's draw calls

Series

  • Level4
    Cointegration thresholdRichCheapFair
  • Line2
    SpreadDickey-Fuller statistic
  • Area1
    Residual z-score
  • Horizontal levels4
Legend
Residual z-score Spread Dickey-Fuller statistic Cointegration threshold Rich Cheap

Signal marks

Rich spread turning back while cointegratedCheap spread turning back while cointegratedCointegration lost with the spread stretched

Drawings and chart effects

  • Dashboard tables1 created

How it runs

From source to the live chart, for this script

  1. 1
    Compile

    The QuaScript compiler checks the 52-line script (39 lines of code) and lowers it for the native C++ engine. Compile errors point at the exact line and column.

  2. 2
    Place

    The header sets pane = hpane with height = 0.26 (26% of the chart), so the output gets its own pane under the price with its own scale.

  3. 3
    Compute

    The script runs once per bar, oldest to newest. 1 persist variable keeps state (objects, counters) across bars.

  4. 4
    Stay live

    On the terminal the script keeps one live engine session: a tick rolls the forming bar back to its checkpoint and re-executes only that bar, and new bars are appended, so history is never rerun while the market moves. The live result is identical to a full run.

  5. 5
    Render

    The results are handed to the chart: series on WebGL, drawings on the Canvas 2D QuaScript layer.

  6. 6
    Tune

    4 settings in 3 groups appear in the chart's settings dialog. Changing one reruns the engine with the new value.

Rendering

Hybrid: this script draws on 2 surfaces at once

WebGPUNot used

The browser's modern GPU API. The chart uploads every line, band and mark of the script as GPU buffers and draws them in one pass, so dense heatmaps and dozens of series stay smooth while you pan and zoom. Without WebGPU support the chart's GPU layer falls back to WebGL 2, then Canvas 2D.

WebGLSeries renderer

GPU drawing through WebGL 2, available in every current browser. Used for chart-native profiles and for series that ask for it with backend = "webgl". Fast for large numbers of bars and wide fills.

In this script
  • • 7 plotted series
  • • 4 levels
  • • 3 signal mark types

Chosen by render = webgl in the header.

CanvasUsed

The browser's 2D drawing API. Text-heavy and object-style output (dashboard tables, zones, tags, segments), candle colors and backgrounds are painted by the QuaScript layer, a Canvas 2D surface over the chart. Series use Canvas when the script's header does not ask for a GPU.

In this script
  • • 1 dashboard tables (QuaScript layer)

Signals

2 signal rules declared with signal.rule

Cointegrated reversion

A stretched spread turned back while the cointegration test holds

Cointegration broken

The spread is stretched and no longer mean-reverting

Settings

4 settings, grouped as in the chart's settings dialog

Pair
SettingTypeDefaultRange / optionsId
Fair-value lengthInteger2110 – 500fair_value_length
Test
SettingTypeDefaultRange / optionsId
Test windowInteger25040 – 1000test_window
Critical value (5%)Number-3.37-6 – -1.5, step 0.01critical_value_5
Signals
SettingTypeDefaultRange / optionsId
Entry zNumber20.5 – 5, step 0.1entry_z

Script

packages/quascript/presets/analysis/stats/cointegration_reversion_monitor.qs

Header

analysis "Cointegration Reversion Monitor" {
    pane = hpane
    height = 0.26
    render = webgl
    short_title = COINT
    precision = 2
}
Total lines
52
Code lines
39
Comment lines
7
Helper functions
0
Loops
0
Persistent variables
1
Higher-interval feeds
0
Engine plots
7

Built with

Technical analysis (ta.*)
ta.vwma
Statistics (stat.*)
stat.cointegration
Math (math.*)
math.absmath.is_na
Imported modules
viz/premium

Discussion

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