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/

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

    General Code/Help
    3
    6
    2092
    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.
    • Z
      z_kvn last edited by z_kvn

      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 1.9.65.122 and messages can be printed out. In the latest version 1.9.66.122 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=pd.read_csv(csvFile)
          df['date']=pd.to_datetime(df['date'])
          df.set_index('date',inplace=True)
          df=df[(df.index>startTime) & (df.index<endTime)]
          return df
      
      def runCerebroTest():
          csvFile="./data/ES-201809-GLOBEX_1min_shifted_EST.csv"
          startTime="2018-06-20 05:00:00"
          endTime="2018-08-20 05:00:00"
      
          cerebro = bt.Cerebro(stdstats=False, writer=False)
          cerebro.addobserver(bt.observers.BuySell)
          cerebro.addobserver(bt.observers.Trades)
          data=bt.feeds.PandasData(dataname=parseCsvFile(csvFile,startTime,endTime) ,timeframe=bt.TimeFrame.Minutes,)
          data_1min=cerebro.adddata(data,name='1min_bar')
          data_15min=cerebro.resampledata(data,timeframe=bt.TimeFrame.Minutes,compression=15,name='15min_bar')
          data_1hr=cerebro.resampledata(data,timeframe=bt.TimeFrame.Minutes,compression=60,name='1hr_bar')
          data_1day=cerebro.resampledata(data,timeframe=bt.TimeFrame.Days,compression=1,name='1day_bar')
      
          cerebro.addstrategy(testStrategy)
          cerebro.run(runonce=False)
      
      
      class testStrategy(bt.Strategy):
          def log(self, txt, dt=None):
              ''' Logging function for this strategy'''
              dt = dt or self.data.datetime.datetime(0)
              print('%s, %s' % (dt, txt))
          def __init__(self):
              self.dataclose = self.dnames['1min_bar'].close
              self.emaShort=btind.EMA(self.dnames['1hr_bar'],period=5)
              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")
          runCerebroTest()
      
      Z 1 Reply Last reply Reply Quote 0
      • Z
        z_kvn @z_kvn last edited by

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

        1 Reply Last reply Reply Quote 0
        • B
          bishbashbosh last edited by bishbashbosh

          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)
          
          1 Reply Last reply Reply Quote 0
          • Z
            z_kvn last edited by

            @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.

            https://community.backtrader.com/topic/132/confusion-about-resampledata-and-creating-new-datas

            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)
            
            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              Please see here: https://community.backtrader.com/topic/1397/resampling-fixes-for-1-9-67-122

              Z 1 Reply Last reply Reply Quote 0
              • Z
                z_kvn @backtrader last edited by

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

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