0bps

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…

Author
ChaoZhang
Complexity
Starter · 18 lines
Parameters
0
Backtest bar
2h · intraday
Tested on
BTC/USDT · futures

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:

  1. Calculate two MAs, one short-term e.g. 5-day and one long-term e.g. 21-day
  2. Go long when the 5-day MA crosses above the 21-day MA
  3. Close the long when the 5-day MA crosses back below the 21-day MA
  4. Similarly calculate a 14-day and 28-day MA
  5. Go short when the 14-day MA crosses below the 28-day MA
  6. 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.

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-426776.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