OPEN-SOURCE SCRIPT

AI SuperTrend [PickMyTrade]

2 963
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:

  • 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

Snapshot

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:
  1. KNN is warmed up (table shows ● LIVE)
  2. Hurst confirms a trending regime (H ≥ Trend Threshold input)
  3. 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.

Haftungsausschluss

Die Informationen und Veröffentlichungen sind nicht als Finanz-, Anlage-, Handels- oder andere Arten von Ratschlägen oder Empfehlungen gedacht, die von TradingView bereitgestellt oder gebilligt werden, und stellen diese nicht dar. Lesen Sie mehr in den Nutzungsbedingungen.