Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Feed custom indicator data into new indicator?

    Indicators/Strategies/Analyzers
    2
    3
    102
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hoflz5 last edited by

      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 = [0]
      
          def __init__(self):
              if len(self.datas[-1].dch) > 0:
                  self.lines.dc = self.datas[0].dch[0] - self.datas[0].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 1 Reply Last reply Reply Quote 0
      • run-out
        run-out @hoflz5 last edited by

        @hoflz5 said in Feed custom indicator data into new indicator?:

        def init(self):
        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.datas is 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.

        RunBacktest.com

        H 1 Reply Last reply Reply Quote 0
        • H
          hoflz5 @run-out last edited by

          @run-out Thanks! That explanation made it very clear. This was what I was trying to create: Screenshot 2021-03-08 at 14.18.43.png

          An indicator that shows positive spikes when the donchian channel increases and negative spikes when it decreases.

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors