Moving Average Crossover Strategy

Author: ChaoZhang, Date: 2023-12-05 11:52:28



The moving average crossover strategy is a quantitative trading strategy based on technical indicators. It judges the trend direction of the market by calculating the crossover relationship between two moving average lines, and generates corresponding trading signals.

Strategy Logic

The core indicators of this strategy are two moving average lines: a longer 40-period simple moving average (SMA) and the closing price of the stock. When the closing price breaks through the 40-period SMA from below, it indicates that the market trend may reverse and the stock may enter a new uptrend. At this point, the strategy will generate a long signal. When the closing price falls below the 40-period SMA, it indicates that the uptrend of the stock has ended and it may enter a downtrend channel. The strategy will close long positions at this time.

By comparing the breakthrough relationship between closing price and SMA, the turning points of the price trend can be captured to make trading decisions according to the trend direction.

Advantages of the Strategy

The strategy has the following advantages:

  1. Simple and clear rules that are easy to understand and implement.
  2. Can effectively capture the reversal of stock mid-to-long term trends and timely adjust positions.
  3. The SMA indicator has a certain filtering effect on abnormal price movements and can reduce false signals.
  4. Customizable SMA parameters, applicable to different trading varieties and cycles.

Risks of the Strategy

There are also the following risks:

  1. As a trend tracking tool, the SMA indicator lags in responding to sudden events.
  2. Frequent trading and whipsaw may increase trading costs and locked profit risks.
  3. Improper parameter settings may lead to overtrading or missing opportunities.

Risks can be controlled by adjusting SMA parameters, setting stop loss lines, etc.

Optimization Directions

The strategy can also be optimized in the following aspects:

  1. Add comparisons of multiple moving averages to form a filter to reduce false signals.
  2. Incorporate other indicators such as trading volume and graph rector to enhance reliability.
  3. Dynamically optimize SMA parameters to automatically adapt them to market changes.
  4. Set stop loss mechanisms with compound conditions to control single transaction loss.


The moving average crossover strategy judges trend reversal by comparing price changes with the SMA relationship. It is a relatively classic rule-based trading strategy. The strategy is simple to implement, easy to follow mid-to-long term trends for profit, while there are also certain risks of profit retracement and lagging identification. Risks can be controlled and decision effectiveness improved by parameter settings and incorporating other indicators.

start: 2023-11-04 00:00:00
end: 2023-12-04 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]

strategy(title="MA Crossover (40)", overlay=true)

// Input for the SMA length (24)
sma_length = input(40, title="SMA Length")
sma = ta.sma(close, sma_length)

// Determine if the current candle crosses above the 24-period SMA
longCondition = ta.crossover(close, sma)

// Determine if the current candle crosses and closes below the 24-period SMA
closeLongCondition = ta.crossunder(close, sma)

// Plot the 24-period SMA
plot(sma,, title="24-period SMA")

// Long entry signal
if (longCondition)
    strategy.entry("Long", strategy.long)

// Close long position when the current candle crosses and closes below the 24-period SMA
if (closeLongCondition)

// Create alerts
alertcondition(longCondition, title="Candle Crosses Above SMA 40", message="Candle has crossed above SMA 40.")
alertcondition(longCondition, title="Candle Closes Above SMA 40", message="Candle has closed above SMA 40.")