Backtrader Community

    • 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/

    GenericCSVData does not take time, only takes date

    General Code/Help
    5
    8
    1054
    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.
    • B
      bng last edited by

      import backtrader as bt

      class Database:

      def __init__(self, timeStamp):
          self.timeStamp= timeStamp.upper()
          
      def getBtCSVdata(self):
          '''
          This method will return backtrader CSV data to feed directly
          '''
          fn = 'D://Data//your_csv_file.csv'
          if self.timeStamp == 'DAY':
              # For daily data
              dateformat = '%Y-%m-%d'
              timeformat = None
          else:
              # For 1min or 5min or 15min or 30min data
              dateformat = '%Y-%m-%d %H:%M:%S'
              timeformat = '%H:%M:%S'
          
          btCSVData = bt.feeds.GenericCSVData(dataname=fn,
                                         header = False,
                                         dtformat = dateformat,
                                         tmformat = timeformat,
                                         nullvalue = 0.0,
                                         datetime = 0,
                                         open = 1,
                                         high = 2,
                                         low = 3,
                                         close = 4,
                                         volume = 5,
                                         openinterest = -1)
          
          return btCSVData
      

      #==========================================================

      class TestStrategy(bt.Strategy):

      def log(self, txt, dt=None):
          ''' Logging function for this strategy'''
          dt = dt or self.datas[0].datetime.datetime(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':

      cerebro = bt.Cerebro()
      
      # Add a strategy
      cerebro.addstrategy(TestStrategy)
      
      # Create a Data Feed
      ds = Database('1min')
      #ds.updateFromAlphavantage()
      
      data1 = ds.getBtCSVdata()
      
      # Add the Data Feed to Cerebro
      cerebro.adddata(data1)
      
      # 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())
      

      #=================================
      My CSV data are as follows:

      2019-03-13 15:25:00,148.70,148.80,148.70,148.70,92180,0.00
      2019-03-13 15:26:00,148.55,148.65,148.55,148.60,102998,0.00
      2019-03-13 15:27:00,148.55,148.60,148.45,148.45,98636,0.00
      2019-03-13 15:28:00,148.50,148.60,148.50,148.55,73246,0.00
      2019-03-13 15:29:00,148.55,148.75,148.45,148.75,75646,0.00
      #=======================================
      The output of backtrader is follows:

      Starting Portfolio Value: 100000.00
      2019-03-13T23:59:59.999989, Close, 148.60
      2019-03-13T23:59:59.999989, Close, 148.45
      2019-03-13T23:59:59.999989, Close, 148.55
      2019-03-13T23:59:59.999989, Close, 148.75
      Final Portfolio Value: 100000.00
      #====================
      How do I import the CORRECT time data in my programme?

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by ab_trader

        You need to specify timeframe and compression for data feed added.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        B B 2 Replies Last reply Reply Quote 3
        • B
          bng @ab_trader last edited by

          Thank you @ab_trader very much to help me.

          1 Reply Last reply Reply Quote 0
          • B
            benmercerdev @ab_trader last edited by

            @ab_trader where do you add these parameters?

            P Jr G 1 Reply Last reply Reply Quote 0
            • run-out
              run-out last edited by

              Have a look here.

              RunBacktest.com

              B 1 Reply Last reply Reply Quote 0
              • B
                benmercerdev @run-out last edited by

                @run-out great, thank you.

                1 Reply Last reply Reply Quote 1
                • P Jr G
                  P Jr G @benmercerdev last edited by

                  @benmercerdev I had same problem. I solved it by following the suggestion of ab_trader. Now my custom csv method looks like:

                  class my_custom_csv(bt.feeds.GenericCSVData):
                      params = (
                          ('dtformat', '%Y-%m-%d %H:%M:%S'),
                          ...
                          ('timeframe', bt.TimeFrame.Minutes),
                          ('compression', 1),
                      )
                  

                  Adding timeframe and compression solved it for me.

                  A 1 Reply Last reply Reply Quote 0
                  • A
                    ab_trader @P Jr G last edited by

                    @P-Jr-G these parameters are already in the data feed class, no need to add them one more time when you define your own data feed class. You just need to call your data feed as follows:

                    data = my_custom_csv(path_to_data_file, timeframe=bt.TimeFrame.YourTimeFrame_here)
                    
                    • If my answer helped, hit reputation up arrow at lower right corner of the post.
                    • Python Debugging With Pdb
                    • New to python and bt - check this out
                    1 Reply Last reply Reply Quote 0
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors