Backtrader Community

    • 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 behavior adddata vs resample

    Indicators/Strategies/Analyzers
    2
    4
    1388
    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.
    • T
      ThatBlokeDave last edited by

      Hello,

      I noticed some different behavior of an indicator when I use cerebro.adddata() and cerebro.resampledata()

      The following is simple script that just prints which bar the indicator and the strategy are on at each call of next().

      import backtrader as bt
      from datetime import datetime
      
      class printTest(bt.Indicator):
          lines = ('signal',)
      
          def __init__(self):
              pass
      
          def next(self):
              print('The Indicator bar is {}'.format(len(self.data)))
      
      
      class test(bt.Strategy):
          def __init__(self):
              self.ind = printTest(subplot=False)
      
          def next(self):
              #print('Dataclose = {}'.format(self.data.close[0]))
              print('The Strategy bar is {}'.format(len(self.data)))
      
      
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(test)
      
      fromdate = datetime(2016,1,2)
      todate = datetime(2016,1,4)
      datapath = '../data/csv/forex/GBP_USD/GBPUSD_m1_Ask_2016-2017.csv'
      data0 = bt.feeds.GenericCSVData(
          timeframe=bt.TimeFrame.Minutes,
          compression=1,
          dataname=datapath,
          nullvalue=0.0,
          dtformat=('%m/%d/%Y'),
          tmformat=('%H:%M:%S'),
          fromdate=fromdate,
          todate=todate,
          datetime=0,
          time=1,
          high=3,
          low=4,
          open=2,
          close=5,
          volume=6,
          openinterest=-1 #-1 means not used
          )
      
      cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes,
                              compression=60)
      
      #cerebro.adddata(data0)
      
      cerebro.run()
      cerebro.plot(style='candle')
      

      When I resample the data I get the following output.

      The Indicator bar is 1
      The Strategy bar is 1
      The Indicator bar is 2
      The Strategy bar is 2
      The Indicator bar is 3
      The Strategy bar is 3
      The Indicator bar is 4
      The Strategy bar is 4
      The Indicator bar is 5
      The Strategy bar is 5
      

      This is what I would expect according to my knowledge.

      However if I comment out the resample lines and uncomment adddata line, my output is as follows (I shortened it to make it easier to see what I am showing)

      The Indicator bar is 186
      The Indicator bar is 187
      The Indicator bar is 188
      The Indicator bar is 189
      The Indicator bar is 190
      The Indicator bar is 191
      The Indicator bar is 192
      The Indicator bar is 193
      The Indicator bar is 194
      The Indicator bar is 195
      The Strategy bar is 1
      The Strategy bar is 2
      The Strategy bar is 3
      The Strategy bar is 4
      The Strategy bar is 5
      The Strategy bar is 6
      The Strategy bar is 7
      The Strategy bar is 8
      The Strategy bar is 9
      The Strategy bar is 10
      The Strategy bar is 11
      The Strategy bar is 12
      The Strategy bar is 13
      The Strategy bar is 14
      The Strategy bar is 15
      The Strategy bar is 16
      The Strategy bar is 17
      

      backtrader seems to loop through the Indicator's next() calls first and then move onto the strategies next() calls second.

      Is this expected behavior?

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @ThatBlokeDave last edited by

        @ThatBlokeDave said in Indicator behavior adddata vs resample:

        print('The Strategy bar is {}'.format(len(self.data)))
        

        This is not 100% accurate. len(self) is the actual length of the strategy. In your case and with only 1 data feed, both will be the same. With multiple data feeds it may not be the case (when the feeds are not perfectly aligned in time) Nothing important in this case.

        The default mode of the platform is runonce=True which pre-calculates indicators in batch mode in a single pass over the data (broken down in 3 parts for pre-minimum period, the minimum period and then anything beyond that)

        See this: Docs - Operating the platform

        With adddata and unless you change it this holds true.

        But this behavior is automatically disabled under some circumstances and the calculation of the indicators happens synchronously with that of the strategy. Not only with the use case you describe above. The batch calculation mode is also disabled for live feeds for example.

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          See also this thread: Community - Is backtrader event driven or vectorized?

          1 Reply Last reply Reply Quote 0
          • T
            ThatBlokeDave last edited by

            @backtrader

            Thanks, I will take a look.

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