
This strategy generates trading signals based on the crossover of two moving averages (EMA). When the short-term EMA (20-day) crosses above the long-term EMA (50-day), a buy signal is triggered; when the short-term EMA crosses below the long-term EMA, a sell signal is triggered. Additionally, the strategy plots a 200-day EMA as a reference for the long-term trend. The main idea behind this strategy is to capture shifts in market trends by utilizing the crossover of moving averages with different periods.
The EMA Double Moving Average Crossover Strategy is a simple and straightforward trading strategy suitable for trending markets. It utilizes the crossover of short-term and long-term moving averages to capture shifts in market trends while incorporating a long-term trend reference. Although the strategy has some limitations, such as suboptimal performance in choppy markets and the lag of moving averages, it can be further enhanced by incorporating additional indicators, optimizing parameters, implementing risk management measures, and confirming trends. These optimizations can improve the strategy’s robustness and profitability.
/*backtest
start: 2023-03-23 00:00:00
end: 2024-03-28 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("EMA Crossover Strategy by Peter Gangmei", overlay=true)
// Define the length for moving averages
short_ma_length = input.int(20, "Short MA Length")
long_ma_length = input.int(50, "Long MA Length")
long_ma_200_length = input.int(200, "Long MA 200 Length")
// Define start time for testing
start_time = timestamp(2024, 01, 01, 00, 00)
// Calculate current date and time
current_time = timenow
// Calculate moving averages
ema20 = ta.ema(close, short_ma_length)
ema50 = ta.ema(close, long_ma_length)
ema200 = ta.ema(close, long_ma_200_length)
// Crossing conditions
crossed_above = ta.crossover(ema20, ema50)
crossed_below = ta.crossunder(ema20, ema50)
// Entry and exit conditions within the specified time frame
if true
if (crossed_above)
strategy.entry("Buy", strategy.long)
alert("Buy Condition", alert.freq_once_per_bar_close)
if (crossed_below)
strategy.entry("Sell", strategy.short)
alert("Sell Condition", alert.freq_once_per_bar_close)
// Plotting moving averages for visualization
plot(ema20, color=color.green, title="EMA20")
plot(ema50, color=color.red, title="EMA50")
plot(ema200, color=color.blue, title="EMA200")
// Placing buy and sell markers
plotshape(series=crossed_above, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(series=crossed_below, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")