PROTECTED SOURCE SCRIPT
Donninger HMM Risk Proxy

// Donninger HMM Risk Proxy for TradingView (Pine Script v6)
// ---------------------------------------------------------
// This script implements a **practical approximation** of the Donninger (2017) two‑state
// Hidden‑Markov‑Model (HMM) market risk classification that used:
// • Daily absolute return of the protected equity ETF (scaled annualized).
// • VIX futures term structure (VX2 − VX1) as a contango/backwardation signal.
// In the paper, the "risky" state shows both higher realized volatility *and* VIX futures
// backwardation; transitions between states tend to be abrupt, and thresholds such as 0.95,
// 0.90, and 0.70 were used to scale hedging / switching actions (VIX futures hedge, switch to
// Treasuries, reduce leverage). This Pine version **does not run a full Baum‑Welch EM** each bar;
// instead it derives a continuous "risk probability" score from normalized features designed to
// behave similarly to the paper's risky‑state probability. Parameters are exposed so you can tune
// to historical data or paste in coefficients estimated offline.
//
// WHAT THIS SCRIPT DOES
// ---------------------
// 1. Pulls daily data for a base equity ETF (default SPY) and VIX front & second month futures.
// 2. Computes annualized daily absolute return of the base ETF (Donninger dimension #1 proxy).
// 3. Computes VIX term structure spread VX2 − VX1 (Donninger dimension #2 proxy).
// 4. Normalizes both over a user‑defined trailing window (default 504 trading days ≈ 2y).
// 5. Forms a weighted linear risk score where high vol & negative term structure raise risk.
// 6. Maps score through logistic to get 0‑1 "risk probability".
// 7. Visual encodings + alert conditions keyed to Donninger‑style thresholds (0.70/0.90/0.95).
//
// EXTENSIONS YOU CAN ADD (ask me!)
// ---------------------------------
// • Offline EM fit of a 2‑state Gaussian HMM -> paste µ/σ & transition matrix into inputs; run
// forward filter in Pine for exact state probabilities.
// • Synthetic multi‑asset backtest approximating: (a) VIX futures tail hedge, (b) switch to IEF/TLT,
// (c) levered base+Ultra combos by regime. Pine cannot place orders in multiple symbols, but we
// can compute a synthetic equity curve.
// • Alternative or blended term‑structure measures: %spread, roll yield, or contango ratio.
// • Intraday adaptation: compute features on higher‑freq bars but re‑sample to daily for state.
//
// DISCLAIMER
// ----------
// This is an educational approximation. Real VIX futures hedging requires futures position sizing,
// contract specification & slippage modeling not natively supported in Pine strategies. Validate
// everything on exported data before risking capital.
// ---------------------------------------------------------
// This script implements a **practical approximation** of the Donninger (2017) two‑state
// Hidden‑Markov‑Model (HMM) market risk classification that used:
// • Daily absolute return of the protected equity ETF (scaled annualized).
// • VIX futures term structure (VX2 − VX1) as a contango/backwardation signal.
// In the paper, the "risky" state shows both higher realized volatility *and* VIX futures
// backwardation; transitions between states tend to be abrupt, and thresholds such as 0.95,
// 0.90, and 0.70 were used to scale hedging / switching actions (VIX futures hedge, switch to
// Treasuries, reduce leverage). This Pine version **does not run a full Baum‑Welch EM** each bar;
// instead it derives a continuous "risk probability" score from normalized features designed to
// behave similarly to the paper's risky‑state probability. Parameters are exposed so you can tune
// to historical data or paste in coefficients estimated offline.
//
// WHAT THIS SCRIPT DOES
// ---------------------
// 1. Pulls daily data for a base equity ETF (default SPY) and VIX front & second month futures.
// 2. Computes annualized daily absolute return of the base ETF (Donninger dimension #1 proxy).
// 3. Computes VIX term structure spread VX2 − VX1 (Donninger dimension #2 proxy).
// 4. Normalizes both over a user‑defined trailing window (default 504 trading days ≈ 2y).
// 5. Forms a weighted linear risk score where high vol & negative term structure raise risk.
// 6. Maps score through logistic to get 0‑1 "risk probability".
// 7. Visual encodings + alert conditions keyed to Donninger‑style thresholds (0.70/0.90/0.95).
//
// EXTENSIONS YOU CAN ADD (ask me!)
// ---------------------------------
// • Offline EM fit of a 2‑state Gaussian HMM -> paste µ/σ & transition matrix into inputs; run
// forward filter in Pine for exact state probabilities.
// • Synthetic multi‑asset backtest approximating: (a) VIX futures tail hedge, (b) switch to IEF/TLT,
// (c) levered base+Ultra combos by regime. Pine cannot place orders in multiple symbols, but we
// can compute a synthetic equity curve.
// • Alternative or blended term‑structure measures: %spread, roll yield, or contango ratio.
// • Intraday adaptation: compute features on higher‑freq bars but re‑sample to daily for state.
//
// DISCLAIMER
// ----------
// This is an educational approximation. Real VIX futures hedging requires futures position sizing,
// contract specification & slippage modeling not natively supported in Pine strategies. Validate
// everything on exported data before risking capital.
Script protetto
Questo script è pubblicato come codice protetto. Tuttavia, è possibile utilizzarlo liberamente e senza alcuna limitazione – per saperne di più clicca qui.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.
Script protetto
Questo script è pubblicato come codice protetto. Tuttavia, è possibile utilizzarlo liberamente e senza alcuna limitazione – per saperne di più clicca qui.
Declinazione di responsabilità
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.