top of page

Pine Script Library

Public·1 member

Adaptive Supertrend — Trend Drop


# Adaptive Supertrend — Trend Drop


The classic Supertrend is brilliant in trending tape and brutal in chop because the multiplier never changes. This adaptive build flexes the ATR multiplier in real time based on the current volatility regime, so the stop tightens in quiet conditions and widens before the noise can shake you out.


## What it does

- Computes Wilder ATR and compares it to a longer-period ATR average to read the current volatility regime

- Scales the Supertrend multiplier dynamically — tighter stops in low-vol, wider stops in high-vol — clamped between user-defined min/max

- Plots a single supertrend line that flips color green ↔ red as price closes through the trailing stop

- Drops green/red arrows on every flip and optionally tints the background so the regime is impossible to miss

- Built-in `alertcondition` calls fire on each long/short flip — wire them into TradingView alerts


## Recommended settings

- ATR length: **10**

- ATR average length: **50**

- Base multiplier: **2.0**

- Min multiplier: **1.0**

- Max multiplier: **4.0**

- Show flip arrows: **on**

- Color background: **on**


## Ideal timeframe

Built for 15m–4h swing and 1h–Daily position work where the volatility regime actually shifts enough to matter. On 1m/5m it'll still flip cleanly but the adaptive scaling is most useful on higher timeframes where ATR regime changes are meaningful and slower. Pair with a higher-timeframe trend filter for best results.


## How to use

Treat the colored line as a dynamic trailing stop: long while green, short while red, flat or reverse on a confirmed flip. The arrow is your trigger — fire on the close that confirms it, not intrabar. Because the multiplier widens when volatility expands, you get fewer false flips during news spikes and earnings; in slow grinding tape it tightens and gives you earlier exits. Keep min ≥ 1.0 so the stop never collapses to the bar, and max ≤ 4.0 so it never drifts so wide it becomes useless. If you're seeing too many flips, raise the base multiplier; if you're getting whipsawed, increase ATR length to smooth.


## Pine Script v5 code

```

//@version=5

// ─────────────────────────────────────────────

// │ MarketFragments.com | DNA & Market │

// │ info@marketfragments.com │

// │ www.marketfragments.com │

// ─────────────────────────────────────────────

// Time →

// │

// █ █ █│ █

// █ █ █ │ █ █

// █ █ █ │ █ █ █ ╭─╮

// █ █ █ │ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮

// █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ █ ╰─╮ ╭─╯

// █ █ █ │ █ ╰─────╯

// ──────┴──────────────────────────────────────────────────────────────

// T1 T2 T3 T4 T5 T6

//

// Adaptive Supertrend — MarketFragments free drop

// Volatility-adaptive Supertrend that scales its ATR multiplier based on

// the ratio of current ATR to a longer-term ATR average. Quiet tape →

// tighter stop. Loud tape → wider stop. Clamped between min/max.

// Free for non-commercial use. Attribution appreciated.


indicator("MF Adaptive Supertrend", overlay=true, shorttitle="MF AdSupT")


// --- Inputs ---

atrLength = input.int(10, "ATR Length", minval=1)

atrAvgLength = input.int(50, "ATR Avg Length", minval=1)

baseMult = input.float(2.0, "Base Multiplier", minval=0.1, step=0.1)

minMult = input.float(1.0, "Min Multiplier", minval=0.1, step=0.1)

maxMult = input.float(4.0, "Max Multiplier", minval=0.1, step=0.1)

showArrows = input.bool(true, "Show Flip Arrows")

colorBg = input.bool(true, "Color Background")


// --- ATR + volatility-adaptive multiplier ---

atr_ = ta.rma(ta.tr(true), atrLength)

atrAvg = ta.sma(atr_, atrAvgLength)

volRatio = atrAvg <= 0 ? 1.0 : atr_ / atrAvg

adaptiveMult = math.max(minMult, math.min(maxMult, baseMult * volRatio))


// --- Basic bands ---

src = hl2

upBasic = src + adaptiveMult * atr_

dnBasic = src - adaptiveMult * atr_


// --- Ratcheting final bands ---

var float upperBand = na

var float lowerBand = na

prevUpper = nz(upperBand[1], upBasic)

prevLower = nz(lowerBand[1], dnBasic)

prevClose = nz(close[1], close)

upperBand := (upBasic < prevUpper or prevClose > prevUpper) ? upBasic : prevUpper

lowerBand := (dnBasic > prevLower or prevClose < prevLower) ? dnBasic : prevLower


// --- Direction: 1 = up (use lower band), -1 = down (use upper band) ---

var int dir = 1

if na(atr_[1])

dir := 1

else if nz(dir[1], 1) == -1

dir := close > upperBand ? 1 : -1

else

dir := close < lowerBand ? -1 : 1


// --- Supertrend line ---

supertrend = dir == 1 ? lowerBand : upperBand


upPlot = dir == 1 ? supertrend : na

dnPlot = dir == -1 ? supertrend : na

plot(upPlot, "ST Up", color=color.green, linewidth=2)

plot(dnPlot, "ST Dn", color=color.red, linewidth=2)


// --- Flip arrows ---

longFlip = dir == 1 and dir[1] == -1

shortFlip = dir == -1 and dir[1] == 1

plotshape(showArrows and longFlip, "Long Flip",

style=shape.triangleup, location=location.belowbar,

color=color.green, size=size.small)

plotshape(showArrows and shortFlip, "Short Flip",

style=shape.triangledown, location=location.abovebar,

color=color.red, size=size.small)


// --- Background tint ---

bgcolor(colorBg ? (dir == 1 ? color.new(color.green, 92) : color.new(color.red, 92)) : na)


// --- Alerts ---

alertcondition(longFlip, "MF Adaptive Supertrend LONG flip",

"Adaptive Supertrend flipped LONG on {{ticker}} @ {{close}}")

alertcondition(shortFlip, "MF Adaptive Supertrend SHORT flip",

"Adaptive Supertrend flipped SHORT on {{ticker}} @ {{close}}")

```


Drop your tweaks, screenshots, and questions below — that's how the library gets better.


Brain with financial data analysis.

Inquiries at :

Important Risk Notice: Trading involves substantial risk of loss. This is educational content only—not advice. Full details here  ------------>  

Proceed only if you're prepared.

tel#: (843) 321-8514

bottom of page