0bps

Strategy vault / Trend following

Moving Average Crossover Strategy 436617

The moving average crossover strategy is a timing strategy based on moving averages. It generates buy and sell signals by calculating different period moving averages and judging their crossover. This strategy also…

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

IMG

Overview

The moving average crossover strategy is a timing strategy based on moving averages. It generates buy and sell signals by calculating different period moving averages and judging their crossover. This strategy also combines the exponential moving average as an auxiliary indicator to further improve the accuracy of signals.

Principles

The core logic of this strategy is based on the crossover between two moving averages. Specifically, it calculates the n-day simple moving average (short MA) and the m-day simple moving average (long MA). When the short MA breaks through the long MA from bottom to top, a buy signal is generated. When the short MA breaks through the long MA from top to bottom, a sell signal is generated. This reflects the wash and correction of short-term trends on long-term trends.

In addition, this strategy also introduces the x-day exponential moving average (EMA) as an auxiliary indicator. Compared with SMA, EMA is smoother and can reflect price changes faster. Its auxiliary effect is that only when the short-term EMA also confirms the moving average crossover signal, the actual trading signal will be triggered. This avoids some interference from false signals and improves the stability of trading strategies.

Advantages

The moving average crossover strategy has the following advantages:

  1. Simple and easy to use. This strategy relies solely on the crossover between two moving averages, which is very simple, easy to understand and implement.

  2. Intuitive and vivid. Moving averages can clearly reflect market trends, and their crossover is also very intuitive without complex calculations.

  3. Long history. Moving average strategies can be traced back to the early 20th century and have undergone 100 years of market test to become one of the classic technical analysis tools.

  4. Controllable risks. By adjusting the moving average parameters, you can control the frequency of trading signals and thus control risks.

  5. Universal and flexible. The moving average crossover strategy is suitable for various products and time cycles, making it a very versatile and flexible trading strategy.

Risks

This strategy also has some risks:

  1. Frequent position switching. When the market fluctuates sharply, the moving averages may frequently crossover, resulting in over-frequent position switching.

  2. Lagging effects. The moving average itself carries a certain lag, especially long-cycle moving averages, which may miss short-term trading opportunities.

  3. Parameter optimization needed. For different products and time cycles, the parameters of the moving averages need to be independently tested and optimized, otherwise the results may be poor.

  4. Can be combined with other indicators. A single moving average strategy is not the best performing. It often requires other technical indicators to filter out signals.

Optimization Directions

This strategy can be optimized in the following aspects:

  1. Adjust moving average parameters to adapt to different cycles. Different combinations of short-term and long-term moving averages can be tested to find the optimal parameters.

  2. Add auxiliary judgment of trading volume. For example, set up indicators for breaking through trading volume to avoid invalid signals.

  3. Add volatility indicators for judgment. For example, KDJ and MACD can judge the actual market trend and filter uncertain signals.

  4. Combine fundamentals. Adjust parameters based on earnings expectations to make strategies more forward-looking.

  5. Portfolio application of strategies. Use with other strategies or models to achieve synergistic effects.

Conclusion

The moving average crossover strategy generates trading signals through the simple principle of moving average crossover. It is intuitive, easy to understand, flexible in parameter adjustment and risk controllable. But it also has inherent lagging properties and risks of over-frequent position switching. Therefore, this strategy can be optimized and combined in various ways to maximize its utility. It has become a simple and effective basic strategy in quantitative trading.

Parameters

NameDefaultWhat it controls
v_input_110Short MA Length
v_input_240Long MA Length
v_input_320EMA Length

Source Pine Script

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

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

// Define input parameters
shortLength = input(10, title="Short MA Length")
longLength = input(40, title="Long MA Length")
emaLength = input(20, title="EMA Length")

// Calculate moving averages
shortMA = ta.sma(close, shortLength)
longMA = ta.sma(close, longLength)
colorfulEMA = ta.ema(close, emaLength)

// Create buy and sell conditions
buyCondition = ta.crossover(shortMA, longMA)
sellCondition = ta.crossunder(shortMA, longMA)

// Execute buy and sell orders
if (buyCondition)
    strategy.entry("Buy", strategy.long)
    strategy.close("Sell")

if (sellCondition)
    strategy.entry("Sell", strategy.short)
    strategy.close("Buy")

// Color the background based on buy and sell conditions
bgcolor(buyCondition ? color.new(color.blue, 90) : na)
bgcolor(sellCondition ? color.new(color.red, 90) : na)

// Plot moving averages
plot(shortMA, color=color.new(color.blue, 90), title="Short MA")
plot(longMA, color=color.new(color.red, 90), title="Long MA")

// Plot colorful EMA with transparency
plot(colorfulEMA, color=color.new(color.green, 90), title="Colorful EMA")

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-26. 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/Moving-Average-Crossover-Strategy-436617.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