Skip to content

Engle Granger Residual

Family: Stat-arb

What it computes

Emits EngleGrangerResidualTick ticks carrying hedge_ratio, gamma_intercept, residual_current, residual_zscore off daily-close cache (2 symbols).

Available on the live and replay drives. The historical drive fails closed with KAIROS_ERR_HISTORICAL_UNWIRED until the relevant cache is hydrated externally.

Methodology

Engle-Granger (1987) Stage-1 long-run OLS residual stream ε_t = log(price_a) − γ̂ − δ̂ · log(price_b) with per-tick z-score — Vidyamurthy (2004) pair-trading entry signal.

See the methodology overview for the citation index.

Inputs

Daily-close cache (2 symbols).

Key outputs

Hedge_ratio, gamma_intercept, residual_current, residual_zscore. The full field set is in the tick table below.

Output schema (EngleGrangerResidualTick)

The field / type / description table below is regenerated from the EngleGrangerResidualTick Rust source by docs-site/scripts/inject-doc-tables.ts on every npm run docs:build. Do not hand-edit between the sentinels.

FieldTypeDescription
symbol_aArc<str>LHS leg of the regression (interned).
symbol_bArc<str>RHS comparator leg of the regression (interned).
datei32Trading-session date (YYYYMMDD) at emission time.
ms_of_dayi32Milliseconds since midnight ET at emission time. 0 for watermark-driven boot snapshots before any live tick.
hedge_ratiof64Long-run OLS slope δ̂ — the per-share hedge ratio (short δ̂ shares of B per long share of A so the spread is mean-reverting under cointegration).
gamma_interceptf64Long-run OLS intercept γ̂ — the equilibrium log-price differential.
residual_currentf64Latest in-sample residual ε_T = log(price_a)_T − γ̂ − δ̂ · log(price_b)_T — the per-tick spread reading every pair-trading entry signal fires off.
residual_meanf64In-sample residual mean (1 / N) · Σ ε_t over the trailing window — the centring constant the z-score subtracts.
residual_stddevf64In-sample residual standard deviation σ_ε = sqrt((1 / N) · Σ (ε_t − mean(ε))²) — the scaling constant the z-score divides by.
residual_zscoref64Standardised residual z = (residual_current − mean(ε)) / σ_ε — the institutional entry signal: `
sessions_usedi32Count of paired log-price observations the regression consumed.
lookback_sessionsi32The configured trailing-window length, echoed for audit.

Configuration (EngleGrangerResidualParams)

Regenerated from the EngleGrangerResidualParams Rust source — see the note above.

FieldTypeDescription
contractsSecurityFilterContracts the subscription tracks — the per-contract [SecurityFilter] resolves symbol_a (the LHS regression leg).
conditionsConditionPolicyTrade-condition admission policy applied to every print.
venuesExchangeFilterExchange / venue admission policy.
daily_closeArc<DailyCloseCache>Boot-time hydrated daily-close cache. Must hold at least lookback_sessions rows for BOTH symbol_a AND symbol_b for the regression window to populate fully.
symbol_bArc<str>Fixed comparator leg — symbol_b on the regression log(price_a) = γ + δ · log(price_b) + ε.
lookback_sessionsusizeTrailing-window length in trading sessions. Defaults to [DEFAULT_LOOKBACK_SESSIONS] (252).
min_emit_interval_msi32Minimum interval between emissions per pair, in milliseconds. Defaults to [DEFAULT_MIN_EMIT_INTERVAL_MS] (60_000 ms).

Example

Python

python
import kairos_thetadata as kt

client = kt.Client.connect(kt.Credentials.from_env())

def on_event(row):
    print(row)

sub = client.live().engle_granger_residual(["QQQ"]).on_event(on_event)
sub.wait(timeout_seconds=60.0)

TypeScript

typescript
import { Client, Credentials } from "kairos-thetadata";

const client = await Client.connect(Credentials.fromEnv());

await client
  .live()
  .engleGrangerResidual(["QQQ"])
  .onEvent((tick) => {
    console.log(tick);
  });

Rust

rust
// Cargo.toml:
//   kairos = "0.1"

use kairos::{Client, EngleGrangerResidualRow};

# fn run() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::connect(("me@example.com", "secret"))?;

let sub = client
    .live()
    .engle_granger_residual(["QQQ"])
    .on_event(|row: &EngleGrangerResidualRow| {
        println!("residual_zscore={} hedge_ratio={}", row.residual_zscore, row.hedge_ratio)
    })?;
// ... later ...
sub.unsubscribe();
# Ok(())
# }

Proprietary. All rights reserved.