Indicator · Pine Script v4 · On the chart

SuperTrend-original

by darkshloser· code credited to Alex Orekhov (everget)

A Pine v4 script that plots three SuperTrend trailing stop lines on the price chart, based on the well-known ATR and midpoint approach.

View on GitHub

GitHub stars1on the repository
Pine versionv4older, still runs
Lines of code214
Settings18inputs
LicenceApache-2.0
Last update2 years ago15 Jul 2024

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

214 lines of Pine Script v4 from theindicator.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

theindicator.pine
//@version=4
// Copyright (c) 2019-present, Alex Orekhov (everget)
// SuperTrend script may be freely distributed under the terms of the GPL-3.0 license.
study("SuperTrend-original", overlay=true)

length = input(title="ATR Period", type=input.integer, defval=12)
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3)
src = input(title="Source", type=input.source, defval=hl2)
wicks = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
highlightState = input(title="Highlight State ?", type=input.bool, defval=true)

atr = mult * atr(length)

highPrice = wicks ? high : close
lowPrice = wicks ? low : close
doji4price = open == close and open == low and open == high

longStop = src - atr
longStopPrev = nz(longStop[1], longStop)

if longStop > 0
    if doji4price
        longStop := longStopPrev
    else
        longStop := (lowPrice[1] > longStopPrev ? max(longStop, longStopPrev) : longStop)
else
    longStop := longStopPrev

shortStop = src + atr
shortStopPrev = nz(shortStop[1], shortStop)

if shortStop > 0
    if doji4price
        shortStop := shortStopPrev
    else
        shortStop := (highPrice[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop)
else
    shortStop := shortStopPrev

var int dir = 1
dir :=
         dir == -1 and highPrice > shortStopPrev ? 1 :
         dir == 1 and lowPrice < longStopPrev ? -1 :
         dir

var color longColor = color.green
var color shortColor = color.red

longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
buySignal = dir == 1 and dir[1] == -1
plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)

shortStopPlot = plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
sellSignal = dir == -1 and dir[1] == 1
plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)

midPricePlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none, editable=false)

longFillColor = highlightState ? (dir == 1 ? longColor : na) : na
shortFillColor = highlightState ? (dir == -1 ? shortColor : na) : na
fill(midPricePlot, longStopPlot, title="Long State Filling", color=longFillColor)
fill(midPricePlot, shortStopPlot, title="Short State Filling", color=shortFillColor)

// changeCond = dir != dir[1]
// alertcondition(changeCond, title="Alert: SuperTrend Direction Change", message="SuperTrend has changed direction!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
// alertcondition(buySignal, title="Alert: SuperTrend Buy", message="SuperTrend Buy!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
// alertcondition(sellSignal, title="Alert: SuperTrend Sell", message="SuperTrend Sell!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")

// ===========================

length1 = input(title="ATR Period", type=input.integer, defval=10)
mult1 = input(title="ATR Multiplier", type=input.float, step=0.1, defval=1)
src1 = input(title="Source", type=input.source, defval=hl2)
wicks1 = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
showLabels1 = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
highlightState1 = input(title="Highlight State ?", type=input.bool, defval=true)

atr1 = mult1 * atr(length1)

highPrice1 = wicks1 ? high : close
lowPrice1 = wicks1 ? low : close
doji4price1 = open == close and open == low and open == high

longStop1 = src1 - atr1
longStopPrev1 = nz(longStop1[1], longStop1)

if longStop1 > 0
    if doji4price1
        longStop1 := longStopPrev1
    else
        longStop1 := (lowPrice1[1] > longStopPrev1 ? max(longStop1, longStopPrev1) : longStop1)
else
    longStop1 := longStopPrev1

shortStop1 = src1 + atr1
shortStopPrev1 = nz(shortStop1[1], shortStop1)

if shortStop1 > 0
    if doji4price1
        shortStop1 := shortStopPrev1
    else
        shortStop1 := (highPrice1[1] < shortStopPrev1 ? min(shortStop1, shortStopPrev1) : shortStop1)
else
    shortStop1 := shortStopPrev1

var int dir1 = 1
dir1 :=
         dir1 == -1 and highPrice1 > shortStopPrev1 ? 1 :
         dir1 == 1 and lowPrice1 < longStopPrev1 ? -1 :
         dir1

var color longColor1 = color.green
var color shortColor1 = color.red

longStopPlot1 = plot(dir1 == 1 ? longStop1 : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor1)
buySignal1 = dir1 == 1 and dir1[1] == -1
plotshape(buySignal1 ? longStop1 : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor1, transp=0)
plotshape(buySignal1 and showLabels1 ? longStop1 : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor1, textcolor=color.white, transp=0)

shortStopPlot1 = plot(dir1 == 1 ? na : shortStop1, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor1)
sellSignal1 = dir1 == -1 and dir1[1] == 1
plotshape(sellSignal1 ? shortStop1 : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor1, transp=0)
plotshape(sellSignal1 and showLabels1 ? shortStop1 : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor1, textcolor=color.white, transp=0)

midPricePlot1 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none, editable=false)

longFillColor1 = highlightState1 ? (dir1 == 1 ? longColor1 : na) : na
shortFillColor1 = highlightState1 ? (dir1 == -1 ? shortColor1 : na) : na
fill(midPricePlot1, longStopPlot1, title="Long State Filling", color=longFillColor1)
fill(midPricePlot1, shortStopPlot1, title="Short State Filling", color=shortFillColor1)

