OPEN-SOURCE SCRIPT

EMA Crossover Strategy

//version=5
strategy("EMA Crossover Strategy", overlay=true)

// Define the length of EMAs
ema50Length = 50
ema200Length = 200

// Calculate the EMAs
ema50 = ta.ema(close, ema50Length)
ema200 = ta.ema(close, ema200Length)

// Plot the EMAs on the chart
plot(ema50, title="50 EMA", color=color.green, linewidth=2)
plot(ema200, title="200 EMA", color=color.red, linewidth=2)

// Define the crossover condition
longCondition = ta.crossover(ema50, ema200)
shortCondition = ta.crossunder(ema50, ema200)

// Generate alerts
alertcondition(longCondition, title="Buy Signal", message="50 EMA is above 200 EMA - Buy")
alertcondition(shortCondition, title="Sell Signal", message="50 EMA is below 200 EMA - Sell")

// Execute trades based on the conditions
if (longCondition)
strategy.entry("Buy", strategy.long)

if (shortCondition)
strategy.close("Buy")
Bands and ChannelsCandlestick analysisChart patterns

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