0bps

Strategy vault / Breakout

Donchian Channel Breakout Strategy 427308

This strategy uses the Donchian Channel indicator to trade breakouts of the upper and lower bands, enabling trend following operations across stocks/futures/crypto/forex etc, belonging to medium-to-long-term trend…

Author
ChaoZhang
Complexity
Starter · 53 lines
Parameters
6
Backtest bar
3m · intraday
Tested on
BTC/USDT · futures

Overview

This strategy uses the Donchian Channel indicator to trade breakouts of the upper and lower bands, enabling trend following operations across stocks/futures/crypto/forex etc, belonging to medium-to-long-term trend breakout strategies.

Strategy Logic

  1. Calculate the highest high and lowest low over a given period (e.g. 20 days) to get the upper and lower bands.

  2. The midline is the average of the upper and lower bands. Breaking upper band signals uptrend, breaking lower band signals downtrend.

  3. When price closes above upper band, determine uptrend has started, go long to enter.

  4. When price breaks below midline, take profit to exit.

  5. Can reference backtest timeframe to generate actual trading signals.

  6. Optionally, breaking lower band can also act as short signal.

The strategy determines trend start by channel breakouts, uses midline as profit taking exit, capturing mid-to-long term trends. Channel parameters can be adjusted to fit the market.

Advantage Analysis

  1. Donchian Channel is simple to calculate and implement.

  2. Price breaking channel signals trend change.

  3. Midline as profit taking level is reasonably set.

  4. Clear signal rules, easy to execute.

  5. Can flexibly adjust channel parameters for different products and timeframes.

  6. Can evaluate long term or short term trading performance.

  7. Large expansion space, can introduce other technical indicators.

Risk Analysis

  1. Channel breakout may lag, risking missed early opportunities.

  2. Does not consider divergence before breakout, may generate false signals.

  3. Fixed midline stop loss sensitive to market volatility.

  4. Improper backtest period risks over-fitting.

  5. Lacks stop loss, need to watch out for enlarged losses.

Optimization Directions

  1. Test and optimize channel period parameters.

  2. Evaluate other MA types as stop loss lines.

  3. Add filters like volume indicators.

  4. Add moving or trailing stop loss mechanisms.

  5. Introduce machine learning to predict price breakouts.

  6. Optimize money management, set profit ratio etc.

  7. Consider combining long/short term operations or multiple products.

Summary

This strategy uses Donchian Channel to determine trend direction, trading breakouts, a typical mid-to-long term trend following approach. Optimizing channel parameters and adding other technical indicators can form a more robust breakout system. The clear and concise logic allows expansions, making it a foundational quant strategy module with great practical utility.

Parameters

NameDefaultWhat it controls
v_input_12000Backtest Start Year
v_input_2trueBacktest Start Month
v_input_3trueBacktest Start Day
v_input_42018Backtest Start Year
v_input_512Backtest Start Month
v_input_6trueBacktest Start Day

Source Pine Script

/*backtest
start: 2023-09-11 00:00:00
end: 2023-09-15 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=3
//future strategy
//strategy(title = "stub", default_qty_type = strategy.fixed, default_qty_value = 1,  overlay = true, commission_type=strategy.commission.cash_per_contract,commission_value=2)
//stock strategy
strategy(title = "dc", default_qty_type = strategy.percent_of_equity, default_qty_value = 100,  overlay = true, commission_type=strategy.commission.cash_per_contract,commission_value=.005)
//forex strategy
//strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 100,  overlay = true)
//crypto strategy
//strategy(title = "stub", default_qty_type = strategy.percent_of_equity, default_qty_value = 100,  overlay = true, commission_type=strategy.commission.percent,commission_value=.25,default_qty_value=10000)


testStartYear = input(2000, "Backtest Start Year")
testStartMonth = input(1, "Backtest Start Month")
testStartDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)

testEndYear = input(2018, "Backtest Start Year")
testEndMonth = input(12, "Backtest Start Month")
testEndDay = input(1, "Backtest Start Day")
testPeriodEnd = timestamp(testStartYear,testStartMonth,testStartDay,0,0)


testPeriod() =>
    true
    //time >= testPeriodStart  ? true : false

dcPeriod = 20

dcUpper = highest(close, dcPeriod)[1]
dcLower = lowest(close, dcPeriod)[1]
dcAverage = (dcUpper + dcLower) / 2

plot(dcLower, style=line, linewidth=3, color=red, offset=1)
plot(dcUpper, style=line, linewidth=3, color=aqua, offset=1)

plot(dcAverage, color=yellow, style=line, linewidth=1, title="Mid-Line Average")

strategy.entry("simpleBuy", strategy.long, when=close >= dcUpper)
strategy.close("simpleBuy",when=close < dcAverage)
    
//strategy.entry("simpleSell", strategy.short,when=close <= dcLower)
//strategy.close("simpleSell",when=close > dcAverage)
    

Published by ChaoZhang on the FMZ strategy square · last modified 2023-09-19. 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/Donchian-Channel-Breakout-Strategy-427308.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