Strategy vault / Trend following
Moving Average Crossover Strategy 426776
The moving average crossover strategy generates buy and sell signals by calculating the crossover between two moving averages of different periods. A long signal is generated when the shorter period MA crosses above…
Strategy Logic
The moving average crossover strategy generates buy and sell signals by calculating the crossover between two moving averages of different periods. A long signal is generated when the shorter period MA crosses above the longer period MA, while a short signal is generated on the downward crossover.
For example, going long when the 5-day MA crosses above the 21-day MA, and closing the long when the 5-day MA crosses back below the 21-day MA.
The trading logic is:
- Calculate two MAs, one short-term e.g. 5-day and one long-term e.g. 21-day
- Go long when the 5-day MA crosses above the 21-day MA
- Close the long when the 5-day MA crosses back below the 21-day MA
- Similarly calculate a 14-day and 28-day MA
- Go short when the 14-day MA crosses below the 28-day MA
- Close the short when the 14-day MA crosses back above the 28-day MA
Different MA period combinations can suit short or long term trends.
Advantages
- Simple and easy to implement
- MAs provide some trend filtering
- Parameters can be optimized by adjusting periods
Risks
- MAs lag price, time delay
- Longs and shorts can open simultaneously
- Prone to whipsaws in choppy markets
Summary
The MA crossover strategy uses MA crosses to generate signals, with adjustable periods to fit market cycles. A simple trend following approach, but lagging MAs and whipsaw risk need caution. Consider combining with other indicators for filtration and optimization.
Source Pine Script
/*backtest
start: 2023-08-14 00:00:00
end: 2023-09-13 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
strategy("My Strategy", overlay=true)
longCondition = crossover(sma(close, 5), sma(close, 21))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-14. 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.
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Moving-Average-Crossover-Strategy-426776.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- ADX MA and EMA Long Only Trend Tracking StrategyPine Script · Trend following · 1d
- ATR and Moving Average Crossover Hybrid StrategyPine Script · Trend following · 1h
- Adaptive FVG Detection and MA Trend Trading Strategy with Dynamic ResistancePine Script · Trend following · 1d
- Adaptive Risk Management Strategy Based on Dynamic ATR and Moving Average CrossoverPine Script · Trend following · 2h
- Adaptive Trading Strategy Based on Stochastic RSI Dual Line CrossoverPine Script · Trend following · 4h
- Adaptive Volatility Based Trend Following Short Strategy with Time Delay and Stop Loss ProtectionPine Script · Trend following · 2m