Indicator · Pine Script v5 · On the chart

theIndicator v2

by darkshloser· code credited to Author: dkovache

A refactored version of a triple SuperTrend overlay.

View on GitHub

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

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

theindicator-v2.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-v2", overlay=true)


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



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

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

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

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

src = input(hl2, "Super Trend source")


averageTrueRange(multiplier, atrPeriod) =>
    result = multiplier * ta.atr(atrPeriod)
    result

highPrice(wicks) =>
    result = wicks ? high : close
    result

lowPrice(wicks) => 
    result = wicks ? low : close
    result

dojiCandle() =>
    result = open == close and open == low and open == high
    result

longStop(src, averageTrueRange) =>
    result = src - averageTrueRange
    result

longStopPrev(longStop) =>
    result = nz(longStop[1], longStop)
    result

updateLongStop(src, wicks, multiplier, atrPeriod) =>
    atrValue = averageTrueRange(multiplier, atrPeriod)
    currentLongStop = longStop(src, atrValue)
    prevLongStop = longStopPrev(currentLongStop)
    if currentLongStop > 0
        if dojiCandle()
            prevLongStop
        else
            lowPrice1 = lowPrice(wicks)[1]
            result = (lowPrice1 > prevLongStop ? math.max(currentLongStop, prevLongStop) : currentLongStop)
            result
    else
        prevLongStop


shortStop(src, averageTrueRange) =>
    result = src + averageTrueRange
    result

shortStopPrev(shortStop) =>
    result = nz(shortStop[1], shortStop)
    result

updateShortStop(src, wicks, multiplier, atrPeriod) =>
    atrValue = averageTrueRange(multiplier, atrPeriod)
    currentShortStop = shortStop(src, atrValue)
    prevShortStop = shortStopPrev(currentShortStop)
    if currentShortStop > 0
        if dojiCandle()
            prevShortStop
        else
            highPrice1 = highPrice(wicks)[1]
            result = (highPrice1 < prevShortStop ? math.min(currentShortStop, prevShortStop) : currentShortStop)
            result
    else
        prevShortStop


// Function to calculate direction
calculateDirection(dir, highPrice, lowPrice, shortStopPrev, longStopPrev) =>
    dir_tmp = dir == -1 and highPrice > shortStopPrev ? 1 :
           dir == 1 and lowPrice < longStopPrev ? -1 :
           dir
    dir_tmp

