Created
September 4, 2019 12:12
-
-
Save mementum/1adc2aea1102f222bfa8b93ef892aae8 to your computer and use it in GitHub Desktop.
Backtrader - DonchianChannels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment