Free: indicator teardowns, a repaint checker and open-source scripts. No buy/sell signals, ever.

Taught to TradeSystems over signals.

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

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

Next steps

  • Get a script fixed. Compiles but behaves differently live, or changes after a reload? In a Pine Script Audit, an AI model reads every line and writes a report on what to fix for $49, or also fixes those problems for $149. It cannot prove a script is safe.
  • Check your own script. Paste its source into the free repaint checker. It runs in your browser and flags known patterns.
Subscribe to the Teardown Letter: $19/moBack to Taught to Trade

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.