0bps

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…

Author
ChaoZhang
Complexity
Starter · 39 lines
Parameters
4
Backtest bar
1d · swing
Tested on
BTC/USDT · futures

IMG

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:

  1. The logic is simple and easy to understand and implement;

  2. The parameters of fast and slow moving averages are customizable to fit different markets;

  3. It contains both stop loss and take profit settings to limit losses;

  4. It can perform well in both trending and range-bound markets.

Risk Analysis

The dual moving average crossover strategy also has the following risks:

  1. The signal from the crossover may be a false breakout, leading to losses;

  2. Improper settings of stop loss and take profit may result in huge losses or reduced expected profits;

  3. It solely relies on technical indicators without considering fundamentals.

Corresponding solutions:

  1. Add other technical indicators to filter out false signals;

  2. Test and optimize the stop loss and take profit parameters;

  3. Incorporate fundamental analysis.

Optimization Directions

The strategy can be optimized from the following aspects:

  1. Test different parameter combinations of moving averages to find the optimal one;

  2. Add price-volume confirmation indicators to avoid false breakouts;

  3. Dynamically adjust the stop loss and take profit percentages for better profit taking;

  4. 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

NameDefaultWhat it controls
v_input_110Fast MA Length
v_input_230Slow MA Length
v_input_3trueStop Loss (%)
v_input_42Take 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.

in Claude Code, Cursor, Codex & 4 more
/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
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open