0bps

Strategy vault / Trend following

Moving Average Crossover Strategy 434701

This strategy adopts the crossover of 20-day moving average and 60-day moving average to generate trading signals. It goes long when price breaks above 20-day MA and closes position when price breaks below 20-day MA…

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

IMG

This strategy adopts the crossover of 20-day moving average and 60-day moving average to generate trading signals. It goes long when price breaks above 20-day MA and closes position when price breaks below 20-day MA. Similarly, it forms trading signals when price crosses 60-day MA. This strategy belongs to a typical trend following system.

Strategy Logic

  1. Calculate 20-day simple moving average and 60-day simple moving average
  2. Go long when closing price breaks above 20-day MA
  3. Close position when closing price breaks below 20-day MA
  4. Go long when closing price breaks above 60-day MA
  5. Close position when closing price breaks below 60-day MA

The above rules define the trading signals and logic for this strategy. When price crosses over the MA line, it shows a new trend is emerging and we can follow the trend to go long. When price drops below the MA line, it shows the trend is ending so we close position.

Advantages

  1. Adopting double MAs makes the strategy more steady. The 20-day MA captures short-term opportunities faster while the 60-day MA filters out some market noises and locks in medium-long term trend.
  2. The backtest starts from 2018 and selects Taiwan stock market, which has more developed trading system compared to China A-share market, better reflecting the strategy efficacy.
  3. It sets proper stop loss and position sizing, maximally controlling the risk.

Risks

  1. The strategy solely relies on MA indicator. It may generate more whipsaws when there is no obvious trend in the market.
  2. The strategy does not optimize the buy/sell size and position, failing to maximize the capital usage.
  3. The strategy reacts symmetrically to price ups and downs, unable to adapt to different market conditions.

Risk Solutions:

  1. Add other indicators like KDJ, MACD to form multiple confirmation, avoiding wrong trades.
  2. Optimize position sizing and capital usage efficiency according to market cap, volatility etc.
  3. Adopt asymmetric moves based on market stages, reduce trading during range-bound market and increase position size during obvious trend.

Optimization Directions

  1. Optimize buy/sell quantity. Dynamically adjust position size based on stop loss.
  2. Optimize MA parameters. Find better parameters through walk forward optimization and random optimization.
  3. Add stop loss strategy. Moving stop loss or stop limit order better protects profits.
  4. Add position sizing management. Dynamically adjust position size per trade based on capital size, market cap etc.

Summary

This is a typical dual moving average crossover strategy. The core idea is to follow trends by establishing position when price crosses over MA line. The strategy is simple and practical to implement. Meanwhile, there is room for further optimization, by parameter tuning, stop loss, position sizing etc. to achieve better results.

Parameters

NameDefaultWhat it controls
v_input_12018backtest_year
v_input_int_1truebacktest_month
v_input_int_2truebacktest_day

Source Pine Script

/*backtest
start: 2022-12-01 00:00:00
end: 2023-12-07 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/
// © Astorhsu

//@version=5
strategy("Astor SMA20/60 TW", overlay=true, margin_long=100, margin_short=100)
backtest_year = input(2018, title='backtest_year') //Backtest start year
backtest_month = input.int(01, title='backtest_month', minval=1, maxval=12) //Backtest start month
backtest_day = input.int(01, title='backtest_day', minval=1, maxval=31)  //Backtest start day
start_time = timestamp(backtest_year, backtest_month, backtest_day, 00, 00)  //Time function for the start of backtesting

//Indicators
sma20 = ta.sma(close,20)
sma60 = ta.sma(close,60)
plot(sma20, color=color.green, title="sma(20)")
plot(sma60, color=color.red, title="sma(60)")

//Entry Conditions
longCondition = ta.crossover(close, ta.sma(close, 20))
if (longCondition) and time >= start_time
    strategy.entry("open long20", strategy.long, qty=1, comment="On the Sitem20go long")


shortCondition = ta.crossunder(close, ta.sma(close, 20))
if (shortCondition) and time >= start_time
    strategy.close("open long20",comment="Break belowm20Close Position", qty=1)     
    
longCondition1 = ta.crossover(close, ta.sma(close, 60))
if (longCondition1) and time >= start_time
    strategy.entry("open long60", strategy.long, qty=1, comment="On the Sitem60go long")


shortCondition1 = ta.crossunder(close, ta.sma(close, 60))
if (shortCondition1) and time >= start_time
    strategy.close("open long60",comment="Break belowm60Close Position", qty=1)     

Published by ChaoZhang on the FMZ strategy square · last modified 2023-12-08. 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-434701.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