Indicator · Pine Script v6 · On the chart

Super Trend Bands (STBANDS)

by mihakralj

This indicator calculates SuperTrend-style upper and lower bands from the high-low midpoint, offset by an average true range times a multiplier.

View on GitHub

GitHub stars99on the repository
Pine versionv6current
Lines of code67
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

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

stbands.pine
// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Super Trend Bands (STBANDS)", "STBANDS", overlay=true)

//@function Calculates Super Trend Bands using ATR-based dynamic support/resistance
//@param source Series to calculate bands from
//@param period Lookback period for ATR calculation
//@param multiplier ATR multiplier for band distance
//@returns [upper_band, lower_band, trend] Super Trend band values and trend direction
//@optimized for performance and dirty data
stbands(series float source, simple int period, simple float multiplier) =>
    if period <= 0 or multiplier <= 0.0
        runtime.error("Period and multiplier must be greater than 0")
    var int p = math.max(1, period), var int head = 0, var int count = 0
    var array<float> tr_buffer = array.new_float(p, na)
    var float tr_sum = 0.0
    float high_val = nz(high), float low_val = nz(low), float close_val = nz(source)
    float prev_close = nz(source[1], source)
    float tr = math.max(high_val - low_val, math.max(math.abs(high_val - prev_close), math.abs(low_val - prev_close)))
    float oldest_tr = array.get(tr_buffer, head)
    if not na(oldest_tr)
        tr_sum -= oldest_tr
        count -= 1
    tr_sum += tr
    count += 1
    array.set(tr_buffer, head, tr)
    head := (head + 1) % p
    float atr = count > 0 ? tr_sum / count : tr
    float hl2_val = (high_val + low_val) / 2
    float basic_upper = hl2_val + multiplier * atr
    float basic_lower = hl2_val - multiplier * atr
    var float final_upper = na, var float final_lower = na
    var int trend = 1
    
    // Initialize on first bar
    if bar_index == 0
        final_upper := basic_upper
        final_lower := basic_lower
        trend := 1
    else
        prev_upper = nz(final_upper[1], basic_upper)
        prev_lower = nz(final_lower[1], basic_lower)
        prev_close_val = nz(source[1], source)
        
        final_upper := basic_upper < prev_upper or prev_close_val > prev_upper ? basic_upper : prev_upper
        final_lower := basic_lower > prev_lower or prev_close_val < prev_lower ? basic_lower : prev_lower
        
        prev_trend = nz(trend[1], 1)
        trend := close_val <= prev_lower ? 1 : close_val >= prev_upper ? -1 : prev_trend
    
    [final_upper, final_lower, trend]

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

// Inputs
i_period = input.int(10, "ATR Period", minval=1)
i_source = input.source(close, "Source")
i_multiplier = input.float(3.0, "ATR Multiplier", minval=0.001)

// Calculation
[upper_band, lower_band, trend] = stbands(i_source, i_period, i_multiplier)

// Plot
p_upper = plot(upper_band, "Upper Band", color=color.yellow, linewidth=2)
p_lower = plot(lower_band, "Lower Band", color=color.yellow, linewidth=2)
fill(p_upper, p_lower, color=color.new(color.blue, 90), title="Band Fill")

Overview

A rolling buffer keeps the true-range sum over the chosen period, and the bands only tighten until the source closes beyond the previous band. It plots both bands on the price chart and fills the area between them. Period, source, and ATR multiplier are adjustable. The computed trend direction is not plotted.

Details

Published by

mihakralj on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Volatility & bands

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_period
Sourcesourceclosei_source
ATR Multiplierfloat3.0i_multiplier
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

Chandelier Exit

by everget
948 stars

This indicator plots an ATR-based Chandelier Exit trailing stop on the price chart. It tracks a long stop under recent highs and a short stop above recent lows, then switches direction when price crosses the opposite stop. Buy and sell labels mark those flips.

Indicator v6 Volatility & bandsTrend Repaints
Updated 10 days ago · GPL-3.0 GitHub repository of Chandelier Exit

The Momentum Setup is a multi-indicator visual tool designed for trend and momentum analysis directly on the price chart. It combines Relative Strength Index (RSI), Money Flow Index (MFI), Commodity Channel Index (CCI), Supertrend, and a 250-bar percentage cha

Indicator v5 Oscillators & momentumTrend
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup - Ankush Bajaj Momentum Investing Setup
330 stars

Range Filter [Alorse] is a technical indicator overlay for TradingView designed to filter market volatility and identify price trends. It calculates a smoothed range based on historical volatility using a configurable sampling period and multiplier applied to

Indicator v5 TrendVolatility & bands
Updated 2 months ago · None stated GitHub repository of Range Filter [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon

[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

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