For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Indicator behavior adddata vs resample

  • 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):
        def next(self):
            print('The Indicator bar is {}'.format(len(
    class test(bt.Strategy):
        def __init__(self):
            self.ind = printTest(subplot=False)
        def next(self):
            #print('Dataclose = {}'.format([0]))
            print('The Strategy bar is {}'.format(len(
    cerebro = bt.Cerebro()
    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(
        openinterest=-1 #-1 means not used
    cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes,

    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?

  • administrators

    @ThatBlokeDave said in Indicator behavior adddata vs resample:

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

    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.

  • administrators

  • @backtrader

    Thanks, I will take a look.

Log in to reply