Dual Moving Average Crossover Strategy

Author: ChaoZhang, Date: 2024-04-03 15:12:10



This strategy uses two moving averages with different periods (fast and slow) to generate trading signals. When the fast MA crosses above the slow MA, it generates a buy signal; when the fast MA crosses below the slow MA, it generates a sell signal. The strategy also sets stop loss and take profit levels to control risk and lock in profits.

Strategy Principle

The core principle of this strategy is to utilize the trend-following characteristic of moving averages. Moving averages can smooth out price fluctuations and reflect the main trend of prices. The short-term moving average is more sensitive to price changes, while the long-term moving average reacts more slowly. When the short-term moving average crosses the long-term moving average, it indicates that the price trend may have changed.

Specifically, when the fast MA (short-term moving average) crosses above the slow MA (long-term moving average), it suggests that an upward trend may begin, generating a buy signal; conversely, when the fast MA crosses below the slow MA, it suggests that a downward trend may begin, generating a sell signal. At the same time, the strategy sets a 2% stop loss and a 10% take profit to control risk and lock in profits.

Strategy Advantages

  1. Simple and easy to understand: The logic of this strategy is clear and easy to understand and implement. It only requires calculating two moving averages with different periods and judging their crossover relationship to generate trading signals.

  2. Trend tracking: The core advantage of the moving average strategy lies in its trend tracking ability. By using the crossover of fast and slow MAs, it can capture changes in price trends and adjust trading positions in a timely manner.

  3. Risk control: The strategy sets explicit stop loss and take profit levels, which can effectively control the risk exposure of a single trade. Once the price reaches the stop loss or take profit level, the strategy will automatically close the position, avoiding excessive losses or profit givebacks.

Strategy Risks

  1. Parameter selection: The performance of this strategy largely depends on the selection of fast and slow MA periods. Different period combinations may lead to different trading results. How to choose the optimal parameter combination is one of the main risks faced by this strategy.

  2. Choppy market: In a choppy market, prices fluctuate frequently but lack clear trends. At this time, fast and slow MAs may cross frequently, generating a large number of trading signals, leading to overtrading and high trading costs.

  3. Lag: Moving averages are lagging indicators, and their reaction to price changes has a certain delay. This means that the strategy may miss some early trend opportunities or fail to close positions in a timely manner when the trend reverses.

Strategy Optimization Directions

  1. Parameter optimization: By backtesting different period combinations, we can find the parameter settings with the best historical performance. This requires comprehensive testing and validation on in-sample and out-of-sample data.

  2. Trend filtering: To reduce overtrading in choppy markets, trend filtering indicators such as ADX or ParabolicSAR can be introduced. Trades are only made when the trend is obvious, avoiding trading in rangebound markets.

  3. Dynamic stop loss: Fixed percentage stop loss may not be suitable for all market environments. Dynamic stop loss mechanisms, such as ATR stop loss or trailing stop loss, can be considered, allowing the stop loss level to adjust dynamically with market volatility.

  4. Portfolio optimization: This strategy can be combined with other uncorrelated strategies to improve overall returns and stability. Through reasonable position sizing and risk management, the overall profitability can be improved while ensuring a high win rate.


The dual moving average crossover strategy is a simple and easy-to-use trend-following strategy. It generates trading signals based on the crossover relationship of fast and slow MAs while setting fixed stop loss and take profit levels to control risk. Although the strategy is easy to understand and implement, its performance largely depends on parameter selection and faces the risk of overtrading in choppy markets. Through parameter optimization, trend filtering, dynamic stop loss, and strategy combination, the robustness and profitability of this strategy can be further improved, making it a trustworthy quantitative trading tool.

start: 2023-03-28 00:00:00
end: 2024-04-02 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/
// © uugankhuu

strategy("Moving Average Crossover Strategy", overlay=true)

// Define length for fast and slow moving averages
fastLength = input(9, title="Fast MA Length")
slowLength = input(21, title="Slow MA Length")

// Calculate moving averages
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)

// Generate buy and sell signals
buySignal = ta.crossover(fastMA, slowMA)
sellSignal = ta.crossunder(fastMA, slowMA)

// Plot moving averages
plot(fastMA, color=color.blue, title="Fast MA")
plot(slowMA, color=color.red, title="Slow MA")

// Execute trades based on signals
strategy.entry("Buy", strategy.long, when=buySignal)
strategy.close("Buy", when=sellSignal)

// Set stop loss and take profit levels
stopLoss = input(0.02, title="Stop Loss (%)") // 2% stop loss
takeProfit = input(0.10, title="Take Profit (%)") // 10% take profit

strategy.exit("Take Profit/Stop Loss", "Buy", stop=close * (1 - stopLoss), limit=close * (1 + takeProfit))