Loading…

Swing Magnitude Distribution Export

Traders often set a target or a trailing distance from a vague sense of "how big swings usually are" on a chart, without ever having actually measured it. This script measures it: it reconstructs the market's ZigZag-style swing structure over the loaded history and exports every leg's size and duration as a row in a CSV file, ready to drop into a spreadsheet for a proper distribution. The pivot chain is built in two passes rather than a single running extreme tracker. Pass one scans for fractal turning points - a bar whose high or low is the most extreme among InpFractalWidth bars on each side. Pass two walks those candidates in order and merges them into a clean alternating high-low-high-low chain: same-side duplicates are collapsed to the more extreme point, and a reversal that fails to clear InpDeviationPips is dropped outright rather than promoted to a pivot. This two-pass structure avoids a classic bug in naive single-pass zigzag code, where the "opposite" running extreme keeps quietly accumulating bars from before the last confirmed pivot and contaminates where the next leg is measured from. Each leg between two confirmed pivots is one row of output: its direction, its size in pips, and how many bars it took to form. Notes and limitations - This is a structural export, not a repainting chart indicator - it runs once over closed history and does not update live. It has no relationship to any on-chart ZigZag indicator's exact pivot placement, only a comparable general shape. - The fractal-plus-deviation method is one reasonable way to define "a swing", not the only one. A different InpFractalWidth or InpDeviationPips will produce a materially different leg count and size distribution on the same data - the output is only as meaningful as the settings used to produce it. - A bar that is simultaneously a fractal high and a fractal low (an outside bar dominating both neighbours) is skipped rather than assigned to either side, which can occasionally drop a genuine turning point on very volatile data. - Duration is measured in bars of the chosen timeframe, not wall-clock time, so results are not directly comparable across timeframes without converting first.

Script · MT4 · v1.0 · Free

About Swing Magnitude Distribution Export

Swing Magnitude Distribution Export is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 7 settings, such as Symbol, Timeframe and Max bars. MQL source code included.

Files in the download: SwingMagnitudeDistribution.mq4, SwingMagnitudeDistribution.txt.

Settings

SettingDefaultWhat it does
SymbolSymbol to study, blank = this chart's symbol
TimeframePERIOD_H1Timeframe to study
Max bars5000Most recent bars to include
Fractal width2Bars on each side that must be lower/higher for a fractal pivot
Deviation pips15.0Minimum reversal size to keep a pivot, in pips
Save csvtrueWrite every leg to a CSV file
DelimiterDelimiterCommaCSV column separator

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: Pivot Points · ZigZag · Trailing Stop & Break-even · Alerts · MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria