OPEN-SOURCE SCRIPT

MA Crossover Strategy V6

107
//version=6
strategy("MA Crossover Strategy V6", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

// === Inputs ===
shortLength = input.int(9, title="Short MA Length", minval=1)
longLength = input.int(21, title="Long MA Length", minval=1)
useEMA = input.bool(false, title="Use EMA Instead of SMA")

// === Moving Averages ===
shortMA = useEMA ? ta.ema(close, shortLength) : ta.sma(close, shortLength)
longMA = useEMA ? ta.ema(close, longLength) : ta.sma(close, longLength)

// === Plot MAs ===
plot(shortMA, color=color.orange, title="Short MA", linewidth=2)
plot(longMA, color=color.blue, title="Long MA", linewidth=2)

// === Entry Conditions ===
longCondition = ta.crossover(shortMA, longMA)
shortCondition = ta.crossunder(shortMA, longMA)

// === Strategy Logic ===
if (longCondition)
strategy.entry("Long", strategy.long)

if (shortCondition)
strategy.entry("Short", strategy.short)

// === Optional: Plot Buy/Sell Signals ===
plotshape(longCondition, title="Buy Signal", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(shortCondition, title="Sell Signal", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)

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.