0bps

Strategy vault / Trend following

The moving average crossover strategy is a quantitative trading strategy

The moving average crossover strategy is a quantitative trading strategy based on simple moving averages (SMA). It generates buy and sell signals when different period SMA crossovers occur. Specifically, this…

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

IMG

Overview

The moving average crossover strategy is a quantitative trading strategy based on simple moving averages (SMA). It generates buy and sell signals when different period SMA crossovers occur.

Specifically, this strategy calculates the 9-period and 45-period SMA. When the price crosses above both SMA lines, a buy signal is generated. When the price crosses below both lines, a sell signal is triggered.

Strategy Logic

The core logic of this strategy is based on the "golden cross" and "dead cross" principles of moving averages. Moving averages can effectively filter out market noise and indicate major trend changes. When the shorter-term MA crosses above the longer-term MA, it signals an upward trend reversal. The opposite crossover signals a downtrend.

In particular, this strategy uses the 9-period and 45-period simple moving averages. The 9-period line represents short-term trends while the 45-period line captures longer-term moves. When the price crosses above both SMA lines, it indicates the price is in upward channels both short-term and long-term, hence triggering a long entry. The opposite crossover suggests weakening upside momentum and prompts exit signals.

From the code perspective, the strategy first computes the 9-period and 45-period SMA values. It then uses the ta.crossover and ta.crossunder functions to detect golden crosses and dead crosses between the two MA lines. When buy and sell signals are triggered, plotshape functions draw triangles and inverted triangles on the price chart.

In addition, stop-loss logic is implemented to manage trade exits. Specifically, the high and low prices of the previous bar are extracted as the stop-loss price after opening new trades. This allows the strategy to lock in gains and prevent huge losses.

Advantage Analysis

  • The dual moving average setup captures mid-to-long term trend shifts while filtering out short-term noises, improving signal quality.

  • The stop-loss mechanism effectively controls risks and locks in profits.

  • Simple and easy-to-implement logic, suitable for beginners.

  • High capital utilization for compounding gains.

Risk Analysis

  • Dual MA strategies tend to generate whipsaws and invalid signals during choppy markets.

  • Conservative stop loss placement unable to track trends effectively.

  • Suboptimal parameter selection may lead to overtrading or insufficient trade frequency.

  • Unable to adapt to huge trend reversals.

Solutions:

  1. Optimize MA parameters to reduce false signals

  2. Implement trend-following dynamic stops

  3. Add filters using other indicators

  4. Manual override around major reversals

Optimization Directions

Further improvements for the strategy:

  1. Use adaptive or exponential MAs to better capture trends.

  2. Add volatility filter to avoid false signals during ranging markets.

  3. Perform parameter optimization for best parameter combinations.

  4. Incorporate trend-following mechanisms into stop-loss logic.

  5. Add support-resistance analysis to avoid signals around key levels.

  6. Leverage machine learning to further filter signal quality.

Conclusion

The moving average crossover system is a simple yet effective trend following approach. By filtering out noise and tracking mid-term trends, it generates quality signals. With proper stop losses, it enables risk-managed trend trading. The simple logic also makes it ideal for beginners to put into practice. Further optimizations can integrate this strategy as an effective component of overall quant systems.

Parameters

NameDefaultWhat it controls
v_input_19Fast SMA Length
v_input_245Slow SMA Length

Source Pine Script

/*backtest
start: 2022-12-15 00:00:00
end: 2023-12-21 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy("SMA Crossover Strategy", overlay=true)

// Input parameters
fast_length = input(9, title="Fast SMA Length")
slow_length = input(45, title="Slow SMA Length")

// Calculate moving averages
fast_sma = ta.sma(close, fast_length)
slow_sma = ta.sma(close, slow_length)

// Buy condition
buy_condition = ta.crossover(close, fast_sma) and ta.crossover(close, slow_sma)

// Sell condition
sell_condition = ta.crossunder(close, fast_sma) and ta.crossunder(close, slow_sma)

// Calculate stop loss levels
prev_low = request.security(syminfo.tickerid, "1D", low[1], lookahead=barmerge.lookahead_on)
prev_high = request.security(syminfo.tickerid, "1D", high[1], lookahead=barmerge.lookahead_on)

// Plot signals on the chart
plotshape(buy_condition, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(sell_condition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// Strategy exit conditions
long_stop_loss = sell_condition ? prev_low : na
short_stop_loss = buy_condition ? prev_high : na

strategy.exit("Long Exit", from_entry="Long", when=sell_condition, stop=long_stop_loss)
strategy.exit("Short Exit", from_entry="Short", when=buy_condition, stop=short_stop_loss)

strategy.entry("Long", strategy.long, when=buy_condition)
strategy.entry("Short", strategy.short, when=sell_condition)

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-22. 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/The-moving-average-crossover-strategy-is-a-quantitative-trading-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