Support Resistance Clustering
A fractal swing high at 1.10502 and another one three days later at 1.10498 are the same level to anyone looking at the chart, but they are two different doubles to any code that has not deliberately merged them. The obvious fix - round every price to the nearest few pips - throws away exactly the information that makes a level worth marking, because rounding cannot tell five genuine touches clustered near a price apart from one touch that happened to round onto it. This header keeps the touch count by clustering nearby swing points with single-linkage grouping on a sorted price array: a point joins the current cluster if it sits within tolerance of the PREVIOUS point in the run, not of the cluster's average. That distinction matters - a chain of small steps can span more than one tolerance band end to end and still be one zone, because each individual step really was a retest of the same area even as the zone itself drifted slightly over time. A flat rounding grid cannot represent that at all. Swing detection uses the standard fractal definition (a bar's high or low is strictly the extreme across a configurable number of bars on each side), generalised so a wider wing finds fewer, more significant swings and a narrower one finds more, noisier ones. The clustering tolerance is supplied in pips and converted internally with 3/5-digit-aware pip sizing, so the same input value behaves consistently across 4-digit and 5-digit quotes. The result is an array of zones, each with a mean price, a min/max bound, and a touch count, sorted strongest first. Touch count is the only strength signal this header claims - recency weighting, confluence with other timeframes, or round-number bias is context the calling program has and this header deliberately does not guess at. Self-contained: no dependencies beyond the standard library, no DLLs. Notes and limitations - This is a descriptive tool, not a signal generator. A high touch count describes the past; it is not a promise the zone holds on the next test. - Swing detection near the current bar can still repaint: a "swing high" a few bars back can stop being one as new bars form to its right within the wing window. Only swings older than InpFractalWing bars are stable. - Single-linkage clustering can, in pathological cases, chain a long run of closely-stepped prices into one very wide zone. Check loBound/hiBound before treating a zone's price as a precise level. - No thread or state concerns beyond the usual chart-history caveat: results depend on how much history the terminal has downloaded for the symbol.
Script · MT5 · v1.0 · Free
About Support Resistance Clustering
Support Resistance Clustering is a script for MetaTrader 5. It has 4 settings, such as Bars back, Fractal wing and Tolerance p. MQL source code included.
Files in the download: LevelClustering.mq5.mqh, LevelClustering.txt, LevelClusteringDemo.ex5, LevelClusteringDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Bars back | 300 | How many bars of history to scan |
| Fractal wing | 2 | Bars required on each side of a swing (2 = classic 5-bar fractal) |
| Tolerance p | 8.0 | Clustering tolerance, in pips |
| Top n | 8 | How many strongest zones to print |
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: Support & Resistance · Fractals · MT5 Script · MQL5 Source Code