Indicator · Pine Script v6 · On the chart

SuperTrend

by mihakralj

This indicator calculates SuperTrend from an ATR-smoothed band around the midpoint of high and low.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code66
Settings3inputs
LicenceApache-2.0
Last update4 weeks ago27 Sep 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

66 lines of Pine Script v6 from lib/dynamics/super/super.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

super.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("SuperTrend", "SUPER", overlay=true)

//@function Calculates SuperTrend using ATR-based dynamic support/resistance
//@param source Price series for calculation (typically hlc3 or close)
//@param atr_period Lookback period for ATR calculation
//@param multiplier Multiplier applied to ATR for band calculation
//@returns Tuple [supertrend, direction] where direction is 1 (bullish) or -1 (bearish)
//@optimized O(1) with proper warmup handling
super(series float source, simple int atr_period, simple float multiplier) =>
    if atr_period <= 0
        runtime.error("ATR period must be greater than 0")
    if multiplier <= 0.0
        runtime.error("Multiplier must be greater than 0")
    float hl2_value = (high + low) / 2.0
    float tr = math.max(high - low, math.max(math.abs(high - nz(close[1])), math.abs(low - nz(close[1]))))
    float alpha = 1.0 / atr_period
    float beta = 1.0 - alpha
    var bool warmup = true
    var float e = 1.0
    var float atr = 0.0
    var float compensated_atr = tr
    atr := alpha * (tr - atr) + atr
    if warmup
        e *= beta
        float c = 1.0 / (1.0 - e)
        compensated_atr := c * atr
        warmup := e > 1e-10
    else
        compensated_atr := atr
    float basic_ub = hl2_value + (multiplier * compensated_atr)
    float basic_lb = hl2_value - (multiplier * compensated_atr)
    var float final_ub = basic_ub
    var float final_lb = basic_lb
    var int trend = 1
    final_ub := basic_ub < final_ub or nz(close[1]) > final_ub ? basic_ub : final_ub
    final_lb := basic_lb > final_lb or nz(close[1]) < final_lb ? basic_lb : final_lb
    int prev_trend = nz(trend[1], 1)
    trend := close > final_ub ? 1 : close < final_lb ? -1 : prev_trend
    float supertrend = trend == 1 ? final_lb : final_ub
    [supertrend, trend]

// ---------- Main loop ----------

// Inputs
i_atr_period = input.int(10, "ATR Period", minval=1, maxval=100)
i_multiplier = input.float(3.0, "Multiplier", minval=0.1, step=0.1)
i_source = input.source(close, "Source")

// Calculation
[st_line, st_direction] = super(i_source, i_atr_period, i_multiplier)

// Colors
color bullish_color = color.new(color.green, 0)
color bearish_color = color.new(color.red, 0)
color line_color = st_direction == 1 ? bullish_color : bearish_color

// Plot
plot(st_line, "SuperTrend", color=line_color, linewidth=2, style=plot.style_line)

// Optional: Plot buy/sell signals when direction changes
bool direction_changed = st_direction != nz(st_direction[1])
plotshape(direction_changed and st_direction == 1, "Buy Signal", shape.labelup, location.belowbar, color=bullish_color, text="BUY", textcolor=color.white, size=size.small)
plotshape(direction_changed and st_direction == -1, "Sell Signal", shape.labeldown, location.abovebar, color=bearish_color, text="SELL", textcolor=color.white, size=size.small)

Overview

SuperTrend plots the active band on the price chart, green in an uptrend and red in a downtrend. Trend flips when close crosses the opposite band and otherwise holds. ATR period and multiplier change band width. Buy and sell labels mark direction changes. It is an indicator only and has no position sizing or stop rules.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Volatility & bands, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

algorithmic-tradingcsharpdotnetpinescriptpythonquantitative-financesimdta-libtechnical-analysistechnical-indicators

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
ATR Periodint10i_atr_period
Multiplierfloat3.0i_multiplier
Sourcesourceclosei_source
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

[RS]Donchian Fans

by ricardosantos79
261 stars

This indicator plots Donchian Channel boundary lines along with dynamic fan projections directly on the price chart. It calculates the highest high, lowest low, and median price over a customizable lookback period. Whenever price establishes a new channel high

Indicator v4 Support & resistanceTrend
Updated 4 years ago · None stated GitHub repository of [RS]Donchian Fans

Dynamic Linear Regression

by TradersPost
218 stars

Dynamic Linear Regression calculates and plots linear regression trendlines and standard deviation channels directly on the price chart over a user-selected time period. Traders can configure static channels locked at the end time, dynamic channels that contin

Indicator v6 TrendSupport & resistance
Updated 3 months ago · None stated GitHub repository of Dynamic Linear Regression

Moon Dev Lunar Sniper

by moondevonyt
92 stars

Moon Dev Lunar Sniper is a multi-indicator overlay that scores confluence across fast and slow EMAs, RSI, MACD, DMI, and BB width to fire signals only when score meets the minimum threshold. It plots buy and sell signals on the price chart, highlights cosmic b

Indicator v6 TrendOscillators & momentum Repaints
Updated 8 weeks ago · Custom licence GitHub repository of Moon Dev Lunar Sniper

SuperTrend & Pivots

by dcaoyuan
55 stars

This overlay indicator plots a SuperTrend line on the price chart using a fixed ATR length of 10 and a factor of 3. When the trend direction is up it draws a green line and a light green fill toward the candle midpoint; when down it draws a red line and a ligh

Indicator v6 TrendVolatility & bands
Updated 6 months ago · Apache-2.0 GitHub repository of SuperTrend & Pivots
47 stars

The Ultimate Channel calculates an ultra-smoothed centerline alongside upper and lower volatility bands directly on the main price chart. It processes true range values through an advanced infinite impulse response smoothing filter to establish dynamic channel

Indicator v6 Volatility & bandsTrend
Updated 8 months ago · MIT GitHub repository of Ultimate Channel (UCHANNEL)

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers