Loading…

Bayesian Change Point Detector

Most "structural break" tools on MetaTrader either use a fixed lookback window that arbitrarily decides how far back to compare, or a moving average crossover dressed up as a regime filter. This implements the actual Bayesian Online Change Point Detection algorithm from Adams and MacKay's 2007 paper: at every closed bar, it maintains a full posterior distribution over "run length" - how many bars have passed since the last genuine change point - using a Normal-Inverse-Gamma conjugate model of log returns and a Student-t predictive distribution. Each new bar either extends the current run (business as usual) or resets it to zero (a change point), and the algorithm tracks the probability of both outcomes simultaneously rather than committing to one. The plotted value is the posterior probability that the run length reset to zero at that bar - in plain terms, "how likely is it that market behaviour just changed". Because the hypothesis set would otherwise grow by one every single bar forever, the run-length posterior is truncated at InpMaxRunLength and renormalised each step; a run this old carries negligible probability mass in practice long before that cap is reached, so the truncation is a standard, well-understood approximation rather than a shortcut that changes the answer. The recursion only ever advances on bars that have actually closed - the still-forming bar is deliberately skipped every time - so a probability once plotted for a closed bar is final and can never repaint later. Notes and limitations - Needs InpWarmupBars + 2 bars of history before any reading appears, and the first pass over a long history can take a moment to compute since every bar performs a full posterior update. - The run-length truncation at InpMaxRunLength is a deliberate, documented approximation of the unbounded original algorithm - raise it if very long, stable regimes are common on the instrument being watched and the extra computation is acceptable. - Uses a constant hazard rate (a fixed expected time between change points); it does not adapt the hazard itself over time the way some research variants do. - A high change-point probability means the model judges recent return behaviour statistically unlike what preceded it - it says nothing about direction, and is not, by itself, a trade signal. - Assumes log returns are reasonably well described by a Normal distribution with unknown, possibly changing mean and variance; it will underperform on data with heavy structural features that model does not capture, such as strong autocorrelation.

Indicator · MT4 · v1.0 · Free

About Bayesian Change Point Detector

Bayesian Change Point Detector is an indicator for MetaTrader 4. It draws buy and sell arrows on the chart and can alert you with a pop-up, sound, email or push message. It has 5 settings, such as Max run length, Hazard lambda and Change prob threshold. MQL source code included.

Files in the download: BayesianChangePointDetector.ex4, BayesianChangePointDetector.mq4, BayesianChangePointDetector.txt.

Settings

SettingDefaultWhat it does
Max run length200Truncation cap on the run-length hypothesis set
Hazard lambda250Expected bars between change points (hazard = 1/lambda)
Change prob threshold0.5P(change point) at or above this is flagged
Warmup bars50Oldest bars sampled to set the prior's variance scale
AlertstrueAlert once when a flagged change point appears

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Indicators.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the indicator from the Navigator onto a chart and adjust the settings.

Download the PDF guide

Tags: Moving Average · Buy/Sell Arrows · Alerts · MT4 Indicator · MQL4 Source Code

MetaTrader 4 & 5 indicators · PolyMaria