0bps

Strategy vault / Breakout

Dual Moving Average Breakout Strategy 441003

The dual moving average breakout strategy is a typical trend following quantitative trading strategy. It generates trading signals by calculating simple moving averages of different periods and checking if the price…

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

IMG

Overview

The dual moving average breakout strategy is a typical trend following quantitative trading strategy. It generates trading signals by calculating simple moving averages of different periods and checking if the price breaks through them to determine positions. This strategy uses 20-day and 60-day moving averages as trading signals.

Strategy Logic

The core logic of the dual MA strategy is to use moving averages of different periods to capture price trends and generate trading signals when the price breaks through the moving averages.

Specifically, this strategy employs 20-day and 60-day simple moving averages. These two moving averages can be seen as tools to capture short-term and medium-long term trends respectively. When the short term price breaks through the medium-long term price, it signals that the market is in an upward trend and thus should go long. When the short term price drops below the medium-long term price, it signals that the market is in a downward trend and thus positions should be reduced.

The code uses ta.crossover and ta.crossunder to determine if the price has broken through or dropped below a moving average. Trading signals of going long or reducing position are emitted accordingly when a breakout happens.

Advantages

The dual moving average breakout strategy has the following advantages:

  1. The concept is simple and easy to understand and implement.
  2. It can effectively track market trends and avoid noise interference.
  3. Few strategy parameters and easy to optimize.
  4. Flexible in choosing moving average periods to adjust market sensitivity.

Risks

There are also some risks with the strategy:

  1. Prone to whipsaws when market is ranging. Can be alleviated by increasing holding period.
  2. Ineffective in catching quick market reversals. Other indicators can be added as filters.
  3. Moving averages inherently lagging, unable to early signal price changes. Shortening period may help.

Enhancement Areas

The strategy can be enhanced from the following dimensions:

  1. Optimize moving average periods to find best parameter sets.
  2. Add other indicators to filter out false signals, e.g. MACD, KD etc.
  3. Add stop loss logic.
  4. Incorporate multi-timeframe analysis for robustness.

Summary

The dual moving average breakout strategy is a simple and practical trend following strategy. It can effectively capture medium-long term trends while avoiding short-term market noise. Also, the easy-to-understand logic and limited parameters make it very suitable for quantitative trading. Of course there are rooms for improvements, such as parameter tuning, signal filtering and stop loss to make it more stable and profitable.

Parameters

NameDefaultWhat it controls
v_input_12018backtest_year
v_input_int_1truebacktest_month
v_input_int_2truebacktest_day

Source Pine Script

/*backtest
start: 2024-01-04 00:00:00
end: 2024-02-03 00:00:00
period: 4h
basePeriod: 15m
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", overlay=true)
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
sma10 = ta.sma(close,10)
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
// trend1 = sma60 > sma20 //Assume that the current trend is60>20
longCondition = ta.crossover(close, ta.sma(close, 20))
if (longCondition) 
    strategy.entry("open long20", strategy.long, qty=1, comment="On the Sitem20go long")


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


shortCondition1 = ta.crossunder(close, ta.sma(close, 60))
if (shortCondition1) 
    strategy.close("open long60",comment="Break belowm60Close Position", qty=1)     
    
// longCondition2 = ta.crossover(close, ta.sma(close, 10))
// if (longCondition2) 
//     strategy.entry("open long10", strategy.long, qty=1, comment="On the Sitem10go long")


// shortCondition2 = ta.crossunder(close, ta.sma(close, 10))
// if (shortCondition2)
//     strategy.close("open long10",comment="Break belowm10Close Position", qty=1)   

Published by ChaoZhang on the FMZ strategy square · last modified 2024-02-04. 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/Dual-Moving-Average-Breakout-Strategy-441003.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