Range Cycle Alternation Study
"A narrow range day sets up a breakout day" is a common claim that is rarely tested against what a purely random Narrow/Wide sequence would already produce by itself. This script classifies every daily bar as Narrow or Wide by comparing its own high-low range to the average range of a trailing lookback window ending strictly before that bar - so the threshold used to classify a bar never looks at the bar itself or anything beyond it. Once the whole history is classified into a binary Narrow/Wide sequence, the script counts the observed number of runs (unbroken streaks of the same label) and compares it to the run count a random sequence with the same Narrow/Wide totals would be expected to produce, using the standard Wald-Wolfowitz runs test: expected run count, its variance, and a z-score. Too FEW runs than expected means same-type days cluster together (a range regime, the opposite of alternation); too MANY runs means genuine alternation; a z-score near zero means the sequence is not distinguishable from chance. Alongside the formal test, the script reports the plain conditional probability of tomorrow being Wide given today was Narrow, against the unconditional probability of any day being Wide - the practical number most traders actually mean by "narrow precedes wide". Notes and limitations - Daily timeframe only (PERIOD_D1). - The runs test's z-score assumes a reasonably large sample; with only a small handful of classified bars the normal approximation used for the variance is not reliable - the script needs at least 30 classified bars and will refuse to run below that. - "Wide" and "Narrow" are always relative to the SAME symbol's own trailing history, never an absolute pip threshold, so the study is meaningful across FX pairs, metals and indices without any manual recalibration. - A significant runs-test result describes the historical sequence scanned. It says nothing about whether the same pattern will persist, and nothing here accounts for trading costs.
Script · MT5 · v1.0 · Free
About Range Cycle Alternation Study
Range Cycle Alternation Study is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Max bars, Lookback and Output file. MQL source code included.
Files in the download: RangeCycleAlternation.ex5, RangeCycleAlternation.mq5, RangeCycleAlternation.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Max bars | 3000 | Daily bars to scan (0 = all available) |
| Lookback | 20 | Bars used to build each day's trailing average range |
| Output file | RangeCycleAlternation.csv | Output file (in MQL5\Files) |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Breakout · Trailing Stop & Break-even · Alerts · MT5 Script · MQL5 Source Code