Strategy vault / Trend following
Dynamic Dual Moving Average Crossover Quantitative Trading Strategy
This is a quantitative trading strategy based on the EMA indicator, which makes trading decisions by calculating the crossover signals of short-term (9-period) and long-term (21-period) exponential moving averages…

Overview
This is a quantitative trading strategy based on the EMA indicator, which makes trading decisions by calculating the crossover signals of short-term (9-period) and long-term (21-period) exponential moving averages. The strategy includes stop-loss and take-profit conditions set at 2% and 4% respectively to control risk and lock in profits. The core idea is to capture market trend turning points through moving average crossovers, enabling timely buy and sell operations when market trends change.
Strategy Principles
The strategy employs two exponential moving averages (EMA) with different periods: 9-period and 21-period. A buy signal is generated when the short-term EMA crosses above the long-term EMA, while a sell signal is triggered when the short-term EMA crosses below the long-term EMA. The strategy incorporates risk management mechanisms through 2% stop-loss and 4% take-profit levels to protect capital and secure gains. The short-term moving average is more sensitive to price changes, while the long-term moving average reflects longer-term trends, making their crossovers effective in capturing market trend transitions.
Strategy Advantages
- Clear operational rules and signals, easy to execute and backtest
- Effective risk control through stop-loss and take-profit settings
- Automatically adapts to market volatility without manual intervention
- Simple calculations with high execution efficiency
- Applicable to different time periods and market environments
- Clear code structure, easy to maintain and optimize
- Good scalability, can incorporate additional technical indicators for optimization
Strategy Risks
- May generate frequent false breakout signals in choppy markets
- Moving averages have inherent lag, potentially missing important market turning points
- Fixed stop-loss and take-profit parameters may not suit all market conditions
- Trading costs not considered, actual returns may be lower than backtest results
- Frequent stop-losses may be triggered in highly volatile markets
- Market liquidity risk not addressed
- Lack of consideration for macro market conditions
Strategy Optimization Directions
- Introduce volatility indicators for dynamic adjustment of stop-loss and take-profit parameters
- Add volume indicators to improve signal reliability
- Incorporate trend confirmation indicators like RSI or MACD
- Dynamically adjust moving average periods based on market conditions
- Add position management mechanisms for dynamic capital allocation
- Implement market condition assessment for parameter adjustment
- Consider trading costs and optimize trading frequency
Summary
This strategy is a classic trend-following approach that captures market trend changes through moving average crossovers. While relatively simple in design, it includes complete trading logic and risk control mechanisms. The strategy's stability and profitability can be further enhanced through optimization measures such as dynamic parameter adjustment and market condition assessment. In practical application, it is recommended to optimize parameters based on specific trading instruments and market conditions while maintaining proper risk control.
Source Pine Script
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-27 00:00:00
period: 1d
basePeriod: 1d
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/
// © ancour
//@version=5
strategy("Moving Average Crossover", overlay=true)
// Define the length for short-term and long-term EMAs
shortEmaLength = 9
longEmaLength = 21
// Calculate EMAs
shortEma = ta.ema(close, shortEmaLength)
longEma = ta.ema(close, longEmaLength)
// Plot EMAs on the chart
plot(shortEma, title="Short-term EMA", color=color.green, linewidth=2)
plot(longEma, title="Long-term EMA", color=color.red, linewidth=2)
// Strategy conditions for crossovers
longCondition = ta.crossover(shortEma, longEma)
shortCondition = ta.crossunder(shortEma, longEma)
// Enter long when short EMA crosses above long EMA
if (longCondition)
strategy.entry("Buy", strategy.long)
// Exit long or enter short when short EMA crosses below long EMA
if (shortCondition)
strategy.entry("Sell", strategy.short)
// Optional: Add stop-loss and take-profit levels for risk management
stopLossPercent = 2
takeProfitPercent = 4
strategy.exit("Sell TP/SL", "Buy", stop=low * (1 - stopLossPercent/100), limit=high * (1 + takeProfitPercent/100))Published by ChaoZhang on the FMZ strategy square · last modified 2024-11-28. 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/Dynamic-Dual-Moving-Average-Crossover-Quantitative-Trading-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