Loading…

Cusum Change Point Detector

A moving average describes where a market has been; it is structurally incapable of saying "the regime just changed, right now." By the time a slow average has moved enough to notice a shift, the shift is old news, and a fast average is too noisy to trust for the same decision. CUSUM (cumulative sum control chart, Page's test) answers a narrower question directly: accumulate each new observation's deviation from a reference level, and alarm the instant that accumulated evidence - not any single observation - crosses a threshold. Because it sums rather than averages, a small persistent drift that would look like noise to a moving average is detected quickly, which is precisely the case a fixed-window filter is blind to. The classical two-sided form (Page's test) is implemented as CusumState plus CusumUpdate: gPos accumulates upward evidence, gNeg accumulates downward evidence, both clamped at zero from below so only a run of consistent one-directional deviation builds up. A slack parameter subtracts a small allowance before each observation is accumulated, which filters ordinary noise so the detector does not fire on every random walk excursion; a threshold sets how much accumulated evidence is required, trading detection delay against false-alarm rate. CusumSuggestParameters gives a standard rule- of-thumb starting point (slack = half the minimum shift you want to catch, threshold a small multiple of that) from a single "what size of shift matters" input. A change in volatility is frequently what a regime shift looks like before it becomes directional, and the same accumulator logic handles that case exactly: feed squared deviations from a reference variance instead of raw values, via CusumUpdateVolatility, and the identical two-sided logic detects a variance step the same way it detects a mean step. The detector is streaming and self-resetting - it holds only a handful of numbers in CusumState and updates in O(1) per observation, cheap enough to run on every tick. The moment either accumulator fires, that side resets to zero automatically, so the detector is immediately ready to catch the next shift rather than staying latched against the one it just reported. CusumScanArray is a batch convenience for backtesting a slack/threshold choice against historical data before wiring the streaming version into live logic. Notes and limitations - Slack and threshold are not free of judgement. Too small a slack fires on ordinary noise; too large a threshold delays detection of a real shift past the point it is useful. CusumSuggestParameters gives a starting point from one interpretable input, not a tuned answer - backtest it. - CUSUM detects a persistent shift in level, not a single outlier. A one-bar spike that reverts will barely move the accumulator; that is by design. - The volatility variant assumes squared deviations are fed in consistently (same reference point, same units) run to run - mixing return-squared and price-deviation-squared in one state will produce a meaningless reference variance. - This is a detection tool, not a trading signal generator on its own. It contains no order logic and places no trades; treat an alarm as an input to a wider decision process.

Script · MT4 · v1.0 · Free

About Cusum Change Point Detector

Cusum Change Point Detector is a script for MetaTrader 4. It has 4 settings, such as Series length, Shift at bar and Mean shift size. MQL source code included.

Files in the download: CusumChangeDetector.mqh, CusumChangeDetector.txt, CusumChangeDetectorDemo.mq4.

Settings

SettingDefaultWhat it does
Series length200Bars in each synthetic test series
Shift at bar120Bar index where the synthetic shift occurs
Mean shift size1.0Size of the injected mean shift
Price bars300Bars of live returns scanned in the last test

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 → Scripts.
  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 script onto a chart to run it once.

Download the PDF guide

Tags: Moving Average · News Trading · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria