OPEN-SOURCE SCRIPT
AI SuperTrend [PickMyTrade]

THE PROBLEM WITH A FIXED MULTIPLIER
Every standard SuperTrend applies the same ATR multiplier across all market conditions — the same constant during a strong trending breakout, a narrow choppy range, and a volatility spike. A value calibrated for one regime is miscalibrated for the others. Most traders compensate by manually switching timeframes or parameters. This script automates that decision.
The question it asks: what if the ATR multiplier were selected from historical bars that most resembled the current market regime — matched by Hurst state and volatility rank — rather than set by the user as a fixed constant?
────────────────────────────────────────
THREE AI LAYERS
Layer 1 — Hurst Exponent (Regime Gate)
The Hurst Exponent is computed via Rescaled Range (R/S) Analysis. It measures the degree of long-range dependence (memory) in the price series:
The Hurst gate is the first filter. Signals only fire when market structure is historically associated with persistence — not randomness or mean-reversion.
Layer 2 — Garman-Klass Volatility Rank
Garman-Klass (1980) estimates realized volatility from OHLC prices rather than close-to-close returns, capturing intrabar price range and making it more sensitive to volatility changes. The current reading is percentile-ranked against recent history (0–100%) and used as the second feature dimension for the KNN search.
Layer 3 — KNN Multiplier Optimizer
K-Nearest Neighbors searches a rolling memory bank of [Hurst, Vol Rank] feature pairs from previous bars. For each current bar it finds the K most similar historical bars by Euclidean distance in that 2D feature space. From those neighbors it retrieves the ATR multipliers that were in effect — weighted by the profitability of the bar that followed. The result is the AI Multiplier: a context-aware value drawn from the most similar past conditions, not a fixed constant.
────────────────────────────────────────
WHAT YOU SEE ON THE CHART

Candle color — the defining visual. Orange = bull trend regime, blue = bear trend regime, gray = random walk or mean-reverting. The regime state is readable on every bar without checking the table.
Signals — ● (circle) marks trend flips with Hurst ≥ 0.65, the high-conviction threshold. ▲▼ (triangle) marks standard threshold crossings. No signal fires in gray (random or mean-reverting) regimes.
SL / TP lines — dashed lines drawn automatically at each signal bar, sized from current ATR × the AI Multiplier active at that bar.
Info table (top right) — live display of Hurst value, Regime label, Direction, AI Multiplier, Vol Rank, and KNN memory bar count. Shows WARMUP until KNN has stored enough bars to begin optimizing.
────────────────────────────────────────
HOW TO USE
A signal requires all three conditions to be true simultaneously:
Circle signals (●) indicate Hurst has exceeded 0.65 — stronger persistence than the standard threshold. Triangle signals (▲▼) are at the user-defined threshold. Gray candles indicate the market is not in a trending regime; reducing exposure or standing aside is appropriate during those periods.
The regime background shading (faint orange or blue fill) shows when the SuperTrend is in an active directional state.
────────────────────────────────────────
INPUTS
AI Engine — Hurst Lookback, KNN Neighbors (K), KNN Memory (bars), Trend Threshold, Mean-Revert Threshold
SuperTrend — ATR Length, Base Multiplier, Volatility Window, EMA Period
Visual — Bull/Bear colors, SL/TP lines toggle, SL ATR Multiplier, Risk:Reward ratio, Regime Background
Display — Zen Mode (hides labels and table), Show Info Table
ALERTS
Three alert conditions: Long Signal, Short Signal, Any Signal.
────────────────────────────────────────
NOTES
Every standard SuperTrend applies the same ATR multiplier across all market conditions — the same constant during a strong trending breakout, a narrow choppy range, and a volatility spike. A value calibrated for one regime is miscalibrated for the others. Most traders compensate by manually switching timeframes or parameters. This script automates that decision.
The question it asks: what if the ATR multiplier were selected from historical bars that most resembled the current market regime — matched by Hurst state and volatility rank — rather than set by the user as a fixed constant?
────────────────────────────────────────
THREE AI LAYERS
Layer 1 — Hurst Exponent (Regime Gate)
The Hurst Exponent is computed via Rescaled Range (R/S) Analysis. It measures the degree of long-range dependence (memory) in the price series:
- H ≥ 0.55 → Persistent (trending) — SuperTrend active, signals enabled
- H ≈ 0.50 → Random walk — candles turn gray, no signal generated
- H ≤ 0.45 → Anti-persistent (mean-reverting) — signals suppressed
The Hurst gate is the first filter. Signals only fire when market structure is historically associated with persistence — not randomness or mean-reversion.
Layer 2 — Garman-Klass Volatility Rank
Garman-Klass (1980) estimates realized volatility from OHLC prices rather than close-to-close returns, capturing intrabar price range and making it more sensitive to volatility changes. The current reading is percentile-ranked against recent history (0–100%) and used as the second feature dimension for the KNN search.
Layer 3 — KNN Multiplier Optimizer
K-Nearest Neighbors searches a rolling memory bank of [Hurst, Vol Rank] feature pairs from previous bars. For each current bar it finds the K most similar historical bars by Euclidean distance in that 2D feature space. From those neighbors it retrieves the ATR multipliers that were in effect — weighted by the profitability of the bar that followed. The result is the AI Multiplier: a context-aware value drawn from the most similar past conditions, not a fixed constant.
────────────────────────────────────────
WHAT YOU SEE ON THE CHART
Candle color — the defining visual. Orange = bull trend regime, blue = bear trend regime, gray = random walk or mean-reverting. The regime state is readable on every bar without checking the table.
Signals — ● (circle) marks trend flips with Hurst ≥ 0.65, the high-conviction threshold. ▲▼ (triangle) marks standard threshold crossings. No signal fires in gray (random or mean-reverting) regimes.
SL / TP lines — dashed lines drawn automatically at each signal bar, sized from current ATR × the AI Multiplier active at that bar.
Info table (top right) — live display of Hurst value, Regime label, Direction, AI Multiplier, Vol Rank, and KNN memory bar count. Shows WARMUP until KNN has stored enough bars to begin optimizing.
────────────────────────────────────────
HOW TO USE
A signal requires all three conditions to be true simultaneously:
- KNN is warmed up (table shows ● LIVE)
- Hurst confirms a trending regime (H ≥ Trend Threshold input)
- Price is on the correct side of the EMA filter
Circle signals (●) indicate Hurst has exceeded 0.65 — stronger persistence than the standard threshold. Triangle signals (▲▼) are at the user-defined threshold. Gray candles indicate the market is not in a trending regime; reducing exposure or standing aside is appropriate during those periods.
The regime background shading (faint orange or blue fill) shows when the SuperTrend is in an active directional state.
────────────────────────────────────────
INPUTS
AI Engine — Hurst Lookback, KNN Neighbors (K), KNN Memory (bars), Trend Threshold, Mean-Revert Threshold
SuperTrend — ATR Length, Base Multiplier, Volatility Window, EMA Period
Visual — Bull/Bear colors, SL/TP lines toggle, SL ATR Multiplier, Risk:Reward ratio, Regime Background
Display — Zen Mode (hides labels and table), Show Info Table
ALERTS
Three alert conditions: Long Signal, Short Signal, Any Signal.
────────────────────────────────────────
NOTES
- KNN requires warmup equal to the KNN Memory setting before the AI Multiplier activates. During warmup the base multiplier scaled by Hurst and volatility rank is used instead.
- No repainting. All signals are confirmed on bar close. KNN stores lagged [1] values only.
- Garman-Klass citation: Garman, M. & Klass, M. (1980). On the Estimation of Security Price Volatilities from Historical Data. Journal of Business, 53(1), 67–78.
Script de código abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que cualquier republicación del código está sujeta a nuestras Normas internas.
Automate TradingView alerts to brokers no coding needed. Trade futures, forex, or crypto, copy trades across unlimited accounts, & control risk with ease.
For Tradovate: bit.ly/4a1KBrY
For all other supported brokers: bit.ly/4qEbU2G
For Tradovate: bit.ly/4a1KBrY
For all other supported brokers: bit.ly/4qEbU2G
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.
Script de código abierto
Fiel al espíritu de TradingView, el creador de este script lo ha convertido en código abierto, para que los traders puedan revisar y verificar su funcionalidad. ¡Enhorabuena al autor! Aunque puede utilizarlo de forma gratuita, recuerde que cualquier republicación del código está sujeta a nuestras Normas internas.
Automate TradingView alerts to brokers no coding needed. Trade futures, forex, or crypto, copy trades across unlimited accounts, & control risk with ease.
For Tradovate: bit.ly/4a1KBrY
For all other supported brokers: bit.ly/4qEbU2G
For Tradovate: bit.ly/4a1KBrY
For all other supported brokers: bit.ly/4qEbU2G
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.