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/

    Empty datas feeds when resempling

    General Code/Help
    2
    5
    46
    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.
    • tessanix
      tessanix last edited by

      Hi!

      I would like to test my strategy with datas on multiple timeframes (hourly and daily) but it seems that it does not work.
      Furthermore, when I launch my strategy data0 and data1 seems to be empty :

      The code :
      Capture d’écran du 2021-01-04 21-57-54.png

      My strategy :
      Capture d’écran du 2021-01-04 21-58-17.png
      The ouput :
      Capture d’écran du 2021-01-04 21-58-52.png

      Some advice?

      1 Reply Last reply Reply Quote 0
      • R
        rajanprabu last edited by

        General note:
        Please use ``` ticks to post the code rather than images.. Its hard to decode with images..

        after a quick glance, you are setting open, high, low and close to be absent by setting them -1. Im not sure if you need a pandas class at all as BT has generic pandas support. For example

            data_file = 'contracts_1_min.csv'
            # Create a Data Feed and add to cerebro
            df = pd.read_csv(data_file, header=0,  index_col=0, parse_dates=True)
            data0 = bt.feeds.PandasData(dataname=df, 
                timeframe=bt.TimeFrame.Minutes, 
                compression=1, 
                sessionstart=datetime.time(9, 15), 
                sessionend=datetime.time(15, 30), 
                openinterest=-1, 
                fromdate=datetime.datetime(2011,1, 3),
                todate=datetime.datetime(2011, 11, 4)
                ) 
        
        1 Reply Last reply Reply Quote 2
        • tessanix
          tessanix last edited by

          Thank you, it's working. Here is the code :
          (no images this time )

          import backtrader as bt
          from trending_detect import  TestStrategy2
          from getting_data import getStockFromCSV
          
          # Create a cerebro entity
          cerebro = bt.Cerebro()
          
          # Add a strategy
          cerebro.addstrategy(TestStrategy2)
          
          data = getStockFromCSV(stock_index='AAPL', period='H1', add=[])[-3000:]
          df = bt.feeds.PandasData(dataname=data, 
                          timeframe=bt.TimeFrame.Minutes, 
                          compression=60, 
                          openinterest=-1) 
          
          
          #df = PandasData(dataname=data)
          cerebro.adddata(df)
          
          cerebro.resampledata(df, timeframe=bt.TimeFrame.Days, compression=1)
          
          # Set our desired cash start
          cerebro.broker.setcash(1000000.0)
          
          # Print out the starting conditions
          print('Starting Portfolio Value: %.5f' % cerebro.broker.getvalue())
          
          # Run over everything
          
          cerebro.run()
          
          # Print out the final result
          print('Final Portfolio Value: %.5f' % cerebro.broker.getvalue())
          
            
          class TestStrategy2(bt.Strategy):
              
              def __init__(self):
                  # Keep a reference to the "close" line in the data[0] dataseries
                  self.dataclose0 = self.data0.close
                  self.dataclose1 = self.data1.close
          
                  #print(len(self.datas))
                  #print(len(self.data0.close))
                  #print(len(self.data1.close))
                  
              def log(self, dt=None):
                  txt0 = 'closing: %.5f' % self.dataclose0[0] 
                  txt1 = 'closing: %.5f' % self.dataclose1[0] 
          
                  date0 = dt or self.datas[0].datetime.date(0)
                  time0 = self.datas[0].datetime.time(0)
                  
                  date1 = dt or self.datas[1].datetime.date(0)
                  time1 = self.datas[1].datetime.time(0)
                  print('%s,%s, %s' % (date0.isoformat(),time0.isoformat(), txt0))
                  print('%s,%s, %s' % (date1.isoformat(),time1.isoformat(), txt1))
          
          
              def next(self):
                  #print('datas', len(self.datas))
                  print('close0', self.dataclose0[0])
                  print('close1', self.dataclose1[0])
                  self.log()
          

          But the output is a litlle weird...

          close0 132.91
          close1 133.72
          2020-12-31,21:00:00, closing: 132.91000
          2020-12-30,23:59:59.999989, closing: 133.72000
          close0 132.03
          close1 132.91
          2021-01-04,16:00:00, closing: 132.03000
          2020-12-31,23:59:59.999989, closing: 132.91000
          close0 131.02
          close1 132.91
          2021-01-04,17:00:00, closing: 131.02000
          2020-12-31,23:59:59.999989, closing: 132.91000
          close0 131.02
          close1 131.02
          2021-01-04,17:00:00, closing: 131.02000
          2021-01-04,23:59:59.999989, closing: 131.02000
          Final Portfolio Value: 1000000.00000
          

          why do I have a few milliseconds difference between the two dates?

          1 Reply Last reply Reply Quote 0
          • R
            rajanprabu last edited by

            1. Backtrader can read csv directly. Have a look at the Feed Docs

            2. try giving sessionstart and sessionend timings when you feed the data. I have given this in my example above.

            1 Reply Last reply Reply Quote 0
            • tessanix
              tessanix last edited by

              Yes thanks!
              I had completely forgotten the market hours.
              It working with the correct start/end session.

              df = bt.feeds.PandasData(dataname=data, 
                              timeframe=bt.TimeFrame.Minutes, 
                              sessionstart=datetime.time(16,0), 
                              sessionend=datetime.time(22,0), 
                              compression=60, 
                              openinterest=-1)
              
              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(); }); }