唐奇安通道原理

唐奇安( Donchian )通道是由Richard Donchian开发的用于市场交易的指标。它是通过获取最后n 个周期的最高价和最低价形成的。高点和低点之间的区域是所选时段的通道。

如果价格稳定,Donchian 通道将相对狭窄。如果价格波动很大,Donchian 通道会更宽。

其中,最后n个周期一般 n = 20,当然也可以根据斐波那契数列13、 21、 34、 55、 89、 144、 233、 377取n的值。

把唐奇安( Donchian )通道指标当作买卖信号,可以按以下规则:(不限于以下方法,可以探索更多更好的方案)

1、当价格向上突破n个周期的最高价时,发出买入信号;

2、当价格向下突破n个周期的最低价时,发出卖出信号。

 

唐奇安通道指标backtrader类

class DonchianChannels(bt.Indicator):
    """
    Params Note:
      - `lookback` (default: -1)
        If `-1`, the bars to consider will start 1 bar in the past and the
        current high/low may break through the channel.
        If `0`, the current prices will be considered for the Donchian
        Channel. This means that the price will **NEVER** break through the
        upper/lower channel bands.
    """

    alias = ("DCH", "DonchianChannel",)

    lines = ("dcm", "dch", "dcl",)  # dc middle, dc high, dc low
    params = dict(
        period=20,
        lookback=-1,  # consider current bar or not
    )

    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, self.data.low
        if self.p.lookback:  # move backwards as needed
            hi, lo = hi(self.p.lookback), lo(self.p.lookback)

        self.l.dch = bt.ind.Highest(hi, period=self.p.period)
        self.l.dcl = bt.ind.Lowest(lo, period=self.p.period)
        self.l.dcm = (self.l.dch + self.l.dcl) / 2.0  # avg of the above