Stock Market Hedging
Prediction market positions often correlate with traditional financial assets. A trader long “Will the Fed cut rates?” can hedge with bond ETFs (TLT) or rate-sensitive stocks. Horizon’s hedge module covers ratio calculation, effectiveness tracking, multi-ticker optimization, and automated rebalancing. All math is in Rust.Overview
OLS Hedge Ratio
hz.hedge_ratio_ols() computes the hedge ratio from rolling price windows.Effectiveness & Basis Risk
hz.hedge_effectiveness() and hz.basis_risk() measure how well the hedge is working.Multi-Ticker Optimization
hz.multi_ticker_hedge() finds weights across multiple hedge instruments.Scenario Analysis
hz.hedge_scenario() shows portfolio P&L under different spot moves.Core Functions
All math runs in Rust. Every output is guarded against NaN/Inf.hz.hedge_ratio_ols
Compute the hedge ratio: h* = Cov(dS, dF) / Var(dF).
Returns 0.0 if fewer than 2 data points or zero variance in hedge returns.
hz.rolling_correlation
Rolling Pearson correlation between two price series.
Returns a list of correlation values, one per window. Empty if insufficient data.
hz.hedge_effectiveness
Hedge effectiveness: HE = 1 − Var(hedged) / Var(unhedged). Values close to 1.0 indicate a good hedge.
Returns 0.0 for empty input or zero unhedged variance.
hz.basis_risk
Residual variance after hedging. Lower is better.hz.optimal_hedge_size
Calculate optimal hedge notional with optional cap.hz.compute_hedge_sensitivities
Returns aHedgeSensitivities object with risk metrics.
hz.multi_ticker_hedge
Multi-instrument hedge optimization. Returns aMultiHedgeResult.
hz.hedge_scenario
P&L projection under a given spot move.Pipeline Functions
These functions return callables for use insidehz.run() pipelines. Call the factory to configure, get back a function you can put in the pipeline list.
hz.hedge_monitor
Computes hedge metrics each cycle. Returns a dict passed to the next pipeline stage.
Returns on each cycle:
hz.hedge_executor
Monitors hedge drift and flags rebalancing. Withauto_rebalance=True, generates OrderRequest objects for Alpaca.
hz.correlation_tracker
Track pairwise correlations across multiple feeds.hz.stock_hedge
Combineshedge_monitor + hedge_executor. Use this if you want both in one call.
Standalone Analysis
These functions run outsidehz.run() for research and one-off analysis.
hz.compute_hedge_report
Returns a full hedge report as a dictionary.hz.run_scenario
Run multiple scenarios at once:Cost Tracking
TheHedgeCostTracker tracks cumulative rebalancing costs:
Examples
Basic Hedge Ratio Calculation
Multi-Ticker Portfolio Hedge
Scenario Analysis
Hedged Pipeline Strategy
Mathematical Background
Minimum-Variance Hedge Ratio
Minimum-Variance Hedge Ratio
The OLS hedge ratio minimizes the variance of the hedged portfolio:h = Cov(dS, dF) / Var(dF)*Where dS and dF are log returns of the spot and hedge instruments. This is the slope from regressing spot returns on hedge returns. The ratio is recomputed over a rolling window so it adjusts as correlations shift.
Hedge Effectiveness
Hedge Effectiveness
Hedge effectiveness measures how much variance the hedge removes:HE = 1 − Var(hedged) / Var(unhedged)Where hedged return = spot return − h × hedge return. Values near 1.0 mean the hedge is working well. Values below 0.5 mean the hedge instrument is a poor match for the spot position.
Multi-Ticker Optimization
Multi-Ticker Optimization
For multiple hedge instruments, the weight vector is:w = Σ_FF⁻¹ × Σ_FSWhere Σ_FF is the covariance matrix of hedge returns and Σ_FS is the cross-covariance vector between hedge and spot returns. Solved via Cholesky decomposition, with fallback to diagonal solve when the matrix is near-singular.
Scenario Analysis Model
Scenario Analysis Model
P&L under a hypothetical spot move:
- Spot P&L = position × price × move
- Hedge move = correlation × (spot_vol / hedge_vol) × spot_move
- Hedge P&L = hedge_position × hedge_price × hedge_move
- Portfolio P&L = Spot P&L + Hedge P&L
- Hedge benefit = Portfolio P&L − Unhedged P&L