OPEN-SOURCE SCRIPT

AetherEdge - Bayesian Changepoint Detection

338
🖊️ Overview

AE-BCP is a Bayesian detector of structural breaks (Adams & MacKay, 2007). Rather than flagging regime shifts with a hard threshold, it maintains a posterior distribution over the run length — the number of bars since the last changepoint — and updates it recursively with every new observation. A Gaussian conjugate predictive (on standardized returns or log-volatility) scores how surprising each bar is under each run-length hypothesis. Beliefs self-evolve recursively, and P(run length = 0) is the live probability that a structural break just occurred. The classic triangular run-length posterior is painted as a heatmap.

🔶 Key Features

BOCPD engine — recursively updates the run-length posterior into growth and changepoint probabilities, outputting structural breaks as a probability.
Self-evolving recursive Bayesian update — each run-length hypothesis's predictive model (sufficient statistics) and the posterior update online with every observation.
Changepoint probability (soft detection) — P(run length = 0) as a continuous 0–1 probability; threshold crossings are marked on price.
Run-length posterior heatmap — the time × run-length × probability triangular heatmap (BOCPD's signature visual), aligned to the price timeline.
Expected run length (regime age) — estimates how many bars the current regime has lasted, collapsing to zero at changepoints (shown as a line).
Volatility or drift — monitor log squared returns to detect volatility-regime changes, or returns to detect drift changes.
Live statistics panel — changepoint probability, regime age, MAP run length, last changepoint, and hazard rate.
Non-repainting design — all updates on confirmed bars only, with no look-ahead.

🧠 Technical Architecture

The monitored feature (Volatility mode = log squared return, Returns mode = return) is standardized over a long window, fixing the predictive variance at ~1. On each confirmed bar, the predictive probability of each run-length hypothesis r is pred(x_t|r) = N(x_t | m_r, σ²(1+1/r)) (m_r = the run's mean). From these, growth P(r_t=r+1) ∝ P(r_{t-1}=r)·pred·(1−H) and changepoint P(r_t=0) ∝ Σ_r P(r_{t-1}=r)·pred·H are computed and normalized. H is the hazard rate (= 1 / mean regime length), the prior probability of a changepoint.
Each run-length's sufficient statistics (sum of the run's observations) update as sum[r+1] = sum[r] + x_t on growth and sum[0] = 0 (prior) on a changepoint. Run length is truncated at Rmax for tractability. Outputs are the changepoint probability P(r_t=0), expected run length E[r]=Σr·P(r), and MAP run length argmax P(r). The posterior is binned into a rolling buffer and, on the last bar, drawn as a heatmap (boxes) aligned to the price timeline.

🎯 Three design choices stand out. First, standardization fixes the predictive variance, allowing a robust Gaussian-conjugate implementation with no gamma functions. Second, monitoring log squared returns turns "variance changes" into "mean changes," so a simple mean-change model captures volatility-regime breaks. Third, confining updates to confirmed bars keeps the historical posterior non-repainting.

⚙️ Recommended Settings & Tuning Guide

As a crypto starting point — BTC/ETH (1D, 4H): Monitor = Volatility, mean regime length 60, standardization length 200, max run length 100, changepoint threshold 0.30; volatility-regime breaks are clearly marked. For more frequent turns: lower mean regime length toward 30 (higher hazard, more sensitive) and lower the threshold to 0.25. To suppress noise: raise mean regime length to 100 and the threshold to 0.40.
Per parameter: Mean regime length is the key knob (hazard = its reciprocal) — shorter detects changepoints more often, longer is conservative. Monitor selects Volatility (vol breaks) or Returns (drift changes) for your purpose. Standardization length sets the baseline scale window (longer is steadier). Max run length sets how far back regimes are retained. Changepoint threshold sets marker sensitivity.

💡 How to Use in Practice

The core read is changepoint probability × regime age. When the changepoint probability is low and stable and the regime age keeps rising, the current regime (trend or range) is persisting — strategies aligned with that premise tend to work. The moment the changepoint probability spikes above the threshold (diamond marker fires) signals the volatility or drift structure has changed — a point to review existing positions or switch strategy to the new regime. Each time the heatmap triangle resets (run length collapses to zero), you can see the market entering a new phase visually.
For multi-timeframe work, read higher-timeframe changepoints for major structural turns and execute on a lower timeframe. It also serves as a meta-filter to run trend or mean-reversion strategies only while the regime is stable.

⚠️ Important Notes

Nothing displays until the warmup period (default 200 bars) completes. Reloading the indicator rebuilds the posterior from scratch — learning state is not persisted. This is a Gaussian-conjugate model with a known (≈1, via standardization) predictive variance that detects mean changes in the monitored feature (Volatility mode captures volatility changes via the log-squared transform). Run length is truncated at Rmax, so for very long regimes the expected run length saturates near the cap. The changepoint probability is a posterior belief, not a certain verdict, and detection requires evidence to accumulate, so it lags by a few bars. Updates occur on confirmed bars only.

🚨 Disclaimer

This indicator is for educational and informational purposes only and does not constitute financial or investment advice. Past performance is not indicative of future results. All trading involves risk. Use it alongside your own thorough testing and sound risk management; all trading decisions remain solely your own responsibility.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.