Strategy vault / Trend following
A Dual Moving Average Crossover Strategy
The dual moving average crossover strategy is a common quantitative trading strategy. It uses the crossover of fast and slow moving averages as the buy and sell signals. When the fast moving average crosses above the…

Overview
The dual moving average crossover strategy is a common quantitative trading strategy. It uses the crossover of fast and slow moving averages as the buy and sell signals. When the fast moving average crosses above the slow moving average from the bottom, a buy signal is generated. When the fast moving average crosses below the slow moving average from the top, a sell signal is generated.
Strategy Principle
The core logic of this strategy is to calculate two groups of moving averages, one is the fast moving average with a period parameter of 10 days, and the other is the slow moving average with a period parameter of 30 days. The fast moving average can respond to price changes faster, while the slow moving average can better reflect the long-term trend.
When the fast moving average crosses above the slow one, it means that the short-term price starts to break through the long-term trend, which is a golden cross signal to go long. When the fast moving average crosses below the slow one, it means that the short-term price starts to fall below the long-term trend, which is a death cross signal to go short.
The strategy also sets stop loss and take profit mechanisms. The stop loss is triggered when the price falls below a certain percentage of the entry price. The take profit is triggered when the price rises above a certain percentage of the entry price.
Advantage Analysis
The dual moving average crossover strategy has the following advantages:
The logic is simple and easy to understand and implement;
The parameters of fast and slow moving averages are customizable to fit different markets;
It contains both stop loss and take profit settings to limit losses;
It can perform well in both trending and range-bound markets.
Risk Analysis
The dual moving average crossover strategy also has the following risks:
The signal from the crossover may be a false breakout, leading to losses;
Improper settings of stop loss and take profit may result in huge losses or reduced expected profits;
It solely relies on technical indicators without considering fundamentals.
Corresponding solutions:
Add other technical indicators to filter out false signals;
Test and optimize the stop loss and take profit parameters;
Incorporate fundamental analysis.
Optimization Directions
The strategy can be optimized from the following aspects:
Test different parameter combinations of moving averages to find the optimal one;
Add price-volume confirmation indicators to avoid false breakouts;
Dynamically adjust the stop loss and take profit percentages for better profit taking;
Incorporate other indicators like trading volume, turnover rate etc.
Conclusion
In summary, the dual moving average crossover strategy is a simple and practical quantitative trading strategy. It is easy to understand and implement and can generate stable profits in most market environments. By optimizing parameters, adding signal filters and dynamic profit taking mechanisms, the strategy can become more reliable and profitable. As one of the fundamental quantitative trading strategies, it is worthwhile learning and applying.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | 10 | Fast MA Length |
| v_input_2 | 30 | Slow MA Length |
| v_input_3 | true | Stop Loss (%) |
| v_input_4 | 2 | Take Profit (%) |
Source Pine Script
/*backtest
start: 2023-01-12 00:00:00
end: 2024-01-18 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Moving Average Crossover", overlay=true)
// Define input parameters
fast_length = input(10, title="Fast MA Length")
slow_length = input(30, title="Slow MA Length")
stop_loss_percent = input(1.0, title="Stop Loss (%)", minval=0.1, maxval=10, step=0.1)
take_profit_percent = input(2.0, title="Take Profit (%)", minval=0.1, maxval=10, step=0.1)
// Calculate moving averages
fast_ma = sma(close, fast_length)
slow_ma = sma(close, slow_length)
// Entry conditions
long_condition = crossover(fast_ma, slow_ma)
short_condition = crossunder(fast_ma, slow_ma)
// Plot moving averages on the chart
plot(fast_ma, title="Fast MA", color=color.blue)
plot(slow_ma, title="Slow MA", color=color.red)
// Strategy orders
strategy.entry("Long", strategy.long, when=long_condition)
strategy.entry("Short", strategy.short, when=short_condition)
// Set stop loss and take profit levels
stop_loss_price = close * (1 - stop_loss_percent / 100)
take_profit_price = close * (1 + take_profit_percent / 100)
strategy.exit("Take Profit/Stop Loss", from_entry="Long", stop=stop_loss_price, limit=take_profit_price)
strategy.exit("Take Profit/Stop Loss", from_entry="Short", stop=take_profit_price, limit=stop_loss_price)
Published by ChaoZhang on the FMZ strategy square · last modified 2024-01-19. 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/A-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 Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Dual Moving Average Convergence Trend Tracking StrategyPine Script · Trend following · 1d
- Dual SMA Crossover StrategyPine Script · Trend following · 5m
- Dynamic Moving Average and Bollinger Bands Cross Strategy with Fixed Stop Loss Optimization ModelPine Script · Trend following · 3h
- Dynamic Signal Line Trend Following Strategy Combining ATR and VolumePine Script · Trend following · 1h
- EMA SMA Trend Following with Swing Trading Strategy Combined Volume Filter and Percentage Take Profit Stop Loss SystemPine Script · Trend following · 1d