Adaptive SuperTrend KMeans MT5
- Indicators
- Thanh Trung Tran
- Version: 1.0
Adaptive SuperTrend KMeans is a MetaTrader 5 port of the open-source TradingView indicator "Machine Learning Adaptive SuperTrend" by AlgoAlpha (Mozilla Public License 2.0). All credit for the idea and the original code goes to AlgoAlpha.
Also available for MetaTrader 4 (free): https://www.mql5.com/en/market/product/198796
At a glance
- What it is: a SuperTrend whose band width adapts to the current volatility level
- Volatility levels: K-means sorts recent ATR values into high, medium and low clusters
- Faithful port: same math and the same defaults as the original Pine Script code
- No repainting: arrows and cluster numbers are drawn only on closed bars
- Price: free and open source
How it works
- Volatility: ATR with Wilder's smoothing, exactly like TradingView's ta.atr.
- Clusters: K-means groups the last 100 ATR values into high, medium and low volatility (strict assignment, as in the original).
- Band: the SuperTrend uses (high + low) / 2 plus or minus factor x the centroid of the current cluster, so the band widens in volatile markets and tightens in quiet ones.
On the chart
- Trend line that breaks when the trend flips (green up, red down)
- Gradient fill between the line and the candles, drawn with native MetaTrader 5 fills (fast, no chart objects)
- Arrows at each trend flip, cluster numbers 3 / 2 / 1 on recent bars
- Statistics table: cluster centroids, cluster sizes and the current volatility level. Click its title to fold it; put it in any corner; its colors follow your chart, dark or light.
Made for MetaTrader 5
- Alerts on a trend shift and/or a change of volatility level: popup, sound only, push notification to the MetaTrader 5 mobile app, or e-mail. Once per bar, on the closed bar.
- Tested bar by bar against an independent reference calculation on gold H1 and M15 and on EURUSD M5 and H4: every value matches, and no value changes after its bar has closed.
- Readable numbers on any symbol: values below 1 show all price digits (the original rounds to 2 decimals).
Parameters (defaults = original)
- ATR Length 10, SuperTrend Factor 3.0
- Training Data Length 100; initial high / medium / low percentile guesses 0.75 / 0.5 / 0.25
- Transparency 70 (near the line) and 95 (near the candle); bullish and bearish colors
- Chart options: bars to calculate, closed-bar-only signals, line width, fill, arrows, cluster numbers, table position, alerts
For EA developers (iCustom): buffers 0 = up trend line, 1 = down trend line, 2 = bullish flip, 3 = bearish flip, 4 = SuperTrend, 5 = direction (-1 up, +1 down, as in Pine), 6 = cluster (0 high, 1 medium, 2 low), 7 = centroid used for the band, 8 = ATR. EMPTY_VALUE means no value. Simplest call with the default inputs: iCustom(_Symbol, _Period, "Market\\Adaptive SuperTrend KMeans MT5") . If you pass inputs, each input group heading counts as one parameter (pass an empty string for it); set Draw_Objects = false when an EA calls it.
Values match TradingView when both use the same price feed. Different brokers give slightly different ATR values.
Source code and license: free and open source under the Mozilla Public License 2.0 (MPL-2.0), the same as the original. The full MQL5 source code is published in the MQL5 Code Base by the same author.
This is a visual tool. It does not place trades and does not predict prices. Test any rule built on it before you trade with it.
More from the author (MetaTrader 5):
- UT Bot Alerts with Signal Stats MT5 (free): https://www.mql5.com/en/market/product/209458
- Range Filter Buy Sell with Signal Stats MT5 (free): https://www.mql5.com/en/market/product/209459
- Chandelier Exit with Signal Stats MT5 (free): https://www.mql5.com/en/market/product/209460
- Tester vs Live Trade Matcher MT5 (free): https://www.mql5.com/en/market/product/209467
- GoldSwing T24 MT5 - trend trading with short holding times: https://www.mql5.com/en/market/product/209324
- Gold Momentum Breakout MT5 - momentum, long and short: https://www.mql5.com/en/market/product/209325
- GoldIBH MT5 - three gold strategies in one EA: https://www.mql5.com/en/market/product/209326
- Gold Entry MT5 - two trend systems in one EA: https://www.mql5.com/en/market/product/209327