// changeCond1 = dir1 != dir1[1]
// alertcondition(changeCond1, title="Alert: SuperTrend Direction Change", message="SuperTrend has changed direction!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
// alertcondition(buySignal1, title="Alert: SuperTrend Buy", message="SuperTrend Buy!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
// alertcondition(sellSignal1, title="Alert: SuperTrend Sell", message="SuperTrend Sell!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")



// ===========================

length2 = input(title="ATR Period", type=input.integer, defval=11)
mult2 = input(title="ATR Multiplier", type=input.float, step=0.1, defval=2)
src2 = input(title="Source", type=input.source, defval=hl2)
wicks2 = input(title="Take Wicks into Account ?", type=input.bool, defval=true)
showLabels2 = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
highlightState2 = input(title="Highlight State ?", type=input.bool, defval=true)

atr2 = mult2 * atr(length2)

highPrice2 = wicks2 ? high : close
lowPrice2 = wicks2 ? low : close
doji4price2 = open == close and open == low and open == high

longStop2 = src2 - atr2
longStopPrev2 = nz(longStop2[1], longStop2)

if longStop2 > 0
    if doji4price2
        longStop2 := longStopPrev2
    else
        longStop2 := (lowPrice2[1] > longStopPrev2 ? max(longStop2, longStopPrev2) : longStop2)
else
    longStop2 := longStopPrev2

shortStop2 = src2 + atr2
shortStopPrev2 = nz(shortStop2[1], shortStop2)

if shortStop2 > 0
    if doji4price2
        shortStop2 := shortStopPrev2
    else
        shortStop2 := (highPrice2[1] < shortStopPrev2 ? min(shortStop2, shortStopPrev2) : shortStop2)
else
    shortStop2 := shortStopPrev2

var int dir2 = 1
dir2 :=
         dir2 == -1 and highPrice2 > shortStopPrev2 ? 1 :
         dir2 == 1 and lowPrice2 < longStopPrev2 ? -1 :
         dir2

var color longColor2 = color.green
var color shortColor2 = color.red

longStopPlot2 = plot(dir2 == 1 ? longStop2 : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor2)
buySignal2 = dir2 == 1 and dir2[1] == -1
plotshape(buySignal2 ? longStop2 : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor2, transp=0)
plotshape(buySignal2 and showLabels2 ? longStop2 : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor2, textcolor=color.white, transp=0)

shortStopPlot2 = plot(dir2 == 1 ? na : shortStop2, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor2)
sellSignal2 = dir2 == -1 and dir2[1] == 1
plotshape(sellSignal2 ? shortStop2 : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor2, transp=0)
plotshape(sellSignal2 and showLabels2 ? shortStop2 : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor2, textcolor=color.white, transp=0)

midPricePlot2 = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none, editable=false)

longFillColor2 = highlightState2 ? (dir2 == 1 ? longColor2 : na) : na
shortFillColor2 = highlightState2 ? (dir2 == -1 ? shortColor2 : na) : na
fill(midPricePlot2, longStopPlot2, title="Long State Filling", color=longFillColor2)
fill(midPricePlot2, shortStopPlot2, title="Short State Filling", color=shortFillColor2)

changeCond2 = dir2 != dir2[1]
alertcondition(changeCond2, title="Alert: SuperTrend Direction Change", message="SuperTrend has changed direction!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
alertcondition(buySignal2, title="Alert: SuperTrend Buy", message="SuperTrend Buy!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")
alertcondition(sellSignal2, title="Alert: SuperTrend Sell", message="SuperTrend Sell!\nSymbol: {{exchange}}:{{ticker}}\nPrice: {{close}}")

// ======================= EMA 200

longest = ema(close, 200)
var color emaColor = color.blue
plot(longest, color = emaColor)

Overview

Each line has its own ATR period, multiplier, source and wicks-or-close option. A green line marks an up direction and a red line a down direction, with circles and optional Buy and Sell labels where the direction flips and optional shaded state fills. Signals appear on bar close of a direction change.

Details

Published by

darkshloser on GitHub

Code credit

Alex Orekhov (everget), as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Trend

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.

Settings

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

SettingTypeDefaultVariable
ATR Periodint12length
ATR Multiplierfloat3mult
Sourcesourcehl2src
Take Wicks into Account ?booltruewicks
Show Buy/Sell Labels ?booltrueshowLabels
Highlight State ?booltruehighlightState
ATR Periodint10length1
ATR Multiplierfloat1mult1
Sourcesourcehl2src1
Take Wicks into Account ?booltruewicks1
Show Buy/Sell Labels ?booltrueshowLabels1
Highlight State ?booltruehighlightState1
Show all 18 settings
SettingTypeDefaultVariable
ATR Periodint11length2
ATR Multiplierfloat2mult2
Sourcesourcehl2src2
Take Wicks into Account ?booltruewicks2
Show Buy/Sell Labels ?booltrueshowLabels2
Highlight State ?booltruehighlightState2
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

947 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 12 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
575 stars

This indicator detects equal highs and lows by comparing prices within user-defined left and right bars using a tolerance percentage. It plots horizontal lines on the price chart for equal highs in one color and lows in another. A screener allows input of mult

Indicator v5 Support & resistanceTrend
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows with Screener
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [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

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns

More Trend scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers