OPEN-SOURCE SCRIPT

Supertrend with Trend Change Arrows

113
//version=6
indicator("Supertrend with Trend Change Arrows", overlay = true, timeframe = "", timeframe_gaps = true)

// Inputs
atrPeriod = input.int(10, "ATR Length", minval = 1)
factor = input.float(3.0, "Factor", minval = 0.01, step = 0.01)

// Calculate Supertrend
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

// Plot Supertrend
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style = plot.style_linebr)
downTrend = plot(direction < 0 ? na : supertrend, "Down Trend", color = color.red, style = plot.style_linebr)

// Fill Backgrounds
bodyMiddle = plot((open + close) / 2, "Body Middle", display = display.none)
fill(bodyMiddle, upTrend, title = "Uptrend background", color = color.new(color.green, 90), fillgaps = false)
fill(bodyMiddle, downTrend, title = "Downtrend background", color = color.new(color.red, 90), fillgaps = false)

// Detect Trend Changes
bullishFlip = direction < 0 and direction[1] >= 0 // Trend flipped from bearish to bullish
bearishFlip = direction >= 0 and direction[1] < 0 // Trend flipped from bullish to bearish

// Plot Arrows for Trend Changes
plotshape(bullishFlip, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, text="▲", offset=0)
plotshape(bearishFlip, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, text="▼", offset=0)

// Alert Conditions
alertcondition(bullishFlip, title='Downtrend to Uptrend', message='The Supertrend value switched from Downtrend to Uptrend')
alertcondition(bearishFlip, title='Uptrend to Downtrend', message='The Supertrend value switched from Uptrend to Downtrend')
alertcondition(bullishFlip or bearishFlip, title='Trend Change', message='The Supertrend value switched from Uptrend to Downtrend or vice versa')

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.