Every few months a new “AI” indicator tops the popularity charts, and the first question a programmer asks is not “does it work?” but “what does it actually compute?”. The honest answer, for most of them, is: a small, well-known statistical algorithm from the 1960s–1980s, applied cleverly to price data. That is not an insult — the Supertrend AI indicator is a genuinely interesting piece of engineering — but it is worth separating the maths from the label. This post decodes the four families you keep meeting, shows the core of each in ProBuilder, and tells you where to find working ProRealTime versions. If you want the broader picture of writing this kind of code yourself, see our complete guide to coding ProRealTime indicators and strategies with AI.
What “AI” means in an indicator (and what it usually means)
Machine learning is a broad family. An indicator legitimately earns the label when it contains a fitting procedure: something in the code adjusts its own parameters from data instead of using the number you typed in the settings panel.
In practice, the “AI” indicators circulating on charting platforms use one of four algorithms:
- k-means clustering — unsupervised, groups values into k buckets. This is what the Supertrend AI indicator runs.
- k-nearest neighbours (KNN) — supervised, classifies the present bar by looking at the most similar past bars.
- Logistic regression — supervised, fits weights on a handful of inputs to output a probability.
- Linear regression / least squares — often relabelled as “AI” when it is a curve fit.
What you will almost never find: a neural network with learned weights, a gradient-boosted tree, or a transformer. Pine Script and ProBuilder both run bar by bar, in a sandbox, with no matrix library and no persistent model file. Training anything larger than a few dozen parameters inside an indicator is not practical — and the authors know it. The marketing is in the name, not usually in the code.
The two honest red flags
Before decoding anything, two properties decide whether the maths is usable at all. Repainting: does the current bar’s output change after it closes? Label leakage: does the training label use information that was not available at the time the label is attached? A classifier trained on “price 4 bars later” must only use labels from bars at least 4 bars old. Every implementation below respects that; some copies floating around do not.
Supertrend AI indicator: k-means on a performance memory
The LuxAlgo Supertrend AI indicator, published on TradingView, is the most copied of the family and the most instructive. The idea is simple and genuinely adaptive: instead of choosing one ATR multiplier, run many Supertrends in parallel (typically a range of factors from 1 to 5), score each one continuously, then cluster the scores and trade the best-scoring group.
Step 1 — the performance memory
Each candidate factor keeps a running, exponentially smoothed measure of how much it would have earned by holding its own direction. This is the real engine of the Supertrend AI indicator; the clustering that follows is just a selection rule on top of it.
// Supertrend with a trailing "performance memory" (core of Supertrend AI)
factor = 3.0
atrPeriod = 10
perfAlpha = 10
atr = AverageTrueRange[atrPeriod](close)
hl2 = (high + low) / 2
upBand = hl2 + factor * atr
dnBand = hl2 - factor * atr
IF close > upBand[1] THEN
dir = 1
ELSIF close < dnBand[1] THEN
dir = 0
ELSE
dir = dir[1]
ENDIF
// what this factor would have earned since the last bar
diff = close - close[1]
IF dir[1] = 1 THEN
ret = diff
ELSE
ret = -diff
ENDIF
// exponential memory of that performance
perf = perf[1] + (2 / (perfAlpha + 1)) * (ret - perf[1])
RETURN perf AS "Performance memory"
Note the dir[1] in the return calculation: the direction used to score the bar is the one known before the bar, which is what keeps the score honest.
Step 2 — k-means on those scores
Run the block above for each factor, store the results in an array, and cluster them into three groups — worst, average, best. One-dimensional k-means is about twenty lines: seed the centroids across the range, assign each point to the nearest centroid, recompute the centroids as the mean of their members, repeat.
// 1-D k-means, 3 clusters, on the performances of the candidate factors
// $perf[0..n-1] holds one performance memory per Supertrend factor
n = 8
iterations = 4
hi = ArrayMax($perf)
lo = ArrayMin($perf)
c1 = lo + (hi - lo) * 0.25
c2 = lo + (hi - lo) * 0.50
c3 = lo + (hi - lo) * 0.75
FOR it = 1 TO iterations DO
s1 = 0
n1 = 0
s2 = 0
n2 = 0
s3 = 0
n3 = 0
FOR j = 0 TO n - 1 DO
d1 = ABS($perf[j] - c1)
d2 = ABS($perf[j] - c2)
d3 = ABS($perf[j] - c3)
IF d1 <= d2 AND d1 <= d3 THEN
s1 = s1 + $perf[j]
n1 = n1 + 1
ELSIF d2 <= d3 THEN
s2 = s2 + $perf[j]
n2 = n2 + 1
ELSE
s3 = s3 + $perf[j]
n3 = n3 + 1
ENDIF
NEXT
IF n1 > 0 THEN
c1 = s1 / n1
ENDIF
IF n2 > 0 THEN
c2 = s2 / n2
ENDIF
IF n3 > 0 THEN
c3 = s3 / n3
ENDIF
NEXT
RETURN c3 AS "best cluster centroid"
That is the whole “AI”. It is real unsupervised learning — the centroids are fitted, not typed in — but it is a selection mechanism over a parameter grid, not a market model. Which, in my experience, is the most useful way to describe the Supertrend AI indicator to someone who expects a neural oracle.
Lorentzian classification: KNN with a different ruler
The Lorentzian Classification indicator (published by jdehorty, and one of the most widely forked AI indicator entries on TradingView) is a k-nearest-neighbours classifier. Feature vector: a few normalised oscillators — typically RSI, CCI, ADX and a wave-trend variant. Label: the sign of the price move four bars after each historical bar. Prediction: the majority vote of the k most similar past bars.
The twist is the distance metric. Instead of Euclidean distance, it sums log(1 + |x - xᵢ|) across features. That logarithm compresses large gaps, so a bar that disagrees violently on one feature is not automatically excluded — the author’s argument being that volatility events warp the feature space and a “gravitational” metric tolerates that better.
// Approximate k-nearest-neighbours with a Lorentzian metric
lookback = 500
k = 8
f1 = RSI[14](close)
f2 = CCI[20]
predict = 0
lastDist = -1
found = 0
FOR i = 4 TO lookback DO
IF i MOD 4 = 0 THEN
d = LOG(1 + ABS(f1 - f1[i])) + LOG(1 + ABS(f2 - f2[i]))
IF d >= lastDist THEN
lastDist = d
IF close[i-4] > close[i] THEN
lab = 1
ELSE
lab = -1
ENDIF
predict = predict + lab
found = found + 1
IF found >= k THEN
BREAK
ENDIF
ENDIF
ENDIF
NEXT
RETURN predict AS "KNN vote"
Two details matter more than the metric. The i MOD 4 test spaces neighbours out in time so the vote is not eight consecutive bars of the same swing. And the ascending-distance filter is an approximate nearest-neighbour search: it is far cheaper than a full sort, but it does not return the true k nearest. The block above simplifies the original further by stopping at k accepted neighbours instead of maintaining a rolling queue. Compared with the Supertrend AI indicator, this one is supervised learning in the textbook sense — it has labels — and it is also the one most exposed to leakage if you get the four-bar offset wrong.
ML adaptive Supertrend: clustering volatility, not signals
The “Machine Learning Adaptive SuperTrend” family (AlgoAlpha’s version being the best known) applies the same k-means, but to a different quantity. Here the clustered variable is volatility: ATR values over a lookback window are grouped into low, medium and high regimes, and each regime gets its own multiplier. Tight bands in quiet markets, wide bands when the range expands.
// Volatility regime -> Supertrend factor
atr = AverageTrueRange[10](close)
lookback = 100
hiV = Highest[lookback](atr)
loV = Lowest[lookback](atr)
// centroids, here simply seeded on the range (refine them with k-means)
c1 = loV + (hiV - loV) * 0.20
c2 = loV + (hiV - loV) * 0.50
c3 = loV + (hiV - loV) * 0.80
d1 = ABS(atr - c1)
d2 = ABS(atr - c2)
d3 = ABS(atr - c3)
IF d1 <= d2 AND d1 <= d3 THEN
factor = 1.5
ELSIF d2 <= d3 THEN
factor = 2.5
ELSE
factor = 3.5
ENDIF
RETURN factor AS "adaptive factor"
Replace the seeded centroids with the k-means loop from earlier and you have the published logic. It is a regime switch, and it is a good one — but it learns the volatility distribution, not the direction. Where the Supertrend AI indicator selects among strategies by result, this one selects among parameters by market state.
Logistic regression and KNN on RSI
The third family is the smallest and the most transparent: a logistic regression updated online, one gradient step per bar. Inputs are normalised oscillators, output is a probability between 0 and 1, and the weights drift as the market changes.
// Online logistic regression on two normalised inputs
lr = 0.01
x1 = (RSI[14](close) - 50) / 50
x2 = (close - Average[50](close)) / AverageTrueRange[14](close)
// prediction made with yesterday's weights and yesterday's inputs
z = w0[1] + w1[1] * x1[1] + w2[1] * x2[1]
p = 1 / (1 + EXP(-z))
IF close > close[1] THEN
y = 1
ELSE
y = 0
ENDIF
err = y - p
w0 = w0[1] + lr * err
w1 = w1[1] + lr * err * x1[1]
w2 = w2[1] + lr * err * x2[1]
RETURN p * 100 AS "P(up) in %", 50 AS "neutral"
Three weights, one learning rate, no repainting. This is stochastic gradient descent — the same optimiser that trains neural networks, just on a model with three parameters. KNN-on-RSI variants do the same job non-parametrically: find the past bars whose RSI was closest to today’s, average what happened next. Both are far more modest than the Supertrend AI indicator in scope, and far easier to audit.
Is there a best AI indicator for trading?
Here is the comparison that matters to a programmer:
| Indicator | Algorithm actually running | What is learned | Classical equivalent |
|---|---|---|---|
| Supertrend AI | k-means (unsupervised) | Which ATR factor is performing | Walk-forward parameter selection |
| Lorentzian classification | Approximate KNN (supervised) | Direction 4 bars ahead | Analogue / pattern matching |
| ML adaptive Supertrend | k-means on ATR | Volatility regime | ATR percentile switch |
| Logistic regression / KNN on RSI | SGD or KNN | Probability of an up bar | Weighted oscillator composite |
Do they beat the plain versions? The only answer worth anything is the one you produce yourself, and the test is easy to rig: code the adaptive version and the fixed-parameter version as two ProBacktest strategies with identical entries, exits and costs, then run both over the same instrument and period. In the specific case of the Supertrend AI indicator, the fair benchmark is not a single Supertrend — it is the average of the candidate factors it selects from, because that is what you would have got without the clustering.
What the extra machinery costs you
Adaptive selection adds degrees of freedom: a lookback for the performance memory, a number of clusters, a number of iterations, the factor grid itself. Each one is a knob you can overfit. So a searchable “best AI indicator” does not really exist — what exists is a trade-off between reactivity and stability, and k-means moves you toward reactivity. Judge any best AI trading indicator claim on out-of-sample bars, on more than one instrument, with spread included.
Where to get them for ProRealTime
All four families have been ported to ProBuilder by the community, and the indicator library is where to look: search it for “Supertrend AI”, “Lorentzian”, “k-means”, “KNN” or “machine learning”. The ports are generally faithful, and reading a ProBuilder version next to the original is the fastest way to understand what a given AI indicator on TradingView really does — ProBuilder’s explicit loops leave nowhere for the algorithm to hide.
A few practical notes when you run them. Array-based clustering is heavy: keep the factor grid small, cap the k-means iterations at four or five, and watch the loading time on long histories. Check the bar-close behaviour before trading anything live. And if you adapt a version of the Supertrend AI indicator for your own use, keep the original author’s credit in the header comment — this whole ecosystem runs on people publishing their work in the open.
Decoded, none of these is magic and none of them is a fraud. They are classical estimators, applied with taste, to a hard problem. That is a reasonable thing to put on a chart — as long as you know which one you are running.