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

Taught to TradeSystems over signals.

Explainers / SuperTrend AI (Clustering)

SuperTrend AI (Clustering), explained from the source

SuperTrend AI (Clustering) is an open-source TradingView script by LuxAlgo (about 30,800 boosts). It draws a trailing stop line, tints the candles, and puts a small number on a label each time the line flips sides. 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 "AI" is a three-way sort of nine numbers

The word clustering suggests a model trained on a lot of data. In the code, the grouping step sorts nine numbers (one per candidate SuperTrend line) into three groups, on every bar. The sections below show what those nine numbers are and why the line you see is a fresh SuperTrend built from the average of one group's factors.

Step 1: nine SuperTrend lines run at once

A SuperTrend line is a trailing stop built from the bar's middle price (high plus low, halved) and the Average True Range (ATR). The upper band is the middle price plus ATR times a factor and the lower band is the middle price minus it. The upper band may only move down while the previous close was below it, and the lower band may only move up while the previous close was above it. The line flips to the lower band when the close goes above the upper band, and to the upper band when the close goes below the lower band.

This script runs that rule for several factors at once. The defaults are an ATR length of 10 and a factor range of 1 to 5 in steps of 0.5, which gives nine factors: 1, 1.5, 2, and so on up to 5. A "Factor Range" with a minimum above the maximum stops the script with an error.

Step 2: a performance score for each line

Each of the nine lines gets a running score. Every bar the script takes the close-to-close change and multiplies it by the side that line was on at the previous close: plus one if the previous close was above the line, minus one if below. That product is smoothed with an exponential average whose weight is 2 divided by (Performance Memory plus 1); the default memory is 10, so the weight is about 0.18.

In plain words, a line scores well when the last several bars moved the way that line's side said they would. The score has a memory of roughly ten bars, so it describes the recent past, not a long record.

Step 3: the three-group split

The nine scores go into a small grouping routine (k-means, with three centroids). The starting centroids are the 25th, 50th and 75th percentile of the nine scores. Each score is assigned to the nearest centroid by absolute distance, each centroid then moves to the average of its group, and the loop repeats until the centroids stop changing (at most 1,001 passes). It runs on every bar, but only on the most recent 10,000 bars by default (the Historical Bars Calculation setting).

With nine inputs and three groups, this is a lot of ceremony for a sort. The settings choose which group is used: Best (the group with the highest centroid), Average or Worst. Best is the default.

Step 4: the chosen factor and the line you see

The script takes the average of the factors that landed in the chosen group and calls it the target factor. If the chosen group is empty on a bar, the previous target factor is kept. It then builds one more SuperTrend, using the same band rules as Step 1, but with that single average factor. That line is the trailing stop on the chart. It is not one of the nine; it is a tenth line whose factor is re-chosen every bar.

Step 5: the number on the label and the colour

The performance index is the chosen group's average score (floored at zero) divided by an exponential average of the absolute close-to-close change, using length 10. Because the numerator and denominator use the same weight, the ratio mostly sits between 0 and 1. The label shows that index times 10, rounded down, so it reads 0 to about 9. The same index sets how strong the candle tint is.

It is a ratio of "how well the chosen lines did recently" to "how much the price has been moving recently". It is not a probability and not a score of the signal that follows.

Step 6: the adaptive average

A second, fainter line moves toward the trailing stop by a fraction equal to the performance index each bar. When the index is near zero the line barely moves; when it is near one it sits almost on the stop. So the line's behaviour changes with the same recent-performance number.

Step 7: the dashboard and the alerts

The corner table shows the size of each group, a dispersion figure (average distance of the group's scores from its centroid) and the list of factors in each group. The script has no alertcondition entries, so no alert can be set from its own conditions; any alert would have to be built by the user from the plotted line.

What the settings change

ATR Length changes how wide every band is. The factor range and step change how many lines are tested (a step of 0.5 over 1 to 5 gives nine). Performance Memory changes how far back the scores look. From Cluster picks Best, Average or Worst. The two optimisation settings change how many passes and how many bars. The style settings only change colour.

What we checked in Python

We ported the nine lines, the performance score, the three-group split and the final stop to Python and ran them on invented random-walk prices (1,500 bars, three seeds, stand-in highs and lows). On those prices no factor has any edge by construction, and the chosen factor still changed on 27% to 32% of bars, moved by a full 1.0 or more on 13% to 17% of bars, and ranged from 1.0 to 5.0. The trailing stop flipped sides 80 to 83 times in about 1,400 bars. The best group held 3.4 to 3.7 of the nine factors on average, and the performance index averaged about 0.24. These figures describe the loop, not how the real script behaves on any market.

Where it fails

Also explained: Lorentzian Classification · Breakout Probability (Expo) · [Liquidity Swings [LuxAlgo]](/liquidity-swings-luxalgo) · Support and Resistance Levels with Breaks · Trendlines with Breaks

Educational only. Not financial advice. Nothing here is a recommendation to buy or sell anything. SuperTrend AI (Clustering) is by LuxAlgo on TradingView and carries a CC BY-NC-SA 4.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, Machine Learning Adaptive SuperTrend, 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.