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/

    indicator slicing

    General Code/Help
    2
    7
    2633
    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
      hobart-liu last edited by

      I developed one indicator, and try to use this indictor in another indictor, and I get slicing problem. Please help check am I doing this properly?

      import backtrader as bt
      import numpy as np
      import datetime
      
      class A(bt.Indicator):
          lines = ('avg',)
          def __init__(self, close):
              self.close = close
              self.addminperiod(3)
          def next(self):
              self.lines.avg[0] = np.mean(self.close.get(size=3).tolist())
      
      
      class B(bt.Indicator):
          lines =('disp_avg_hist',)
          def __init__(self, avg):
              self.avg = avg
              self.addminperiod(2)
          def next(self):
              print(self.avg.get(size=3).tolist())
      
      
      
      class TestStrategy(bt.Strategy):
          def __init__(self):
              self.dataclose = self.datas[0].close
              self.avg = A(close=self.dataclose)
              self.xxx = B(avg = self.avg.avg)
      
          def next(self):
              print(self.avg.avg.get(size=2))
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      data = bt.feeds.YahooFinanceCSVData(
          dataname='orcl-1995-2014.txt',
          fromdate=datetime.datetime(2000, 1, 1),
          todate=datetime.datetime(2000, 12, 31),
          reverse=False)
      cerebro.adddata(data)
      cerebro.run()
      

      The first part of printing message shows classB.next() doesn't doing get(size=3) properly.
      The second part of printing message shows TestStrategy.next() works as expected...

      1 Reply Last reply Reply Quote 0
      • P
        Paska Houso last edited by

        I fail to:

        • See the printout
        • See the meaning of the indicators (there is for sure one in your plans and ideas)

        Bypassing the indicators and values of the data you are not letting the platform work. Your indicators have no proper references to work with. My recommendation would be to read this:

        • https://www.backtrader.com/docu/concepts.html
        1 Reply Last reply Reply Quote 0
        • H
          hobart-liu last edited by hobart-liu

          1. I have read the document several times, not only for this problem but also for other problems I met before. I am not sure whether this document can help. If yes, could you help point it out?

          2. The printing message is just to show get(size=x) doesn't work in my code. I can see self.dataclose of class A works fine, there must be something I didn't do properly, I came here for help.

          3. I cannot understand why cannot you copy/paste and run it in your laptop? This is the log,

            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            array('d', [nan, 27.316666666666666])
            array('d', [27.316666666666666, 25.473333333333333])
            array('d', [25.473333333333333, 25.113333333333333])
            array('d', [25.113333333333333, 26.26])
            array('d', [26.26, 27.623333333333335])
            array('d', [27.623333333333335, 27.813333333333333])
            array('d', [27.813333333333333, 26.923333333333332])
            array('d', [26.923333333333332, 26.459999999999997])
            array('d', [26.459999999999997, 26.926666666666666])
            array('d', [26.926666666666666, 27.689999999999998])
            array('d', [27.689999999999998, 28.66333333333333])
            array('d', [28.66333333333333, 29.34])
            array('d', [29.34, 28.849999999999998])
            array('d', [28.849999999999998, 28.383333333333336])
            array('d', [28.383333333333336, 27.613333333333333])
            array('d', [27.613333333333333, 27.22])
            array('d', [27.22, 25.709999999999997])
            array('d', [25.709999999999997, 24.86])
            array('d', [24.86, 25.223333333333333])
            array('d', [25.223333333333333, 26.38])
            array('d', [26.38, 27.5])
            array('d', [27.5, 28.136666666666667])
            array('d', [28.136666666666667, 29.073333333333334])
            array('d', [29.073333333333334, 29.55333333333333])
            array('d', [29.55333333333333, 29.906666666666666])
            array('d', [29.906666666666666, 30.30333333333333])
            array('d', [30.30333333333333, 30.323333333333334])
            array('d', [30.323333333333334, 30.69666666666667])
            array('d', [30.69666666666667, 30.540000000000003])
            array('d', [30.540000000000003, 30.8])
            array('d', [30.8, 30.706666666666667])
            array('d', [30.706666666666667, 30.236666666666668])
            array('d', [30.236666666666668, 29.916666666666668])
            array('d', [29.916666666666668, 30.156666666666666])
            array('d', [30.156666666666666, 30.72])
            array('d', [30.72, 32.60333333333333])
            array('d', [32.60333333333333, 33.53])
            array('d', [33.53, 35.580000000000005])
            array('d', [35.580000000000005, 35.72666666666667])
            array('d', [35.72666666666667, 35.70666666666667])
            array('d', [35.70666666666667, 35.833333333333336])
            array('d', [35.833333333333336, 36.54333333333333])
            array('d', [36.54333333333333, 37.61666666666667])
            array('d', [37.61666666666667, 38.97])
            array('d', [38.97, 40.343333333333334])
            array('d', [40.343333333333334, 41.45666666666667])
            array('d', [41.45666666666667, 40.73])
            array('d', [40.73, 39.56333333333333])
            array('d', [39.56333333333333, 39.06333333333333])
            array('d', [39.06333333333333, 39.593333333333334])
            array('d', [39.593333333333334, 40.06333333333333])
            array('d', [40.06333333333333, 39.98])
            array('d', [39.98, 39.77])
            array('d', [39.77, 40.47666666666667])
            array('d', [40.47666666666667, 41.93666666666667])
            array('d', [41.93666666666667, 42.99])
            array('d', [42.99, 43.72])
            array('d', [43.72, 43.666666666666664])
            array('d', [43.666666666666664, 42.916666666666664])
            array('d', [42.916666666666664, 41.25])
            array('d', [41.25, 39.833333333333336])
            array('d', [39.833333333333336, 38.89666666666667])
            array('d', [38.89666666666667, 38.48])
            array('d', [38.48, 38.51])
            array('d', [38.51, 39.39333333333334])
            array('d', [39.39333333333334, 41.25666666666667])
            array('d', [41.25666666666667, 41.96666666666667])
            array('d', [41.96666666666667, 41.166666666666664])
            array('d', [41.166666666666664, 38.833333333333336])
            array('d', [38.833333333333336, 37.07333333333333])
            array('d', [37.07333333333333, 34.593333333333334])
            array('d', [34.593333333333334, 34.876666666666665])
            array('d', [34.876666666666665, 36.04333333333333])
            array('d', [36.04333333333333, 38.053333333333335])
            array('d', [38.053333333333335, 37.38666666666666])
            array('d', [37.38666666666666, 36.303333333333335])
            array('d', [36.303333333333335, 36.47])
            array('d', [36.47, 36.696666666666665])
            array('d', [36.696666666666665, 37.51])
            array('d', [37.51, 38.24])
            array('d', [38.24, 39.49])
            array('d', [39.49, 39.57333333333333])
            array('d', [39.57333333333333, 38.88666666666666])
            array('d', [38.88666666666666, 37.98])
            array('d', [37.98, 37.81333333333333])
            array('d', [37.81333333333333, 37.23])
            array('d', [37.23, 36.85666666666666])
            array('d', [36.85666666666666, 35.32333333333333])
            array('d', [35.32333333333333, 35.333333333333336])
            array('d', [35.333333333333336, 35.696666666666665])
            array('d', [35.696666666666665, 37.26])
            array('d', [37.26, 38.39333333333334])
            array('d', [38.39333333333334, 39.06])
            array('d', [39.06, 38.403333333333336])
            array('d', [38.403333333333336, 36.88333333333333])
            array('d', [36.88333333333333, 35.156666666666666])
            array('d', [35.156666666666666, 33.416666666666664])
            array('d', [33.416666666666664, 32.446666666666665])
            array('d', [32.446666666666665, 32.21666666666666])
            array('d', [32.21666666666666, 32.946666666666665])
            array('d', [32.946666666666665, 34.60333333333333])
            array('d', [34.60333333333333, 35.51])
            array('d', [35.51, 37.32333333333333])
            array('d', [37.32333333333333, 38.32333333333333])
            array('d', [38.32333333333333, 39.833333333333336])
            array('d', [39.833333333333336, 39.696666666666665])
            array('d', [39.696666666666665, 39.666666666666664])
            array('d', [39.666666666666664, 39.906666666666666])
            array('d', [39.906666666666666, 40.843333333333334])
            array('d', [40.843333333333334, 40.93666666666667])
            array('d', [40.93666666666667, 40.82])
            array('d', [40.82, 40.20666666666667])
            array('d', [40.20666666666667, 40.53])
            array('d', [40.53, 40.56333333333333])
            array('d', [40.56333333333333, 41.73])
            array('d', [41.73, 42.32])
            array('d', [42.32, 43.03666666666667])
            array('d', [43.03666666666667, 42.28666666666667])
            array('d', [42.28666666666667, 41.196666666666665])
            array('d', [41.196666666666665, 40.61333333333334])
            array('d', [40.61333333333334, 40.73666666666667])
            array('d', [40.73666666666667, 41.31])
            array('d', [41.31, 41.01])
            array('d', [41.01, 41.31333333333333])
            array('d', [41.31333333333333, 40.85333333333333])
            array('d', [40.85333333333333, 39.42666666666667])
            array('d', [39.42666666666667, 38.02])
            array('d', [38.02, 37.303333333333335])
            array('d', [37.303333333333335, 37.60333333333333])
            array('d', [37.60333333333333, 37.053333333333335])
            array('d', [37.053333333333335, 36.85333333333333])
            array('d', [36.85333333333333, 37.126666666666665])
            array('d', [37.126666666666665, 37.76])
            array('d', [37.76, 38.0])
            array('d', [38.0, 37.73666666666667])
            array('d', [37.73666666666667, 37.343333333333334])
            array('d', [37.343333333333334, 37.67666666666667])
            array('d', [37.67666666666667, 37.88666666666666])
            array('d', [37.88666666666666, 38.093333333333334])
            array('d', [38.093333333333334, 37.74])
            array('d', [37.74, 37.96])
            array('d', [37.96, 37.97])
            array('d', [37.97, 37.36666666666667])
            array('d', [37.36666666666667, 37.10333333333333])
            array('d', [37.10333333333333, 36.78])
            array('d', [36.78, 36.903333333333336])
            array('d', [36.903333333333336, 37.28])
            array('d', [37.28, 38.68666666666667])
            array('d', [38.68666666666667, 40.14666666666667])
            array('d', [40.14666666666667, 41.07333333333333])
            array('d', [41.07333333333333, 41.04333333333333])
            array('d', [41.04333333333333, 40.74])
            array('d', [40.74, 40.42666666666667])
            array('d', [40.42666666666667, 40.593333333333334])
            array('d', [40.593333333333334, 40.79])
            array('d', [40.79, 40.800000000000004])
            array('d', [40.800000000000004, 41.06])
            array('d', [41.06, 41.07333333333333])
            array('d', [41.07333333333333, 41.406666666666666])
            array('d', [41.406666666666666, 41.343333333333334])
            array('d', [41.343333333333334, 41.60333333333333])
            array('d', [41.60333333333333, 41.85333333333333])
            array('d', [41.85333333333333, 42.03])
            array('d', [42.03, 42.67666666666667])
            array('d', [42.67666666666667, 43.19])
            array('d', [43.19, 43.79333333333333])
            array('d', [43.79333333333333, 44.49])
            array('d', [44.49, 45.300000000000004])
            array('d', [45.300000000000004, 45.77])
            array('d', [45.77, 45.48666666666667])
            array('d', [45.48666666666667, 45.24666666666667])
            array('d', [45.24666666666667, 44.49666666666667])
            array('d', [44.49666666666667, 43.53])
            array('d', [43.53, 41.56333333333333])
            array('d', [41.56333333333333, 40.77333333333333])
            array('d', [40.77333333333333, 41.02333333333333])
            array('d', [41.02333333333333, 40.846666666666664])
            array('d', [40.846666666666664, 39.953333333333326])
            array('d', [39.953333333333326, 39.01666666666666])
            array('d', [39.01666666666666, 39.28666666666666])
            array('d', [39.28666666666666, 39.699999999999996])
            array('d', [39.699999999999996, 39.93666666666667])
            array('d', [39.93666666666667, 39.74])
            array('d', [39.74, 39.82333333333333])
            array('d', [39.82333333333333, 39.68])
            array('d', [39.68, 40.13333333333333])
            array('d', [40.13333333333333, 40.02])
            array('d', [40.02, 39.833333333333336])
            array('d', [39.833333333333336, 37.836666666666666])
            array('d', [37.836666666666666, 36.06333333333333])
            array('d', [36.06333333333333, 34.32333333333333])
            array('d', [34.32333333333333, 34.01])
            array('d', [34.01, 33.78333333333333])
            array('d', [33.78333333333333, 33.166666666666664])
            array('d', [33.166666666666664, 32.27])
            array('d', [32.27, 31.643333333333334])
            array('d', [31.643333333333334, 32.74666666666667])
            array('d', [32.74666666666667, 33.89333333333334])
            array('d', [33.89333333333334, 34.623333333333335])
            array('d', [34.623333333333335, 33.93666666666667])
            array('d', [33.93666666666667, 34.54333333333333])
            array('d', [34.54333333333333, 35.06333333333333])
            array('d', [35.06333333333333, 35.23])
            array('d', [35.23, 35.04])
            array('d', [35.04, 34.75])
            array('d', [34.75, 34.75])
            array('d', [34.75, 34.21])
            array('d', [34.21, 33.29])
            array('d', [33.29, 32.93666666666667])
            array('d', [32.93666666666667, 32.0])
            array('d', [32.0, 31.313333333333333])
            array('d', [31.313333333333333, 30.416666666666668])
            array('d', [30.416666666666668, 29.27])
            array('d', [29.27, 28.27])
            array('d', [28.27, 26.436666666666667])
            array('d', [26.436666666666667, 26.186666666666667])
            array('d', [26.186666666666667, 25.813333333333333])
            array('d', [25.813333333333333, 25.793333333333333])
            array('d', [25.793333333333333, 26.189999999999998])
            array('d', [26.189999999999998, 27.336666666666662])
            array('d', [27.336666666666662, 28.213333333333335])
            array('d', [28.213333333333335, 28.356666666666666])
            array('d', [28.356666666666666, 26.98])
            array('d', [26.98, 25.813333333333333])
            array('d', [25.813333333333333, 23.646666666666665])
            array('d', [23.646666666666665, 23.436666666666667])
            array('d', [23.436666666666667, 23.183333333333334])
            array('d', [23.183333333333334, 23.3])
            array('d', [23.3, 22.886666666666667])
            array('d', [22.886666666666667, 24.013333333333332])
            array('d', [24.013333333333332, 25.27333333333333])
            array('d', [25.27333333333333, 27.043333333333333])
            array('d', [27.043333333333333, 28.709999999999997])
            array('d', [28.709999999999997, 29.959999999999997])
            array('d', [29.959999999999997, 30.0])
            array('d', [30.0, 29.52])
            array('d', [29.52, 30.103333333333335])
            array('d', [30.103333333333335, 30.916666666666668])
            array('d', [30.916666666666668, 30.356666666666666])
            array('d', [30.356666666666666, 28.876666666666665])
            array('d', [28.876666666666665, 28.146666666666665])
            array('d', [28.146666666666665, 29.353333333333335])
            array('d', [29.353333333333335, 30.393333333333334])
            array('d', [30.393333333333334, 30.373333333333335])
            array('d', [30.373333333333335, 29.540000000000003])
            array('d', [29.540000000000003, 29.959999999999997])
            array('d', [29.959999999999997, 30.77333333333333])
            array('d', [30.77333333333333, 31.17])
            array('d', [31.17, 30.896666666666665])
            array('d', [30.896666666666665, 30.27])

            Process finished with exit code 0

          P 1 Reply Last reply Reply Quote 0
          • P
            Paska Houso @hobart-liu last edited by backtrader

            @hobart-liu said in indicator slicing:

            1. I cannot understand why cannot you copy/paste and run it in your laptop?
            1. I may not want to
            2. I may not have a Python environment here to run it
            3. I may not be here to do your backtesting/development
            4. I may (we could go up to n arguments ...)

            @hobart-liu said in indicator slicing:

            1. The printing message is just to show get(size=x) doesn't work in my code. I can see self.dataclose of class A works fine, there must be something I didn't do properly, I came here for help.

            And that's where the log may (or may not) help. We cannot see what's happening on your side.

            You can always paste reduced version of your log (like 2 first / 2 last lines) to avoid cluttering.

            get(size=x) is obviously working. From your log:

            @hobart-liu said in indicator slicing:

            ...
            [31.17, 30.896666666666665, 30.27]
            [31.17, 30.896666666666665, 30.27]
            ...
            

            You asked for 3 items and you get 3 items. The problem is not whether it's working or not, it is that it's not giving you what you are expecting.

            @hobart-liu said in indicator slicing:

            class B(bt.Indicator):
                lines =('disp_avg_hist',)
                def __init__(self, avg):
            

            Because you are breaking things there (actually you are breaking them in A too). And you know you broke them because you explicitly did this later:

            @hobart-liu said in indicator slicing:

            class TestStrategy(bt.Strategy):
                def __init__(self):
                    ...
                    self.xxx = B(avg = self.avg.avg)
            

            Where you had to do: avg = self.avg.avg, because if not you would be getting an error complaining about a missing parameter avg for the __init__ method of B.

            @hobart-liu said in indicator slicing:

            1. I have read the document several times, not only for this problem but also for other problems I met before. I am not sure whether this document can help. If yes, could you help point it out?

            You may then want to read this section of the document here.

            • https://www.backtrader.com/docu/concepts.html#data-feeds-passing-them-around

            Instead of letting the platform pass the data feeds, you are forcing them as arguments. For B, you could do:

            class B(bt.Indicator):
                lines = ('xxx',)
            
                def next(self):
                    print(self.data.get(size=3).tolist())
            
            class TestStrategy(bt.Strategy):
                def __init__(self):
                    ...
                    self.xxx = B(self.avg)
            

            And it may work without doing anything else (if the data feed given to it self.xxx has always 3 items) But remember that you have equally broken A

            1 Reply Last reply Reply Quote 1
            • H
              hobart-liu last edited by hobart-liu

              to close this question:

              import backtrader as bt
              import numpy as np
              import datetime
              
              class A(bt.Indicator):
                  lines = ('avg',)
                  def __init__(self):
                      self.addminperiod(3)
                  def next(self):
                      self.lines.avg[0] = np.mean(self.datas[0].close.get(size=3).tolist())
              
              
              class B(bt.Indicator):
                  lines = ('avg',)
                  def __init__(self):
                      self.addminperiod(5)
                  def next(self):
                      self.lines.avg[0] = np.mean(self.datas[0].close.get(size=5).tolist())
              
              class C(bt.Indicator):
                  lines =('xxx',)
                  def __init__(self):
                      pass
                  def next(self):
                      print([a-b for a,b in zip(self.datas[0].avg.get(size=3).tolist(), self.datas[1].avg.get(size=3).tolist())])
              
              class TestStrategy(bt.Strategy):
                  def __init__(self):
                      self.avg3 = A(self.datas[0])
                      self.avg5 = B(self.datas[0])
                      self.delta = C(self.avg3, self.avg5)
              
                  def next(self):
                      pass
              
              cerebro = bt.Cerebro()
              cerebro.addstrategy(TestStrategy)
              data = bt.feeds.YahooFinanceCSVData(
                  dataname='orcl-1995-2014.txt',
                  fromdate=datetime.datetime(2000, 1, 1),
                  todate=datetime.datetime(2000, 1, 31),
                  reverse=False)
              cerebro.adddata(data)
              cerebro.run()
              
              1 Reply Last reply Reply Quote 1
              • P
                Paska Houso last edited by Paska Houso

                Although the goal may be other in your case (because C is just printing and not really doing anything sensible), what about such a C

                class C(bt.ind.PeriodN):
                    lines = ('xxx',)
                    def __init__(self):
                        self.lines.xxx = bt.ind.SumN(self.data0, period=self.p.period) - bt.ind.SumN(self.data1, period=self.p.period)
                

                Which is the sum of your [a - b for ...] and could be an expected reasonable target and is made in a generic way, not relying on hard coded values and calling get.

                1 Reply Last reply Reply Quote 0
                • H
                  hobart-liu last edited by

                  Thanks Paska.

                  Yes, the main purpose is to use get(size=xxx) in a right way. Time sequence alg usually needs to check n days back.

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