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/

    Datafeed ReSample Query

    General Code/Help
    2
    2
    103
    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.
    • Priyadarshan S
      Priyadarshan S last edited by

      Hi,

      I'm trying to resample 1 min data to hourly. The Exchange time isbetween 9:15am and 3:30pm. I want the first hourly bar from 9:15am to 10:15am, second hourly bar from 10:15am to 11:15am and so on. The last hourly bar will contain data from 3:15pm to 3:30pm. How can go about to get this implemented? Below is my code which gives hourly bar from 9:15 to 10, 10 to 11 etc and finally from 3 to 3:30.

      class TestStrategy(bt.Strategy):
      
      
          def next(self):
              self.log (self.data.close[0])
      
          def log(self, txt):
              print (self.data.datetime.datetime(0),": ", txt)
                      
      
      if __name__ == '__main__':
      
          path = r'C:\Users\Priyadarshan.sathya\Documents\tata_mod.csv'
          
          cerebro = bt.Cerebro()
          cerebro.addstrategy(TestStrategy)
          
          data = btfeeds.GenericCSVData(
              dataname=path,
              timeframe = bt.TimeFrame.Minutes,
              compression = 1,
              fromdate=datetime.datetime(2019, 7, 1),
              todate=datetime.datetime(2019, 8, 6),
              sessionstart=datetime.time(9, 15),
              sessionend=datetime.time(15, 30),
              nullvalue=0.0,
          
              dtformat=('%d-%m-%Y'),
              tmformat = ('%H:%M:%S'),
          
              datetime=1,
              high=3,
              low=4,
              open=2,
              close=5,
              volume=6,
              openinterest=-1,
              time = 7
          )
          cerebro.adddata(data)
          cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes,
                                   compression=60)
      
          cerebro.broker.setcash(100000.0)
          cerebro.broker.setcommission(commission=0.001)
      
          print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          cerebro.run()
          print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @Priyadarshan S last edited by

        @Priyadarshan-S said in Datafeed ReSample Query:

        I want the first hourly bar from 9:15am to 10:15am

        That's actually not possible directly. You can always resample with pandas.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
        $(document).ready(function () { app.coldLoad(); }); }