Strategy vault / Trend following
EMA Dual Moving Average Crossover Strategy
This strategy uses two exponential moving averages (EMAs) to capture changes in price trends. When the short-term EMA crosses above the long-term EMA from below, a buy signal is generated; when the short-term EMA…

Overview
This strategy uses two exponential moving averages (EMAs) to capture changes in price trends. When the short-term EMA crosses above the long-term EMA from below, a buy signal is generated; when the short-term EMA crosses below the long-term EMA from above, a sell signal is generated. The strategy also sets daily stop-loss and take-profit limits to control single-day losses and profits.
Strategy Principles
- Calculate the short-term EMA (default period of 9) and long-term EMA (default period of 21).
- When the short-term EMA crosses above the long-term EMA, open a long position; when the short-term EMA crosses below the long-term EMA, open a short position.
- Record the account equity at the start of each trading day and calculate the difference between the current account equity and the starting equity, i.e., the daily profit and loss.
- If the daily loss exceeds the maximum allowed loss (0.25% of the initial account funds), close all positions.
- If the daily profit exceeds the maximum allowed profit (2% of the initial account funds), close all positions.
Strategy Advantages
- Simple and easy to understand: The strategy logic is clear and uses only two moving averages to generate trading signals, making it easy to understand and implement.
- Trend following: By using the crossover of fast and slow EMAs, the strategy can capture changes in price trends relatively well, making it suitable for use in trending markets.
- Risk control: The daily stop-loss and take-profit limits can effectively control single-day losses and profits, preventing excessive fluctuations in the account.
Strategy Risks
- Parameter optimization: The performance of the strategy largely depends on the choice of EMA periods, and different parameter settings may lead to drastically different results. Therefore, parameter optimization and backtesting need to be performed in different market environments.
- Choppy markets: In choppy markets, prices frequently fluctuate above and below the EMAs, potentially generating many false signals and leading to frequent trades and capital erosion.
- Trend reversals: When market trends reverse, the strategy may delay entry or exit, missing the best trading opportunities.
Strategy Optimization Directions
- Introduce other technical indicators such as RSI and MACD to help judge trend strength and direction and improve signal accuracy.
- Optimize stop-loss and take-profit rules, such as using trailing stops or dynamic take-profit levels, to better protect profits and control risks.
- Dynamically adjust EMA periods based on market volatility to adapt to different market states.
- Combine fundamental analysis, such as economic data and major events, to filter and confirm trading signals.
Summary
The EMA dual moving average crossover strategy is a simple, easy-to-understand trading strategy suitable for trending markets. By using the crossover of fast and slow moving averages, it can capture changes in price trends relatively well. At the same time, the daily stop-loss and take-profit settings can effectively control risks. However, the strategy may underperform in choppy markets or during trend reversals and needs to be optimized and improved by combining other technical indicators and analysis methods.
Source Pine Script
/*backtest
start: 2023-06-01 00:00:00
end: 2024-06-06 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DD173838
//@version=5
strategy("Moving Average Strategy with Daily Limits", overlay=true)
// Moving Average settings
shortMaLength = input.int(9, title="Short MA Length")
longMaLength = input.int(21, title="Long MA Length")
// Calculate MAs
shortMa = ta.ema(close, shortMaLength)
longMa = ta.ema(close, longMaLength)
// Plot MAs
plot(shortMa, title="9 EMA", color=color.blue)
plot(longMa, title="21 EMA", color=color.red)
// Strategy conditions
crossUp = ta.crossover(shortMa, longMa)
crossDown = ta.crossunder(shortMa, longMa)
// Debug plots to check cross conditions
plotshape(series=crossUp, title="Cross Up", location=location.belowbar, color=color.green, style=shape.labelup, text="UP")
plotshape(series=crossDown, title="Cross Down", location=location.abovebar, color=color.red, style=shape.labeldown, text="DOWN")
// Entry at cross signals
if (crossUp)
strategy.entry("Long", strategy.long)
if (crossDown)
strategy.entry("Short", strategy.short)
// Daily drawdown and profit limits
var float startOfDayEquity = na
if (na(startOfDayEquity) or ta.change(time('D')) != 0)
startOfDayEquity := strategy.equity
maxDailyLoss = 50000 * 0.0025
maxDailyProfit = 50000 * 0.02
currentDailyPL = strategy.equity - startOfDayEquity
if (currentDailyPL <= -maxDailyLoss)
strategy.close_all(comment="Max Daily Loss Reached")
if (currentDailyPL >= maxDailyProfit)
strategy.close_all(comment="Max Daily Profit Reached")
Published by ChaoZhang on the FMZ strategy square · last modified 2024-06-07. 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-Dual-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