<jupyter_start><jupyter_text>Donchian ChannelThe Donchian channel is an indicator used in market trading developed by <NAME>. It is formed by taking the highest high and the lowest low of the last n periods. The area between the high and the low is the channel for the period chosen. See [Donchian channel - Wikipedia](https://en.wikipedia.org/wiki/Donchian_channel).<jupyter_code>%matplotlib inline

from typing import List, Tuple

import backtrader as bt
import pandas as pd
from matplotlib import pyplot as plt<jupyter_output><empty_output><jupyter_text>Read Data<jupyter_code>BAR_SIZE = 3600000 # 1H time bar
TIME_BAR_DIR = f'/data/bars/TimeBar/{BAR_SIZE}'
from utils import read_bars as read_bars_tmp

def read_bars(csv_file: str)->pd.DataFrame:
    TIME_BEGIN = pd.to_datetime('2020-07-01T00:00:00.000Z')
    TIME_END = pd.to_datetime('2020-07-30T00:00:00.000Z')
    bars_df = read_bars_tmp(csv_file)
    bars_df = bars_df[(bars_df['timestamp'] >= TIME_BEGIN) & (bars_df['timestamp'] < TIME_END)]
    return bars_df
time_bars = read_bars(f'/data/bars/TimeBar/{BAR_SIZE}/TimeBar.{BAR_SIZE}.Binance.Swap.BTC_USDT.csv')
time_bars['vwap'].plot(figsize=(16, 8))
from utils import CryptoPandasData

data_feed = CryptoPandasData(dataname=time_bars, timeframe=bt.TimeFrame.Minutes)<jupyter_output><empty_output><jupyter_text>Donchian Channel<jupyter_code>class DonchianChannelsIndicator(bt.Indicator):
    '''Donchian channel.'''

    alias = ('DCH', 'DonchianChannel',)

    lines = ('dcm', 'dch', 'dcl',)  # dc middle, dc high, dc low

    params = (
        ('period', 20), # lookback period
    )

    plotinfo = dict(subplot=False)  # plot along with data
    plotlines = dict(
        dcm=dict(ls='--'),  # dashed line
        dch=dict(_samecolor=True),  # use same color as prev line (dcm)
        dcl=dict(_samecolor=True),  # use same color as prev line (dch)
    )

    def __init__(self):
        hi, lo = self.data.high(-1), self.data.low(-1) # start from previous bar, current bar not considered
        self.l.dch = bt.indicators.Highest(hi, period=self.p.period)
        self.l.dcl = bt.indicators.Lowest(lo, period=self.p.period)
        self.l.dcm = (self.l.dch + self.l.dcl) / 2.0  # avg of the above<jupyter_output><empty_output><jupyter_text>Demo<jupyter_code>class DummyStrategy(bt.Strategy):
    def __init__(self):
        self.dc = DonchianChannelsIndicator()
cerebro = bt.Cerebro()
cerebro.addstrategy(DummyStrategy)
cerebro.adddata(data_feed)
cerebro.run()

plt.rcParams['figure.figsize'] = (16, 8)
cerebro.plot(iplot=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1256788207913111, 'fraction_numerical': 0.02094647013188518, 'mean_word_length': 4.558189655172414, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '106670', 'license': 'MIT', 'n_tokens_mistral': 975, 'n_tokens_neox': 928, 'n_words': 201, 'path': 'samples/crypto-notebooks-master/backtest/indicators/Donchian-Channel.ipynb', 'repo_name': 'webclinic017/Backtrader-14', 'size': '2588'}