0bps

Strategy vault / Trend following

The Moving Average Crossover Trend Strategy

The Moving Average Crossover Trend strategy is a trend-following strategy based on moving average crossover signals. It uses the golden cross and death cross of fast and slow moving averages to determine market…

Author
ChaoZhang
Complexity
Starter · 39 lines
Parameters
6
Backtest bar
3h · intraday
Tested on
BTC/USDT · futures

IMG

Overview

The Moving Average Crossover Trend strategy is a trend-following strategy based on moving average crossover signals. It uses the golden cross and death cross of fast and slow moving averages to determine market trends, establishes positions at the beginning of trends, and closes positions when trend reversal signals appear.

Principles

The strategy uses the crossovers of MACD histogram and signal line to identify the start and end of trends. Specifically, it constructs the MACD histogram based on 12-period fast EMA and 26-period slow EMA. When the histogram crosses above the signal line, a buy signal is generated, indicating the start of an uptrend. When the histogram crosses below the signal line, a sell signal is triggered, marking the start of a downtrend.

For entries, the strategy only goes long when a buy signal is generated on the 15-min chart to capitalize on the early stage of trend starts. For exits, it closes all positions when the MACD histogram crosses below the signal line on the 4-hour chart, signaling a trend reversal.

Advantage Analysis

The biggest advantage of this strategy is its ability to timely catch trend starts and exit on reversal signals, achieving good risk-reward ratios. The main advantages are:

  1. Using MACD for trend identification is reliable with high winning rate
  2. Combining 15-min and 4-hour timeframes balances frequency and risk control
  3. Timely stop loss effectively limits maximum drawdown

Risk Analysis

There are also some risks mainly in the following aspects:

  1. MACD may generate false signals, causing unnecessary entries or stops
  2. The stop loss point may be too crude to accommodate market fluctuations
  3. Improper parameter selection may undermine strategy efficacy

To mitigate the risks, optimizations can be made in:

  1. Adding filter with other indicators to avoid false signals
  2. Adaptive adjustments of stop loss points
  3. Parameter tuning

Optimization Directions

The main aspects to further optimize the strategy include:

  1. Incorporate other indicators like RSI, Bollinger Bands to filter signals
  2. Test more fast and slow period combinations for optimal parameters
  3. Utilize machine learning to train optimum parameters
  4. Optimize stop loss rules with trailing or partial stops
  5. Expand to more timeframes for multi-timeframe combinations

Conclusion

Overall, the Moving Average Crossover Trend Strategy is a simple and practical trend following system. It capitalizes on trends by identifying starts and ends using MACD crossovers, and combining short-term and long-term positions. The advantages lie in its timely entries, effective stops, and balanced risk-reward. Next steps would be improving robustness and profitability via parameterized optimization, signal filtering etc.

Parameters

NameDefaultWhat it controls
v_input_112Fast Length
v_input_226Slow Length
v_input_3_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_int_19Signal Smoothing
v_input_string_10Oscillator MA Type: EMA | SMA
v_input_string_20Signal Line MA Type: EMA | SMA

Source Pine Script

/*backtest
start: 2024-01-01 00:00:00
end: 2024-01-31 23:59:59
period: 3h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=5
strategy(title="Moving Average Convergence Divergence", shorttitle="MACD", overlay=true)

// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing", minval=1, maxval=50, defval=9)
sma_source = input.string(title="Oscillator MA Type", defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Calculating MACD
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal_line = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)

// Entry conditions
longCondition = macd < 0 and ta.crossover(macd, signal_line) 
shortCondition = ta.crossover(signal_line, macd) 

// Plot signals
plotshape(series=longCondition, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small, title="Buy Signal")
plotshape(series=shortCondition, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small, title="Sell Signal")

// Strategy
if (longCondition)
    strategy.entry("Long", strategy.long)
if (shortCondition)
    strategy.entry("Short", strategy.short)

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-28. 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-Trend-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