K Means Liquidity Zone Clusterer MT5
- Göstergeler
-
Anthony Cooke
Quantitative Systems Architect and C++, Python, MQL5, MQL4, and Pine Script developer specializing in non-repainting mathematical indicators, Expert Advisors, and statistical strategy audits. I combine industrial quality-control engineering and Applied Mathematics with multi-physics market modeling - Sürüm: 1.0
OVERVIEW AND SCIENTIFIC ORIGIN
The K Means Liquidity Zone Clusterer is a standalone, unrestricted main-chart structural indicator engineered from Unsupervised Machine Learning and Market Microstructure using Deterministic 1D Volume-Weighted Lloyd's K-Means Clustering. Rather than drawing subjective horizontal lines by eye, this engine automatically groups causally confirmed swing highs and swing lows weighted by local tick volume into objective institutional support, resistance, and Point-of-Control (POC) liquidity zones directly over candlesticks.
It runs with full unrestricted functionality across all symbols, asset classes (Forex, Gold XAUUSD, Silver XAGUSD, Indices, and Commodities), and timeframes with zero external DLL calls and 100 percent causal non-repainting calculation.
FOUR-STEP MACHINE LEARNING ENGINE
1. Causal Swing Confirmation: Identifies swing highs and swing lows strictly after InpPivotOrder (default 3) confirmed bars close to the right, guaranteeing zero repainting on closed bars. Each confirmed swing is weighted by its local 3-bar average tick volume.
2. Deterministic Empirical Quantile Seeding: Seeds K (3 to 5, default 4) initial cluster centroids across empirical price quantiles over the rolling lookback window (InpLookback = 250 bars). Deterministic seeding eliminates random initialization jitter so zones stay 100 percent identical across chart reloads.
3. Lloyd's Iterative Convergence: Executes up to 20 volume-weighted Lloyd assignment and centroid-update loops with deterministic empty-cluster recovery, sorts all clusters ascending by price, and computes each cluster's volume share (%), confirmed swing hit count, and ATR-bounded dispersion width (InpZoneWidthMult = 0.65).
4. Dynamic Role Classification: Automatically designates the highest-volume cluster as the Primary Point-of-Control (POC) Liquidity Magnet and labels surrounding clusters above or below live price as tiered Resistance (RES 1, RES 2...) and Support (SUP 1, SUP 2...).
MAIN-CHART VISUALS AND ZONE GUIDE
Active shaded liquidity boxes (width set by InpZoneBars = 100 bars) render cleanly behind candlesticks so price action stays uncluttered:
- Resistance / Supply Zones (above price): Dark crimson shaded band with a dashed Crimson centroid core line and Tomato tag.
- Support / Demand Zones (below price): Dark navy shaded band with a dashed DeepSkyBlue centroid core line and MediumSpringGreen tag.
- Primary POC Magnet / Active Pivot Zone (highest-volume cluster or zone currently containing price): Dark gold/amber shaded band with a bold solid Gold centroid core line (width 2) and Gold tag.
- Left-Shoulder Zone Tags (InpShowZoneTags = true): Anchored at the top-left of each shaded zone showing Role, Clustered Volume Share %, and Confirmed Swing Hit Count (for example: ACTIVE POC | 36% VOL (20) or SUP 1 | 29% VOL (15)).
DARK TELEMETRY HUD BOX GUIDE
Anchored at the Top-Left of the main chart by default (movable to Top-Right via InpHudRight = true) inside a dark slate panel with a DodgerBlue border:
- Line 1 (K-MEANS ZONES): Live price position relative to the cluster map (AT PRIMARY POC MAGNET or INSIDE ACTIVE PIVOT ZONE in Gold; BETWEEN LIQUIDITY ZONES in Silver) plus active cluster count K.
- Line 2 (RES and SUP): Exact price and broker-point distance to the closest overhead Resistance centroid and closest underlying Support centroid.
- Line 3 (POC): Exact price of the Primary Point-of-Control magnet, its share of clustered swing volume (%), confirmed swing hit count, and signed point distance from live close.
- Line 4 (SAMPLE): Live count of causally confirmed swing highs and lows feeding Lloyd's clustering engine over the rolling lookback window.
PRACTICAL USAGE AND PRICE DISCOVERY
- Liquidity Reactions and Targets: Watch how price reacts at high-volume RES 1 and SUP 1 bands and track pullbacks toward the bold Gold Primary POC Magnet. Higher Volume % and Swing Hit counts mark levels where institutional order flow concentrated repeatedly.
- Breakout Price Discovery: When an instrument breaks out to a fresh lookback extreme beyond all historical swings, all K clusters naturally sit on one side of price and HUD Line 2 explicitly reports RES: NEW HIGH or SUP: NEW LOW rather than inventing artificial levels.
BACKTESTING, STRATEGY TESTER AND NON-REPAINTING VERIFICATION
By default, InpShowTrails is set to false so only the active shaded zones appear on the right side of the chart, keeping live price action completely uncluttered.
To visually backtest historical cluster levels, see how institutional zones shifted bar by bar across past trends, or verify 100 percent causal non-repainting behavior in live markets or the Strategy Tester:
1. Open the indicator Inputs tab and change InpShowTrails from false to true.
2. This plots all 5 historical causal centroid line buffers (KMeans_Centroid_1 through KMeans_Centroid_5 as subtle dotted trails) across the calculation window controlled by InpMaxBars (default 800 bars; raise InpMaxBars when inspecting deeper chart history).
3. For automated Strategy Tester and Expert Advisor integration via iCustom(), three dedicated non-repainting data buffers run continuously even when visual trails are hidden: Nearest_Resistance (Buffer 5), Nearest_Support (Buffer 6), and Primary_POC_Magnet (Buffer 7).
CORE INPUT PARAMETERS AND BUFFER MAP
Clustering Engine Inputs:
- InpClusters (Default: 4, Range 3 to 5): Number of K-Means liquidity clusters.
- InpLookback (Default: 250, Range 80 to 600): Rolling bar window for collecting confirmed swings.
- InpPivotOrder (Default: 3, Range 2 to 6): Causal confirmation bars required on each side of a swing pivot.
- InpZoneBars (Default: 100, Range 30 to 300): Horizontal width of active shaded boxes in chart bars.
- InpZoneWidthMult (Default: 0.65, Range 0.25 to 1.50): Multiplier scaling ATR-bounded cluster dispersion height.
Visuals, Backtesting and HUD Inputs:
- InpShowTrails (Default: false): Set to true to plot dotted historical causal centroid trails for visual backtesting.
- InpShowZoneTags (Default: true): Toggles left-shoulder Role, Volume %, and Hit Count zone tags.
- InpShowHUD (Default: true) and InpHudRight (Default: false): Toggles the 4-line dark telemetry HUD and switches between Top-Left (false) and Top-Right (true).
- InpMaxBars (Default: 800): Maximum historical bars calculated for fast chart loading.
Expert Advisor Integration (iCustom Buffers):
Buffers 0 to 4: KMeans_Centroid_1 through KMeans_Centroid_5 (Dotted historical trails when InpShowTrails = true) | Buffer 5: Nearest_Resistance | Buffer 6: Nearest_Support | Buffer 7: Primary_POC_Magnet. Short name: KMeans_Zones(K=4,250).
USER CALIBRATION PRESETS
- Balanced Swing (Default — H1 to H4 on XAUUSD, Major Forex, US Indices): InpClusters = 4, InpLookback = 250, InpPivotOrder = 3, InpZoneWidthMult = 0.65.
- Fast Intraday and Scalping (M5 to M15): InpClusters = 3 or 4, InpLookback = 120 to 150, InpPivotOrder = 2, InpZoneWidthMult = 0.50.
- Macro Structural Levels (H4 to Daily): InpClusters = 4 or 5, InpLookback = 300 to 400, InpPivotOrder = 4, InpZoneWidthMult = 0.75.
##### FEEDBACK, RATINGS AND SUPPORT #####
If you find this indicator useful in your analysis, please consider leaving an honest star rating and review in the Reviews tab - your feedback directly guides future updates and improvements. Feel free to post any setup or calibration questions in the Comments tab.
#########################################
------Trading on MetaTrader 4 instead of MetaTrader 5? The identically engineered MetaTrader 4 edition of K Means Liquidity Zone Clusterer is available on my MQL5 Seller Profile with the exact same thumbnail, deterministic Lloyd's K-Means engine, shaded liquidity zones, dark telemetry HUD, and parameter set.
