Indicator · Pine Script v5 · On the chart

theIndicator v1.9

by darkshloser· code credited to Author: dkovache

Overlays several SuperTrend-style trailing stop lines on the price chart, each built from an ATR period, ATR multiplier and source, with a choice of using wicks or closes.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code187
Settings13inputs
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

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

theindicator-v1.9.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Author: dkovache

//@version=5
indicator("theIndicator-v1.9", overlay=true)


// Predefined variables
var longColor = color.green
var shortColor = color.red


st1_length = input.int(defval=12, title="ST1 ATR Period", minval=1)
st1_mult = input.float(defval=3.0, title="ST1 ATR Multiplier", step=0.1, minval=0.1)
st1_wicks = input(defval=true, title="Take supertrend1 wicks into account ?")

st2_length = input.int(defval=10, title="ST2 ATR Period", minval=1)
st2_mult = input.float(defval=1, title="ST2 ATR Multiplier", step=0.1, minval=0.1)
st2_wicks = input(defval=true, title="Take supertrend2 wicks into account ?")

st3_length = input.int(defval=11, title="ST3 ATR Period", minval=1)
st3_mult = input.float(defval=2.0, title="ST3 ATR Multiplier", step=0.1, minval=0.1)
st3_wicks = input(defval=true, title="Take supertrend3 wicks into account ?")

second_timeframe = input.int(defval=60, title="Second timeframe for analyze", step=10, minval=10)

src = input.source(defval=hl2, title="St1")
src2 = input.source(defval=hl2, title="St2")
src3 = input.source(defval=hl2, title="St3")
// src = input(title="Source", type=input.source, defval=hl2)

atr = st1_mult * ta.atr(st1_length)
highPrice = st1_wicks ? high : close
lowPrice = st1_wicks ? low : close
dojiCandle = open == close and open == low and open == high

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

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


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

if shortStop > 0
    if dojiCandle
        shortStop := shortStopPrev
    else
        shortStop := (highPrice[1] < shortStopPrev ? math.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

plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)


// ===========================
atr2 = st2_mult * ta.atr(st2_length)
highPrice2 = st2_wicks ? high : close
lowPrice2 = st2_wicks ? low : close
dojiCandle := open == close and open == low and open == high

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

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


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

if shortStop2 > 0
    if dojiCandle 
        shortStop2 := shortStopPrev2
    else
        shortStop2 := (highPrice2[1] < shortStopPrev2 ? math.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

plot(dir2 == 1 ? longStop2 : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
plot(dir2 == 1 ? na : shortStop2, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)


// ===========================
atr3 = st3_mult * ta.atr(st3_length)
highPrice3 = st3_wicks ? high : close
lowPrice3 = st3_wicks ? low : close
dojiCandle := open == close and open == low and open == high


longStop3 = src3 - atr3
longStopPrev3 = nz(longStop3[1], longStop3)

if longStop3 > 0
    if dojiCandle
        longStop3 := longStopPrev3
    else
        longStop3 := (lowPrice3[1] > longStopPrev3 ? math.max(longStop3, longStopPrev3) : longStop3)
else
    longStop3 := longStopPrev3


shortStop3 = src3 + atr3 
shortStopPrev3 = nz(shortStop3[1], shortStop3)

if shortStop3 > 0
    if dojiCandle 
        shortStop3 := shortStopPrev3
    else
        shortStop3 := (highPrice3[1] < shortStopPrev3 ? math.min(shortStop3, shortStopPrev3) : shortStop3)
else
    shortStop3 := shortStopPrev3

var int dir3 = 1
dir3 :=
         dir3 == -1 and highPrice3 > shortStopPrev3 ? 1 :
         dir3 == 1 and lowPrice3 < longStopPrev3 ? -1 :
         dir3


plot(dir3 == 1 ? longStop3 : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
plot(dir3 == 1 ? na : shortStop3, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)


// ===========================================  MAIN timeframe analyze =======================
var activePosition = false
var activeColor = color.blue
var activeColorPrevState = color.blue

var isOpenPosition = false
var isPositionLong = false

bool conditionEntered = false

// Update the status of the previouse bar
activeColorPrevState := activeColor


if dir > 0 and dir2 > 0 and dir3 > 0
    if not isOpenPosition
        activeColor := color.green
        activePosition := true
        isOpenPosition := true
        conditionEntered := true
    else
        conditionEntered := false
else if dir < 0 and dir2 < 0 and dir3 < 0
    if not isOpenPosition
        activeColor := color.red
        activePosition := true
        isOpenPosition := true
        conditionEntered := true
    else
        conditionEntered := false
else
    activeColor := color.blue
    activePosition := false
    isOpenPosition := false
    conditionEntered := false

plotshape(series=conditionEntered and activeColor == color.green, title="Signal", location=location.belowbar, color=longColor, style=shape.triangleup, size=size.small)
plotshape(series=conditionEntered and activeColor == color.red, title="Signal", location=location.abovebar, color=shortColor, style=shape.triangledown, size=size.small)

Overview

Lines are green while the direction is up and red while it is down, with a flip when price crosses the opposite stop. A second timeframe input is provided for higher timeframe analysis. It draws on the main chart and provides no separate order logic.

Details

Published by

darkshloser on GitHub

Code credit

Author: dkovache, as written in the script's header

Type

Indicator, Pine Script v5

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
ST1 ATR Periodint12st1_length
ST1 ATR Multiplierfloat3.0st1_mult
Take supertrend1 wicks into account ?booltruest1_wicks
ST2 ATR Periodint10st2_length
ST2 ATR Multiplierfloat1st2_mult
Take supertrend2 wicks into account ?booltruest2_wicks
ST3 ATR Periodint11st3_length
ST3 ATR Multiplierfloat2.0st3_mult
Take supertrend3 wicks into account ?booltruest3_wicks
Second timeframe for analyzeint60second_timeframe
St1sourcehl2src
St2sourcehl2src2
Show all 13 settings
SettingTypeDefaultVariable
St3sourcehl2src3
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

948 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 11 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 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