OPEN-SOURCE SCRIPT

5/20 SMA Trade Signal

63
//version=5
indicator("5/20 SMA Trade Signal", overlay=true)

// Inputs (editable if you want to tweak later)
fastLen = input.int(5, "Fast SMA (default 5)", minval=1)
slowLen = input.int(20, "Slow SMA (default 20)", minval=1)

// Calculate SMAs
fastSMA = ta.sma(close, fastLen)
slowSMA = ta.sma(close, slowLen)

// Plot SMAs
plot(fastSMA, title="Fast SMA", color=color.orange, linewidth=2)
plot(slowSMA, title="Slow SMA", color=color.blue, linewidth=2)

// Signals (use bar close confirmation to avoid intrabar repaint)
bullCross = ta.crossover(fastSMA, slowSMA) and barstate.isconfirmed // 5 SMA closes ABOVE 20 SMA
bearCross = ta.crossunder(fastSMA, slowSMA) and barstate.isconfirmed // 5 SMA closes BELOW 20 SMA

// Label both as "TRADE SIGNAL"
plotshape(bullCross, title="TRADE SIGNAL (5 > 20)", style=shape.labelup, location=location.belowbar,
text="TRADE SIGNAL", color=color.new(color.green, 0), textcolor=color.white, size=size.tiny)
plotshape(bearCross, title="TRADE SIGNAL (5 < 20)", style=shape.labeldown, location=location.abovebar,
text="TRADE SIGNAL", color=color.new(color.red, 0), textcolor=color.white, size=size.tiny)

// Optional alerts for automation/webhooks
alertcondition(bullCross, title="5 SMA crossed ABOVE 20 SMA",
message="TRADE SIGNAL: 5 SMA crossed ABOVE 20 SMA on {{ticker}} @ {{close}}")
alertcondition(bearCross, title="5 SMA crossed BELOW 20 SMA",
message="TRADE SIGNAL: 5 SMA crossed BELOW 20 SMA on {{ticker}} @ {{close}}")

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.