OPEN-SOURCE SCRIPT
EMA 9/21 Crossover + ATR Stops (v5)

//version=5
indicator("EMA 9/21 Crossover + ATR Stops (v5)", overlay=true)
emaFastLen = input.int(9, "EMA Fast")
emaSlowLen = input.int(21, "EMA Slow")
atrLen = input.int(14, "ATR Length")
slMult = input.float(1.0, "Stop x ATR", step=0.1)
tpMult = input.float(2.0, "Target x ATR", step=0.1)
emaF = ta.ema(close, emaFastLen)
emaS = ta.ema(close, emaSlowLen)
atr = ta.atr(atrLen)
longSignal = ta.crossover(emaF, emaS)
shortSignal = ta.crossunder(emaF, emaS)
// Optional guide levels for next bar after signal
longSL = longSignal ? close - atr * slMult : na
longTP = longSignal ? close + atr * tpMult : na
shortSL = shortSignal ? close + atr * slMult : na
shortTP = shortSignal ? close - atr * tpMult : na
plot(emaF, "EMA Fast", color=color.new(color.teal, 0))
plot(emaS, "EMA Slow", color=color.new(color.orange, 0))
plot(longSL, "Long SL", color=color.new(color.red, 0), style=plot.style_linebr)
plot(longTP, "Long TP", color=color.new(color.lime, 0), style=plot.style_linebr)
plot(shortSL, "Short SL", color=color.new(color.red, 0), style=plot.style_linebr)
plot(shortTP, "Short TP", color=color.new(color.lime, 0), style=plot.style_linebr)
plotshape(longSignal, title="Long", text="Buy", style=shape.labelup, color=color.new(color.lime, 0), location=location.belowbar)
plotshape(shortSignal, title="Short", text="Sell", style=shape.labeldown, color=color.new(color.red, 0), location=location.abovebar)
// Alerts
alertcondition(longSignal, title="EMA Long", message="EMA 9/21 LONG crossover")
alertcondition(shortSignal, title="EMA Short", message="EMA 9/21 SHORT crossunder")
indicator("EMA 9/21 Crossover + ATR Stops (v5)", overlay=true)
emaFastLen = input.int(9, "EMA Fast")
emaSlowLen = input.int(21, "EMA Slow")
atrLen = input.int(14, "ATR Length")
slMult = input.float(1.0, "Stop x ATR", step=0.1)
tpMult = input.float(2.0, "Target x ATR", step=0.1)
emaF = ta.ema(close, emaFastLen)
emaS = ta.ema(close, emaSlowLen)
atr = ta.atr(atrLen)
longSignal = ta.crossover(emaF, emaS)
shortSignal = ta.crossunder(emaF, emaS)
// Optional guide levels for next bar after signal
longSL = longSignal ? close - atr * slMult : na
longTP = longSignal ? close + atr * tpMult : na
shortSL = shortSignal ? close + atr * slMult : na
shortTP = shortSignal ? close - atr * tpMult : na
plot(emaF, "EMA Fast", color=color.new(color.teal, 0))
plot(emaS, "EMA Slow", color=color.new(color.orange, 0))
plot(longSL, "Long SL", color=color.new(color.red, 0), style=plot.style_linebr)
plot(longTP, "Long TP", color=color.new(color.lime, 0), style=plot.style_linebr)
plot(shortSL, "Short SL", color=color.new(color.red, 0), style=plot.style_linebr)
plot(shortTP, "Short TP", color=color.new(color.lime, 0), style=plot.style_linebr)
plotshape(longSignal, title="Long", text="Buy", style=shape.labelup, color=color.new(color.lime, 0), location=location.belowbar)
plotshape(shortSignal, title="Short", text="Sell", style=shape.labeldown, color=color.new(color.red, 0), location=location.abovebar)
// Alerts
alertcondition(longSignal, title="EMA Long", message="EMA 9/21 LONG crossover")
alertcondition(shortSignal, title="EMA Short", message="EMA 9/21 SHORT crossunder")
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.
Open-source script
In true TradingView spirit, the creator of this script has made it open-source, so that traders can review and verify its functionality. Kudos to the author! While you can use it for free, remember that republishing the code is subject to our House Rules.
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.