Explainers / Machine Learning Adaptive SuperTrend
Machine Learning Adaptive SuperTrend, explained from the source
Machine Learning Adaptive SuperTrend is an open-source TradingView script by AlgoAlpha (about 31,400 boosts, an editor's pick). It draws a trailing stop line with a faint fill to the candle body, small arrows where the line flips sides, a number under each bar and a table in the corner. This page walks through its published code in order, says what each part counts, and ends with where it fails. It does not say what any flip will do next.
The surprise: the "machine learning" is a sort of 100 volatility readings into three piles
The name suggests a model that learns from price. In the code, the learning step takes the last 100 values of the Average True Range (ATR), a measure of how far price has been moving bar to bar, and sorts them into three piles: high, medium and low. That is the whole model. It does not look at direction, and it does not look at what happened after any earlier signal.
Step 1: an ordinary ATR
The script computes a standard ATR with length 10. The default Training Data Length is 100, so the sort always looks at the 100 most recent ATR readings. Nothing is trained once and saved: the pile assignment is redone on every bar.
Step 2: three starting guesses
The script finds the highest and lowest ATR among those 100 readings and places three starting points between them: 75% of the way up, 50% and 25%. The settings call these percentile guesses. In the code they are positions inside the range from lowest to highest reading, not percentiles of the readings. If one huge ATR spike sits in the window, the three starting points are spread out toward it.
Step 3: the sort, repeated until it settles
Each of the 100 readings is assigned to the nearest starting point by absolute distance, and a reading exactly as far from two points goes to neither. Each point then moves to the average of the readings assigned to it, and the loop repeats until all three stop moving. This is a one-dimensional k-means with three groups. Newest readings and oldest readings count equally.
Step 4: which pile the current bar belongs to
The current ATR is compared with the three settled points and joins the nearest one. That choice is the cluster for the bar. The point it joins (an ATR-sized number) is called the centroid.
Step 5: a SuperTrend built on the centroid instead of the raw ATR
A SuperTrend is a trailing stop: the middle price (high plus low, halved) plus or minus a factor times a volatility number. The default factor here is 3 and does not adapt. What adapts is the volatility number: the script uses the chosen centroid instead of the bar's own ATR. The upper band can only move down while the previous close was below it, the lower band can only move up while the previous close was above it, and the line flips when the close crosses the opposite band.
The effect is that the stop sits three centroids away. When the bar falls in the high pile, the stop is wide; in the low pile it is narrow. The three centroids themselves shift a little every bar as the 100-bar window moves.
Step 6: what you see on the chart
A green line sits under the close and a red line above it, with a faint fill between the line and the candle body. A small arrow marks each flip. A number sits near the line on every bar: 3 for the high pile, 2 for medium, 1 for low. It is a volatility group, not a rating of the signal.
Step 7: the table and the alerts
The corner table lists the three centroids, how many readings fell in each pile, and which pile the current bar is in. The script defines six alert conditions: bullish trend shift, bearish trend shift, and entering the high, medium and low volatility groups, all waiting for a confirmed bar. The low-volatility alert is written twice with the same name, so the alert list shows a duplicate.
What the settings change
ATR Length and the factor change how wide the stop is. Training Data Length changes how far back the sort looks. The three starting guesses change where the loop starts, and so can change where it settles. The two transparency values and the two colours only change appearance.
What we checked in Python
We ported the ATR, the 100-bar sort (with the script's own starting rule) and the stop to Python and ran them on invented random-walk prices (1,500 bars, three seeds, stand-in highs and lows). On those prices volatility has no pattern worth learning, and the current bar's pile still changed on 21% to 23% of bars. The sort needed about 5 passes on average (at most 22). No pile was empty on any bar, and over the run the current bar fell in each pile on 348 to 542 of about 1,390 bars. The stop flipped sides 37 to 44 times in about 1,390 bars, roughly once every 33 bars, and its median distance was about 4.3% of price. These figures describe the loop, not any market.
Where it fails
- A volatility sort, not a trend forecast. The groups say how big recent bars were. They say nothing about which way the next one goes.
- The factor does not adapt. Only the volatility input changes. The factor stays at the number you typed, so a "3" is still a three-times-ATR stop.
- The three starting points are range positions. The settings text says percentile; the code uses the range between the lowest and highest reading, so one spike moves all three.
- The piles move bar to bar. On invented prices the bar's group changed on about one bar in five, so a stop that was wide yesterday can be narrow today without anything changing in the market.
- The corner table's sizes look swapped. Reading the code, the "High" row prints the low pile's size and the "Low" row prints the high pile's size. This page read that from the code and did not check it on a chart.
- The same ATR in every row. The "current volatility" cells show one ATR value in all three rows; only the highlighted row tells you the group.
- Flips on random prices. About one flip every 33 bars on prices with no pattern at all, so a flip alone is weak evidence of a trend.
- Shapes and labels are not tied to a closed bar. The alerts wait for confirmation; the arrows and numbers do not, so on a live bar they can change before the close. Not tested here.
- Heavy loop. The sort repeats on every bar until it settles; the code sets no cap of its own on passes. We saw about 5 on average on invented prices, and did not test other data.
- No backtest. Nothing in the code measures results after a flip.
- Open and licensed. The code carries a Mozilla Public License 2.0 notice and credits AlgoAlpha; this page describes it in our own words and pastes none of it.
Also explained: SuperTrend AI (Clustering) · Lorentzian Classification · Breakout Probability (Expo) · [Liquidity Swings [LuxAlgo]](/liquidity-swings-luxalgo) · Trendlines with Breaks
Educational only. Not financial advice. Nothing here is a recommendation to buy or sell anything. Machine Learning Adaptive SuperTrend is by AlgoAlpha on TradingView and carries a Mozilla Public License 2.0 notice; this page describes its published open-source code in our own words and is not affiliated with the author. Simulation figures come from synthetic random-walk prices and say nothing about how the script trades.
Also explained: Repaint checker, VWAP, Supertrend, RSI, UT Bot Alerts, Nadaraya-Watson Envelope, Squeeze Momentum, What is repainting on TradingView?, Is TradingView backtesting accurate?, Chandelier Exit, WaveTrend Oscillator, QQE MOD, Pivot highs and lows, Hull Suite, Range Filter, Twin Range Filter, HalfTrend, ZigZag++, SSL Hybrid, CM MacD Ultimate MTF, Volume Profile / Fixed Range, ADX and DI, Support Resistance Channels, Market Structure Break & Order Block, Order Block Finder, Pivot Point SuperTrend, Trendlines with Breaks [LuxAlgo], Divergence for Many Indicators, TradingView script selling terms, Support and Resistance Levels with Break, Liquidity Swings [LuxAlgo], Pine Script errors, Breakout Probability (Expo), Did ChatGPT or Claude write your Pine Sc, Lorentzian Classification, SuperTrend AI (Clustering), Dynamic Swing Anchored VWAP, Support and Resistance Power Channel by , Support and Resistance (High Volume Boxe, Trendline Breakouts With Targets by Char, Breakout Finder by LonesomeTheBlue
Get the next teardown by email
One short email when a new teardown or free tool goes live, about twice a month. No signals, no hype. Unsubscribe in one click.
Almost done. Check your inbox and click the confirmation link.
Sent with Kit. Your address is used only for these emails. Privacy
No TradingView account yet? This affiliate link gives you a $15 coupon toward a paid plan, and we earn a commission if you buy one. The free plan is enough to follow this page.