Feed custom indicator data into new indicator?
I have made a donchian channel indicator, and now I want to feed the channel into a simple directional movement type of indicator to plot this in a separate plot.
So the Donchian Channel is from the tutorial:
class DonchianChannels(bt.Indicator): alias = ('DCH', 'DonchianChannel',) lines = ('dcm', 'dch', 'dcl',) # dc middle, dc high, dc low params = dict( period=20, lookback=0, # 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
and the directional indicator is just like this:
class DCDir(bt.Indicator): alias = ('DC_Dir',) lines = ('dc',) def _plotinit(self): self.plotinfo.plotyhlines =  def __init__(self): if len(self.datas[-1].dch) > 0: self.lines.dc = self.datas.dch - self.datas.dch[-1]
I'm trying to init these in the init of my strategy (this is where I try to feed the data from the DC to the DCDir indicator:
self.donchian = DonchianChannels() self.dc_dir = DCDir(self.donchian)
But I don't understand the data structure in DCDir. The donchian-object I'm trying to pass to the DCDir indicator, does it need a specific structure to be parsed correctly?
run-out last edited by
if len(self.datas[-1].dch) > 0:
Your troubles are in here. Have a look at this recent post halfway down the page. There's an explanation how init/next and  () work.
self.datasis a list of datas and inside the indicator you only have the one data fed in. I'm not sure what you are checking here probably just if the previous high exists? If so, you can probably just dispense with this line.
@run-out Thanks! That explanation made it very clear. This was what I was trying to create:
An indicator that shows positive spikes when the donchian channel increases and negative spikes when it decreases.