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.
Skrip dilindungi
Skrip ini diterbitkan sebagai sumber tertutup. Akan tetapi, anda boleh menggunakannya dengan percuma dan tanpa had – ketahui lebih lanjut di sini.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Skrip dilindungi
Skrip ini diterbitkan sebagai sumber tertutup. Akan tetapi, anda boleh menggunakannya dengan percuma dan tanpa had – ketahui lebih lanjut di sini.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.