OPEN-SOURCE SCRIPT

Supertrend with Buy/Sell Signals

//version=5
indicator("Supertrend with Buy/Sell Signals", overlay=true)

// Input parameters
atrPeriod = input(10, title="ATR Period")
factor = input.float(3.0, title="Factor")

// Calculate ATR
atr = ta.atr(atrPeriod)

// Calculate Supertrend
upperBand = hl2 + (factor * atr)
lowerBand = hl2 - (factor * atr)

var float supertrend = na
var int direction = na

if (na(supertrend))
supertrend := close
direction := 1
else
if (close > supertrend)
supertrend := lowerBand < supertrend or direction == -1 ? lowerBand : supertrend
else
supertrend := upperBand > supertrend or direction == 1 ? upperBand : supertrend

direction := close > supertrend ? 1 : -1

// Plot Supertrend
plot(supertrend, color=direction == 1 ? color.green : color.red, linewidth=2, title="Supertrend")

// Generate Buy/Sell signals
buySignal = ta.crossover(close, supertrend)
sellSignal = ta.crossunder(close, supertrend)

plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", title="Buy Signal")
plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", title="Sell Signal")

إخلاء المسؤولية