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/

    How to work with 15min ticks and resample it to 1h data

    General Code/Help
    4
    11
    555
    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.
    • prnvat8197
      prnvat8197 last edited by

      I am new to backtrader, I was going through the quickstart guide and tried implementing it with my own data which is a 15min tick. I want to convert this 15m to 60m and then run the strategy. I am a little confused about how to do that using the resampledata functionality

      # Datas are in a subfolder of the samples. Need to find where the script is
      # because it could have been called from anywhere
      modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
      datapath = os.path.join(modpath, 'M&M15m.csv')
      
      # Create a Data Feed
      data = bt.feeds.YahooFinanceCSVData(
          dataname=datapath,
          # Do not pass values before this date
          # fromdate=datetime.datetime(2000, 1, 1),
          # # Do not pass values before this date
          # todate=datetime.datetime(2000, 12, 31),
          # Do not pass values after this date
          reverse=False)
      
      # Add the Data Feed to Cerebro
      
      cerebro.resampledata(data, timeframe = bt.TimeFrame.Minutes, compression = 15)
      cerebro.adddata(data)
      
      # Set our desired cash start
      cerebro.broker.setcash(100000.0)
      
      # Print out the starting conditions
      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      
      # Run over everything
      cerebro.run()
      
      # Print out the final result
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      

      I know this is not the right way to do it so can someone correct it for me, thanks!

      run-out prnvat8197 2 Replies Last reply Reply Quote 0
      • run-out
        run-out @prnvat8197 last edited by

        @prnvat8197
        Because you are uploading 15 bars (not ticks) you need to tell backtrader the timeframe/compression right away when you create the data. Then you add the data to cerebro using resample. This is where you indicate the compression of 60 (one hour)

        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            timeframe = bt.TimeFrame.Minutes, 
            compression = 15
            reverse=False
        )
        
        # Add the Data Feed to Cerebro
        
        cerebro.resampledata(data, timeframe = bt.TimeFrame.Minutes, compression = 60)
        

        RunBacktest.com

        prnvat8197 1 Reply Last reply Reply Quote 1
        • prnvat8197
          prnvat8197 @run-out last edited by

          @run-out Yeah I tried it, but its still not printing the hourly close data as I expect it. Maybe its an issue with the format of my csv data? Can you take a look. https://drive.google.com/file/d/1JeyMrYXYN3_D3bN3scUNPQNC47IluvEm/view?usp=sharing

          Also, my code is below as a reference

          from __future__ import (absolute_import, division, print_function,
                                  unicode_literals)
          
          import datetime  # For datetime objects
          import os.path  # To manage paths
          import sys  # To find out the script name (in argv[0])
          
          # Import the backtrader platform
          import backtrader as bt
          
          
          # Create a Stratey
          class TestStrategy(bt.Strategy):
          
              def log(self, txt, dt=None):
                  ''' Logging function for this strategy'''
                  dt = dt or self.datas[0].datetime.date(0)
                  print('%s, %s' % (dt.isoformat(), txt))
          
              def __init__(self):
                  # Keep a reference to the "close" line in the data[0] dataseries
                  self.dataclose = self.datas[0].close
          
              def next(self):
                  # Simply log the closing price of the series from the reference
                  self.log('Close, %.2f' % self.dataclose[0])
          
          
          
          if __name__ == '__main__':
              # Create a cerebro entity
              cerebro = bt.Cerebro()
          
              # Add a strategy
              cerebro.addstrategy(TestStrategy)
          
              # Datas are in a subfolder of the samples. Need to find where the script is
              # because it could have been called from anywhere
              modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
              datapath = os.path.join(modpath, 'M&M15m.csv')
          
              # Create a Data Feed
             # Create a Data Feed
              data = bt.feeds.YahooFinanceCSVData(
              dataname=datapath,
              timeframe = bt.TimeFrame.Minutes, 
              compression = 15,
              reverse=False)
          
              # Add the Data Feed to Cerebro
          
              cerebro.resampledata(data, timeframe = bt.TimeFrame.Minutes, compression = 60)
          
          
              # Set our desired cash start
              cerebro.broker.setcash(100000.0)
          
              # Print out the starting conditions
              print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
              # Run over everything
              cerebro.run()
          
              # Print out the final result
              print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
          
          run-out 1 Reply Last reply Reply Quote 0
          • run-out
            run-out @prnvat8197 last edited by

            @prnvat8197 Your going to have to tell backtrader the format since your data is not in standard format. Default for backtrader is "%Y-%m-%-d %H:%M:%S which is for 2021-07-21 09:30:00 You have datetimes with 2021-07-21 9:30:00, single digit hours.

            I'm not sure off the top of my head what the formatting is for that but you can start here

            You then add in the new formatting when you load date with YahooFinanceCSVData adding in the parameter:

            dtformat="%Y-%m-%d %H:%M:%S"
            
            But you will need to change the <H>
            

            RunBacktest.com

            prnvat8197 1 Reply Last reply Reply Quote 1
            • prnvat8197
              prnvat8197 @run-out last edited by

              @run-out I updated the csv file to the new format https://drive.google.com/file/d/16q6dsFIUEqQofqEA51QfxefJGKM6jeCT/view?usp=sharing formatting was strftime('%Y-%m-%d %I:%M:%S')

              However, Im still getting the daily close price getting printed instead of the hourly
              even updated the input params

              modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
                  datapath = os.path.join(modpath, 'M&M15m-2.csv')
              
                  # Create a Data Feed
                 # Create a Data Feed
                  data = bt.feeds.YahooFinanceCSVData(
                  dataname=datapath,
                  dtformat="%Y-%m-%d %H:%M:%S",
                  timeframe = bt.TimeFrame.Minutes, 
                  compression = 15,
                  reverse=False)
              
                  # Add the Data Feed to Cerebro
              
                  cerebro.resampledata(data, timeframe = bt.TimeFrame.Minutes, compression = 60)
              
              

              Please, help.

              prnvat8197 1 Reply Last reply Reply Quote 0
              • prnvat8197
                prnvat8197 @prnvat8197 last edited by

                @prnvat8197 please use this link https://drive.google.com/file/d/1S49wCI-NdhLnPGrd-zsfpvu50qj7yZVd/view?usp=sharing

                L 1 Reply Last reply Reply Quote 0
                • L
                  lixiaomai001 @prnvat8197 last edited by

                  @prnvat8197
                  try this:

                   dtformat="%Y-%m-%d %H:%M",
                  
                  prnvat8197 1 Reply Last reply Reply Quote 0
                  • prnvat8197
                    prnvat8197 @lixiaomai001 last edited by

                    @lixiaomai001 Nope :(
                    Can you try running on your machine maybe? I'm really stuck with this basic step for a while

                    vladisld 1 Reply Last reply Reply Quote 1
                    • vladisld
                      vladisld @prnvat8197 last edited by

                      @prnvat8197 could it be that there is a new line char separating you date and time columns?

                      prnvat8197 1 Reply Last reply Reply Quote 2
                      • prnvat8197
                        prnvat8197 @vladisld last edited by

                        @vladisld Its just a space. Can you share a sample csv/txt file in the correct format, I can try running my code on it and format my data based on that. Thanks!

                        1 Reply Last reply Reply Quote 1
                        • prnvat8197
                          prnvat8197 @prnvat8197 last edited by

                          @prnvat8197 Aah, finally read the documentation thoroughly and did some tickering around, the Generic csv data is required as input

                          data = btfeeds.GenericCSVData(
                              dataname=datapath,
                              fromdate=datetime.datetime(2021, 7, 1),
                              todate=datetime.datetime(2021, 7, 16),
                              nullvalue=0.0,
                              dtformat=('%Y-%m-%d %H:%M:%S'), 
                              timeframe = bt.TimeFrame.Minutes, 
                              compression = 15,
                              Datetime=0,
                              High=2,
                              Low=3,
                              Open=1,
                              Close=4,
                              Volume=7,
                              Adj_Close=6)
                          
                          

                          Thanks to everyone who replied to this thread! Really appreciate the community :D

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