OPEN-SOURCE SCRIPT

EMA Crossover Strategy

This script calculates the 9, 21, and 50-period EMAs, plots them on the chart, and generates buy signals when the 9 EMA crosses above the 21 EMA while being above the 50 EMA, and sell signals when the 9 EMA crosses below the 21 EMA while being below the 50 EMA. You can customize the colors and styles as needed. ```pinescript //version=5 indicator("EMA Crossover Strategy", overlay=true)

// Define the EMAs ema9 = ta.ema(close, 9) ema21 = ta.ema(close, 21) ema50 = ta.ema(close, 50)

// Plot the EMAs plot(ema9, color=color.blue, title="EMA 9") plot(ema21, color=color.red, title="EMA 21") plot(ema50, color=color.green, title="EMA 50")

// Define buy and sell conditions buySignal = ta.crossover(ema9, ema21) and ema9 > ema50 sellSignal = ta.crossunder(ema9, ema21) and ema9 < ema50

// Plot buy and sell signals plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy") plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")

// Optional: Add alerts for buy and sell signals alertcondition(buySignal, title="Buy Alert", message="Buy Signal: EMA 9 crossed above EMA 21") alertcondition(sellSignal, title="Sell Alert", message="Sell Signal: EMA 9 crossed below EMA 21") pinescript //version=5 indicator("EMA Crossover Strategy", overlay=true)

// Define the EMAs ema9 = ta.ema(close, 9) ema21 = ta.ema(close, 21) ema50 = ta.ema(close, 50)

// Plot the EMAs plot(ema9, color=color.blue, title="EMA 9") plot(ema21, color=color.red, title="EMA 21") plot(ema50, color=color.green, title="EMA 50")

// Define buy and sell conditions buySignal = ta.crossover(ema9, ema21) and ema9 > ema50 sellSignal = ta.crossunder(ema9, ema21) and ema9 < ema50

// Plot buy and sell signals plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="Buy") plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="Sell")

// Optional: Add alerts for buy and sell signals alertcondition(buySignal, title="Buy Alert", message="Buy Signal: EMA 9 crossed above EMA 21") alertcondition(sellSignal, title="Sell Alert", message="Sell Signal: EMA 9 crossed below EMA 21")

// Additional feature: Highlight the background during buy/sell signals bgcolor(buySignal ? color.new(color.green, 90) : na, title="Buy Background") bgcolor(sellSignal ? color.new(color.red, 90) : na, title="Sell Background")
Candlestick analysis

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in publication is governed by House rules. You can favorite it to use it on a chart.

Want to use this script on a chart?

Disclaimer