strategy · on price · open source
SMA Crossover
Running…
Loading chart…
About
The textbook golden/death cross strategy: long when a fast SMA crosses above a slow SMA, exit on the reverse. Slow equal-weight averages smooth through noise better than EMAs but lag more.
- 50/200 is the iconic daily pair; 20/50 for shorter timeframes.
- Higher
Slowmeans bigger swings and fewer trades. - Combine with a volume filter for cleaner breakouts.
Source code
//@version=5
strategy("SMA Crossover", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
fast = input.int(50, "Fast SMA", minval=5, maxval=100, step=1)
slow = input.int(200, "Slow SMA", minval=20, maxval=400, step=1)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
fastS = ta.sma(close, fast)
slowS = ta.sma(close, slow)
longSignal = strategy.position_size == 0 and ta.crossover(fastS, slowS)
exitSignal = strategy.position_size > 0 and ta.crossunder(fastS, slowS)
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
strategy.close("Long", comment="SMA cross down")
p_Fast_SMA = plot(fastS, "Fast SMA", color=color.teal, linewidth=2)
p_Slow_SMA = plot(slowS, "Slow SMA", color=color.navy, linewidth=2)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "SMA long entry", "Fast SMA crossed above slow SMA")
alertcondition(exitSignal, "SMA exit", "Fast SMA crossed below slow SMA")
No comments yet.
Sign in to comment.