// ============================================================== 1st ST indicator ===================================
var int dir1 = 1
at = averageTrueRange(st1_mult, st1_length)
hPrice = highPrice(st1_wicks)
lPrice = lowPrice(st1_wicks)
dCandle = dojiCandle()
lStop = updateLongStop(src,st1_wicks, st1_mult, st1_length)
shStop = updateShortStop(src,st1_wicks, st1_mult, st1_length)
lStopPrev = longStopPrev(lStop)
== 1 ? lStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
shortStopPlot = plot(dir1 == 1 ? na : shStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
direction1 = dir1

// ============================================================== 2nd ST indicator ===================================
var int dir2 = 1
at := averageTrueRange(st2_mult, st2_length)
hPrice := highPrice(st2_wicks)
lPrice := lowPrice(st2_wicks)
dCandle := dojiCandle()
lStop := updateLongStop(src, st2_wicks, st2_mult, st2_length)
shStop := updateShortStop(src,st2_wicks, st2_mult, st2_length)
lStopPrev := longStopPrev(lStop)
shStopPrev := shortStopPrev(shStop)
dir2 := calculateDirection(dir2, hPrice, lPrice, shStopPrev, lStopPrev)

longStopPlot := plot(dir2 == 1 ? lStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
shortStopPlot := plot(dir2 == 1 ? na : shStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
direction2 = dir2

// ============================================================== 3nd ST indicator ===================================
var int dir3 = 1
at := averageTrueRange(st3_mult, st3_length)
hPrice := highPrice(st3_wicks)
lPrice := lowPrice(st3_wicks)
dCandle := dojiCandle()
lStop := updateLongStop(src, st3_wicks, st3_mult, st3_length)
shStop := updateShortStop(src, st3_wicks, st3_mult, st3_length)
lStopPrev := longStopPrev(lStop)
shStopPrev := shortStopPrev(shStop)
dir3 := calculateDirection(dir3, hPrice, lPrice, shStopPrev, lStopPrev)

longStopPlot := plot(dir3 == 1 ? lStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
shortStopPlot := plot(dir3 == 1 ? na : shStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
direction3 = dir3

// ================================================  EMA 200 =========================================================
longest = ta.ema(close, 200)
var color emaColor = color.yellow
plot(longest, color = emaColor)


// ================================================ Analizes =========================================================

// Analize main timeframe 
var activePosition = false
var activeColor = color.white
var activeColorPrevState = color.white
var isOpenPosition = false
var isPositionLong = false

activeColorPrevState := activeColor
if direction1 > 0 and direction2 > 0 and direction3 > 0
    activeColor := color.red
    activePosition := true
else if direction1 < 0 and direction2 < 0 and direction3 < 0
    activeColor := color.green
    activePosition := true
else
    activeColor := color.white
    activePosition := false


// Analyze second timeframe 
// second_timeframe
[trend1_second, direction1_second] = request.security(syminfo.tickerid, str.tostring(second_timeframe), ta.supertrend(st1_mult, st1_length))
[trend2_second, direction2_second] = request.security(syminfo.tickerid, str.tostring(second_timeframe), ta.supertrend(st2_mult, st2_length))
[trend3_second, direction3_second] = request.security(syminfo.tickerid, str.tostring(second_timeframe), ta.supertrend(st3_mult, st3_length))

var underlyingLong = false
var activePositionSecond = false


if direction1_second > 0 and direction2_second > 0 and direction3_second > 0
    underlyingLong := false
    activePositionSecond := true
else if direction1_second < 0 and direction2_second < 0 and direction3_second < 0
    underlyingLong := true
    activePositionSecond := true
else
    activePositionSecond := false
    underlyingLong := false


// ================================================ ALERTS =========================================================

// Open and Close positions
if activePosition == false and isOpenPosition == true and isPositionLong == false
    isOpenPosition := false
    alert("Close SHORT position on MAIN timeframe", alert.freq_once_per_bar)

if activePosition == false and isOpenPosition == true and isPositionLong == true
    isOpenPosition := false
    alert("Close LONG position on MAIN timeframe", alert.freq_once_per_bar)

if activePosition == true and activePositionSecond == true and activeColor == color.red and underlyingLong == false and isOpenPosition == false
    isOpenPosition := true
    isPositionLong := false
    alert("Open SHORT position on MAIN timeframe", alert.freq_once_per_bar)

if activePosition == true and activePositionSecond == true and activeColor == color.green and underlyingLong == true and isOpenPosition == false
    isOpenPosition := true
    isPositionLong := true
    alert("Open LONG position on MAIN timeframe", alert.freq_once_per_bar)


// Underlying deviations from active position
if isOpenPosition
    if isPositionLong == false and direction1_second <= 0 and direction2_second <= 0 and direction3_second > 0
        alert("Light underlying indication to close opned SHORT position")
    else if isPositionLong == false and direction1_second <= 0 and direction2_second <= 0 and direction3_second <= 0
        alert("Stronger underlying indication to close SHORT position")

    if isPositionLong == true and direction1_second >= 0 and direction2_second >= 0 and direction3_second < 0
        alert("Light underlying indication to close opned LONG position")
    else if isPositionLong == true and direction1_second >= 0 and direction2_second >= 0 and direction3_second >= 0
        alert("Stronger underlying indication to close LONG position")

Overview

Each of three lines is calculated through helper functions from an ATR period, multiplier and a shared price source, with an option to use wicks or closing prices. Long stops plot in green and short stops in red, switching when price crosses the trailing stop. A second timeframe input is included. It draws on the price chart and has no order or alert logic in the visible code.

Details

Published by

darkshloser on GitHub

Code credit

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

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 Multiplierfloat1.0st2_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
Super Trend sourcesourcehl2src
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