0bps

Strategy vault / Trend following

Moving Average Crossover Strategy 428084

This strategy utilizes the golden cross and death cross of moving averages to determine trends and identify potential buying and selling opportunities. It uses both fast and slow moving averages and generates trading…

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

Overview

This strategy utilizes the golden cross and death cross of moving averages to determine trends and identify potential buying and selling opportunities. It uses both fast and slow moving averages and generates trading signals based on their crossover.

Strategy Logic

The strategy employs two moving averages with different timeframes. The first MA has a shorter timeframe, set to 20 days, to capture short-term price moves. The second MA has a longer timeframe, set to 120 days, to gauge the long-term trend.

When the faster MA crosses above the slower MA, a golden cross occurs, signaling an upward trend in the short term, and a buy signal is generated. When the faster MA crosses below the slower MA, a death cross occurs, signaling a downward trend in the short term, and a sell signal is generated.

The strategy uses ta.crossover and ta.crossunder to detect the crossover of the MAs. Once a crossover is identified, a corresponding buy or sell signal is triggered.

Advantage Analysis

The biggest advantage of this strategy is its simplicity. Moving averages are among the most common technical analysis tools and easy to understand even for non-professionals. MAs also effectively filter out market noise and identify trend direction.

Compared to more complex indicators, MAs are relatively straightforward to implement in a strategy. It only requires optimizing the MA periods to create a robust system.

Moreover, the MA strategy offers flexibility. The parameters can be adjusted for different products and timeframes, from long term to short term.

Risk Analysis

The major risk is whipsaws generating frequent false signals when the trend oscillates. In this case, the MA periods should be properly tuned to filter out noise.

Another potential risk is the lagging nature of MAs. It takes time for MAs to reflect new trends, which may cause slippage.

Also, the strategy does not consider the impact of sudden events like major news. These could invalidate the effectiveness of MAs. Stops should be implemented to control risks.

Optimization Directions

The strategy can be further enhanced through:

  1. Adding filters like volume to avoid false signals in range-bound markets.

  2. Using adaptive MAs that adjust periods based on volatility.

  3. Combining other indicators like MACD and Stochastics to confirm signals.

  4. Establishing price channels and only considering signals on breakouts.

  5. Implementing stop loss and take profit to increase robustness.

Conclusion

In summary, the MA crossover strategy generates signals by crossing fast and slow MAs. It is easy to use and identifies trends, but also carries risks of false signals and lags. With optimized parameters, added filters, and indicator combinations, it can greatly improve viability. Overall, the MA strategy is a practical trend following system worth studying and applying for traders.

Parameters

NameDefaultWhat it controls
v_input_1_close0Source: close | high | low | open | hl2 | hlc3 | hlcc4 | ohlc4
v_input_2201st MA Length
v_input_string_101st MA Type: EMA
v_input_31202nd MA Length
v_input_string_202nd MA Type: EMA

Source Pine Script

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

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © brandlabng

//@version=5
//study(title="Holly Grail FX", overlay = true)
strategy('HG|E30m', overlay=true)
src = input(close, title='Source')

price = request.security(syminfo.tickerid, timeframe.period, src)
ma1 = input(20, title='1st MA Length')
type1 = input.string('EMA', '1st MA Type', options=['EMA'])

ma2 = input(120, title='2nd MA Length')
type2 = input.string('EMA', '2nd MA Type', options=['EMA'])

price1 = if type1 == 'EMA'
    ta.ema(price, ma1)

price2 = if type2 == 'EMA'
    ta.ema(price, ma2)


//plot(series=price, style=line,  title="Price", color=black, linewidth=1, transp=0)
plot(series=price1, style=plot.style_line, title='1st MA', color=color.new(#219ff3, 0), linewidth=2)
plot(series=price2, style=plot.style_line, title='2nd MA', color=color.new(color.purple, 0), linewidth=2)


longCondition = ta.crossover(price1, price2)
if longCondition
    strategy.entry('Long', strategy.long)

shortCondition = ta.crossunder(price1, price2)
if shortCondition
    strategy.entry('Short', strategy.short)

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