Strategy vault / Trend following
EMA Double Moving Average Crossover Strategy
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…

Overview
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.
Strategy Principle
- Calculate the 20-day EMA, 50-day EMA, and 200-day EMA.
- Determine the crossover conditions of the 20-day EMA and 50-day EMA:
- When the 20-day EMA crosses above the 50-day EMA, a buy signal is generated.
- When the 20-day EMA crosses below the 50-day EMA, a sell signal is generated.
- Plot the 20-day EMA (green), 50-day EMA (red), and 200-day EMA (blue) on the chart for visual observation of their trends and crossovers.
- Mark the corresponding buy (green upward triangle) and sell (red downward triangle) signals on the chart when they occur.
Strategy Advantages
- Simplicity: The strategy is based on the simple principle of moving average crossovers, making it easy to understand and implement.
- Trend Following: By utilizing the crossover of short-term and long-term moving averages, the strategy can effectively capture shifts in market trends, making it suitable for trending markets.
- Long-term Trend Reference: The inclusion of the 200-day EMA provides a reference for the long-term market environment.
- Visual Representation: The strategy clearly plots the moving averages and buy/sell signals on the chart, facilitating easy observation and analysis for traders.
Strategy Risks
- Choppy Markets: In choppy markets, frequent moving average crossovers may generate numerous false signals, resulting in suboptimal performance.
- Lag: Moving averages have an inherent lag, potentially missing the optimal timing of market reversals.
- Parameter Sensitivity: The strategy's performance depends on the choice of moving average periods, and different parameter combinations may lead to varying results.
Strategy Optimization Directions
- Incorporating Additional Indicators: Consider incorporating other technical indicators, such as RSI or MACD, to improve signal reliability and accuracy.
- Parameter Optimization: Optimize the moving average period parameters to find the most suitable combination for the current market conditions.
- Implementing Stop-Loss and Take-Profit: Incorporate reasonable stop-loss and take-profit mechanisms to control risk and profitability on individual trades.
- Trend Confirmation: Filter trading signals based on the direction of the long-term trend (e.g., 200-day EMA) and only trade in the direction of the trend.
Summary
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.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_int_1 | 20 | Short MA Length |
| v_input_int_2 | 50 | Long MA Length |
| v_input_int_3 | 200 | Long MA 200 Length |
Source Pine Script
/*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")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-03-29. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/EMA-Double-Moving-Average-Crossover-Strategy.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.
Related specs
- Adaptive ATR and RSI Trend Following Strategy with Trailing Stop LossPine Script · Trend following · 1h
- Advanced Multi Indicator Multi Dimensional Trend Cross Quantitative StrategyPine Script · Trend following · 3h
- Bollinger Bands and RSI Trend Following StrategyPine Script · Trend following · 1h
- Dual ATR Channel Trend Following StrategyPine Script · Trend following · 15m
- Dynamic Dual EMA Crossover Strategy with Adaptive Profit Loss ControlPine Script · Trend following · 1d
- EMA SMA Multi Indicator Comprehensive Trend Following StrategyPine Script · Trend following · 1h