Donchian Channel Strategy
Explanation & Rationale
The Donchian Channel Breakout Strategy identifies strong price breakouts by tracking the highest high and lowest low over the past 20 days. A buy signal occurs when the price exceeds the 20-day high, signaling upward momentum, while a short signal is triggered when the price falls below the 20-day low, indicating downward momentum. This approach is widely used in trend-following systems, as it helps traders capture sustained price movements while avoiding choppy market conditions.
Code
'''Donchian Channel Breakout Strategy.
Buy when the price breaks above the 20-day high.Short when the price breaks below the 20-day low.This strategy aims to capture breakout momentum.Learn more @ docs.ubacktest.com/examples/other-indicators/donchianchannel'''
import pandas as pdimport numpy as np
def calculate_donchian(series, window):
# Calculate both high and low Donchian Channel values and shift them by 1 to capture previous n days' high/low return series.rolling(window=window).max().shift(1), series.rolling(window=window).min().shift(1)
def strategy(data): # Calculate Donchian High and Low in one step data['Donchian_High'], data['Donchian_Low'] = calculate_donchian(data['close'], window=20)
# Generate breakout signals data['signal'] = np.nan data.loc[data['close'] > data['Donchian_High'], 'signal'] = 1 data.loc[data['close'] < data['Donchian_Low'], 'signal'] = -1
return data