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/

    Intraday time issue/Orders waiting until T+1

    General Code/Help
    2
    5
    1953
    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.
    • I
      iswanb last edited by

      All,

      Have been using Backtrader for about a week and still trying to get the hang of it. I thought I was doing well with some daily stock ideas, but get out over my skis with intraday data.

      I have a year of minute-by-minute EUR/USD data in a OHLC csv. The date is in %Y%M%D and time in %H%M%D. I even added a third column to combine them into a datetime thinking that was the problem.

      20150101,130000,20150101 130000,,1.20965,1.20977,1.20962,1.20962,0
      20150101,130100,20150101 130100,,1.20963,1.20968,1.20962,1.20962,0
      20150101,130200,20150101 130200,,1.20965,1.2097,1.20961,1.20961,0
      

      I add the data to the strategy via the GenericCSVData function

          data = btfeed.GenericCSVData(
              dataname='M:\\EURUSD2.csv',
      
              fromdate=datetime.datetime(2015, 1, 1),
              todate=datetime.datetime(2015, 1, 3),
      
              nullvalue=0.0,
      
              dtformat=('%Y%m%d %H%M%S'),
      
              timefame=bt.TimeFrame.Minutes,
              compression=1,
      
              datetime=2,
              time=-1,
              open=4,
              high=5,
              low=6,
              close=7,
              volume=-1,
              openinterest=-1
          )
      

      When I run a test strategy, all orders wait until the next day to complete. All log data is timestamped "23:59:59.999". From all I can tell, the time isn't being passed to the strategy right. I was under the impression that the strategy is agnostic to tick/minute/hour/etc., it only cares about the next bar.

      I must be missing something simple.

      Thank you.

      B 1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators @iswanb last edited by

        The marking of each bar with the end of day time indicates the data feed is being interpreted as `bt.TimeFrame.Days'. The obvious culprit:

        @iswanb said in Intraday time issue/Orders waiting until T+1:

         timefame=bt.TimeFrame.Minutes,
        
        1 Reply Last reply Reply Quote 0
        • I
          iswanb last edited by

          Thank you. Realize now that calling bt.TimeFrame.minutes was to be done if I wanted to resample or compress the data. However, there was no change when I commented out those lines.

          I even stripped everything down using the simple TestStrategy from the docs/quickstart with no change.

          from __future__ import (absolute_import, division, print_function,
                                  unicode_literals)
          
          import datetime
          import backtrader as bt
          import backtrader.feeds as btfeed
          
          
          class TestStrategy(bt.Strategy):
          
              def log(self, txt, dt=None):
                  ''' Logging function fot this strategy'''
                  #changed to datetime to view time as well
                  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__':
              # Create a cerebro entity
              cerebro = bt.Cerebro()
          
              # Add a strategy
              cerebro.addstrategy(TestStrategy)
          
              # Create a Data Feed
              data = btfeed.GenericCSVData(
                  dataname='M:\\Stock Data\\EURUSD2.csv',
          
                  fromdate=datetime.datetime(2015, 1, 1),
                  todate=datetime.datetime(2015, 1, 3),
          
                  nullvalue=0.0,
          
                  dtformat=('%Y%m%d %H%M%S'),
          
                  datetime=2,
                  time=-1,
                  open=4,
                  high=5,
                  low=6,
                  close=7,
                  volume=-1,
                  openinterest=-1
              )
          
          
              # Add the Data Feed to Cerebro
              cerebro.adddata(data)
          
              # Set our desired cash start
              cerebro.broker.setcash(10000.0)
              cerebro.broker.setcommission(commission=0.001)
          
              # 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())
          

          Results in:

          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          2015-01-02T23:59:59.999989, Close, 1.20
          Final Portfolio Value: 10000.00
          
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            It seems you missed why the specific line of your code was extracted and highlighted:

            • You had written timefame (notice the missing r) as the name of the argument to the data feed.

            • And the name of the argument is timeframe

            The examples in the quickstart docs use a daily timeframe and this is the default. That's why there is no need in that case to specify timeframe=bt.TimeFrame.Minutes

            1 Reply Last reply Reply Quote 0
            • I
              iswanb last edited by

              Got it. Thanks so much!

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