Crossing the Breakout Strategy
The moving average crossover strategy is a very common quantitative trading strategy. It uses the golden cross and death cross of moving averages to determine trends and profit. When the short-term moving average…
Overview
The moving average crossover strategy is a very common quantitative trading strategy. It uses the golden cross and death cross of moving averages to determine trends and profit. When the short-term moving average crosses above the long-term moving average, it signals an uptrend, and a long position can be taken. When the short-term moving average crosses below the long-term moving average, it signals a downtrend, and a short position can be taken.
Strategy Logic
This strategy is based on the golden cross and death cross of moving averages to determine entry and exit points. The code uses two boolean input parameters upOrDown and longOrShort to determine long or short; percentInput to set the threshold percentage of price change; closePositionDays to set the number of days to hold the position.
The core logic is: calculate the increase/decrease of today relative to yesterday. If it reaches the input threshold percentage, a trading signal is triggered. If it's a long signal, when today's price increases more than threshold relative to yesterday, go long. If it's a short signal, when today's price decreases more than threshold relative to yesterday, go short.
After going long/short, the entry day and next 4 days will be marked with colors on the chart. The position will be closed automatically after 4 days.
Advantages
- Using moving average crossovers to determine trend is a mature and reliable method
- Simple and clear strategy logic, easy to understand and implement
- The frequency can be controlled by adjusting parameters
- The automatic stop loss mechanism effectively controls risks
Risks
- Moving averages have lagging effects, may miss best timing of rapid price changes
- Significant price swings may happen in short term, generating unnecessary signals
- Inappropriate parameter settings may affect strategy performance
- Unable to respond effectively to impacts of unexpected events
Risk management:
- Optimize moving average parameters, longer periods help filter noise
- Increase threshold percentage to reduce unnecessary trades
- Test different holding periods to control single loss
- Combine with other indicators to further confirm signals
Optimization Directions
- Consider using EMA, DMA instead of SMA to make it more sensitive
- Add stop loss mechanisms, e.g. stop loss when breaking the average
- Add other technical indicators like MACD, KDJ for combination, improving win rate
- Try machine learning methods to auto optimize parameters
- Optimize entry and exit timing, such as breakout, etc.
Summary
The moving average crossover strategy is a very simple and practical quantitative trading strategy. By judging the relationship between short-term and long-term trends, it profits from the trending nature of asset prices. This strategy is easy to implement with clear logic, and forms the foundation of many quantitative trading strategies. We can obtain better performance through parameter tuning and optimizations. But we also need to manage risks and avoid misuse.
Parameters
| Name | Default | What it controls |
|---|---|---|
| v_input_1 | true | Long=Checked Short=Unchecked |
| v_input_2 | true | Direction of Today vs. Previous day: Up=Checked Down=Unchecked |
| v_input_3 | 4.5 | Percent |
| v_input_4 | 4 | How Many Days to Close Position |
Source Pine Script
/*backtest
start: 2023-01-01 00:00:00
end: 2023-10-11 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=3
// Created by Leon Ross
strategy(title = "DaysAfterCertainPercentChangev1", shorttitle = "DACPCv1", overlay = true,
pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 100,
calc_on_every_tick=true, initial_capital=100000)
//Inputs
longOrShort = input(title="Long=Checked Short=Unchecked", type=bool, defval=true) //long=true, down=false
upOrDown = input(title="Direction of Today vs. Previous day: Up=Checked Down=Unchecked", type=bool, defval=true) //up=true, down=false: this is the direction of days vs previous day
percentInput = input(title="Percent", type=float, defval=4.5)
closePositionDays = input(title="How Many Days to Close Position", defval=4)
//Conditions
//percentUpValue = (close / close[1]) - 1
//percentUp = percentUpValue >= (percentInput/100.0)
//upConditions = percentUp
//percentDownValue = 1- (close / close[1])
//percentDown = percentDownValue >= (percentInput/100.0)
//downConditions = percentDown
upValue = (close / close[1]) - 1
downValue = 1 - (close / close[1])
allConditions = if(upOrDown)
upValue >= (percentInput/100.0)
else
downValue >= (percentInput/100.0)
//Plots
bgcolor(allConditions ? (upOrDown ? green : red) : na, transp=70)
bgcolor(allConditions ? silver : na, transp=70, offset=1)
bgcolor(allConditions ? silver : na, transp=70, offset=2)
bgcolor(allConditions ? silver : na, transp=70, offset=3)
bgcolor(allConditions ? silver : na, transp=70, offset=4)
//bgcolor(downConditions == 1 ? red : na, transp=70)
//bgcolor(downConditions == 1 ? silver : na, transp=70, offset=1)
//bgcolor(downConditions == 1 ? silver : na, transp=70, offset=2)
//bgcolor(downConditions == 1 ? silver : na, transp=70, offset=3)
//bgcolor(downConditions == 1 ? silver : na, transp=70, offset=4)
//Entires
if(longOrShort)
strategy.entry(id = "Long", long = true, when = allConditions)
else
strategy.entry(id = "Short", long = false, when = allConditions)
//Exits
if (barssince(allConditions) == closePositionDays)
if(longOrShort)
strategy.close("Long")
else
strategy.close("Short")
Published by ChaoZhang on the FMZ strategy square · last modified 2023-10-12. 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/Crossing-the-Breakout-Strategy.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
- Bull Market Tracking SystemPine Script · Breakout · 1h
- Dual Breakout Volatility Channel StrategyPine Script · Breakout · 1h
- EMA MACD Quantitative Strategy With Dual Track Running and Leading the Market IndexPine Script · Breakout · 1d
- Kana Candle Breakout Strategy Based on Moving Average and Support ResistancePine Script · Breakout · 1h
- Momentum Breakout Moving Average StrategyPine Script · Breakout · 1d
- Momentum Breakout Trading Strategy 440964Pine Script · Breakout · 5m