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

Resampling from 1 minute bar to 1 day bar not working properly in latest version

  • I have a 1 minute data as input source and I am resampling it into 3 timeframes: 15 minutes, 60 minutes and 1 day respectively. The code below is running fine in and messages can be printed out. In the latest version though the code produces no message in next(), unless daily data related lines (i.e. line 24, 38 and 42 in the code) are commented out.

    I also tried using live data as input source and the issue was still there. Please take a look.

    By the way I couldn't find "Issues" button on your Github so I am just posting the issue here.

    import backtrader as bt
    import backtrader.indicators as btind
    import pandas as pd
    def parseCsvFile(csvFile, startTime,endTime):
        df=df[(df.index>startTime) & (df.index<endTime)]
        return df
    def runCerebroTest():
        startTime="2018-06-20 05:00:00"
        endTime="2018-08-20 05:00:00"
        cerebro = bt.Cerebro(stdstats=False, writer=False)
        data=bt.feeds.PandasData(dataname=parseCsvFile(csvFile,startTime,endTime) ,timeframe=bt.TimeFrame.Minutes,)
    class testStrategy(bt.Strategy):
        def log(self, txt, dt=None):
            ''' Logging function for this strategy'''
            dt = dt or
            print('%s, %s' % (dt, txt))
        def __init__(self):
            self.dataclose = self.dnames['1min_bar'].close
            self.atr_daily= btind.ATR(self.dnames['1day_bar'], period=5)
        def next(self):
            self.log("1 min data {}".format(self.dataclose[0]))
            self.log("1 hour data {}".format(self.emaShort[0]))
            self.log("1 day data {}".format(self.atr_daily[0]))
    if __name__ == '__main__':
        print("running test")

  • Bump. This is a clear issue and nobody has responded yet.

  • I noticed this myself yesterday. The result was a single bar?

    A workaround would be to do the resample in Pandas:

        df_1min = pd.read_csv(csvFile)
        method = {
                    'Open': 'first',
                    'High': 'max',
                    'Low': 'min',
                    'Close': 'last'
        df_1day = df_1min.resample('D').apply(method)
        data_1day = bt.feeds.PandasData(dataname=df_1day)

  • @bishbashbosh Thanks, I may try your Pandas way later.

    I've also found another workaround is from the below post that works for live data. Add the same data feed twice and then re sample respectively.

    data0 = ibstore.getdata('FOO')  # data feed for 1-minute resampling
    cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=1)
    data1 = ibstore.getdata('FOO')  # data feed for 1-day resampling
    cerebro.resampledata(data1, timeframe=bt.TimeFrame.Days, compression=1)

  • administrators

  • @backtrader This is awesome! Thanks for the quick fix.

Log in to reply