For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

4 hour time intervals



  • Hi,

    I'm just getting used to Backtrader and I'm trying to use the Quick Start guide just slightly tweaked. Basically, my data is in 4 hour intervals, but I can't quite see how to get Backtrader to keep things at the 4 hour level. In particular, I'm using:

    data = btfeeds.GenericCSVData(
    dataname='my.csv',
    fromdate=datetime.datetime(2000, 1, 1),
    todate=datetime.datetime(2018, 12, 31),
    nullvalue=0.0,
    dtformat=('%Y-%m-%d %H:%M:%S'),
    datetime=0,
    high=1,
    low=2,
    open=3,
    close=4,
    volume=-1,
    openinterest=-1
    )

    Then, I'm using:

    def log(self, txt, dt=None):
    ''' Logging function for this strategy'''
    dt = dt or self.datas[0].datetime.date(0)
    print('%s, %s' % (dt.strftime('%Y-%m-%d %H:%M:%S'), txt))

    However, my print out is looking like:

    2017-12-06 00:00:00, Close, 0.00000994
    2017-12-06 00:00:00, Close, 0.00001009
    2017-12-06 00:00:00, Close, 0.00000940

    i.e. the hours are all 00:00:00. Any help would be appreciated.



  • FAQ - Session Start/End Times and TimeFrame/Compression

    You need to set time frame and compression.



  • @ab_trader Definitely a step in the right direction! I've updated my code:

    data = btfeeds.GenericCSVData(
        dataname='ada.csv',
        fromdate=datetime.datetime(2000, 1, 1),
        todate=datetime.datetime(2018, 12, 31),
        timeframe=bt.TimeFrame.Minutes, compression=240,
        nullvalue=0.0,
        dtformat=('%Y-%m-%d %H:%M:%S'),
        datetime=0,
        high=1,
        low=2,
        open=3,
        close=4,
        volume=-1,
        openinterest=-1
    )
    

    and now, my buy creates/buy executes are acting as expected (the timing seemed to be "off" before). However, the printout remains unchanged; backtrader still seems to ignore my hours at least in the cosmetic sense. Is there another setting that I need to add to make sure the time component gets printed out?

    Thanks!



  • Can you show full script and data sample?



  • @kreut said in 4 hour time intervals:

    You have to call self.datas[0].datetime.datetime(0) in your logging method:

    def log(self, txt, dt=None):
        ''' Logging function for this strategy'''
        dt = dt or self.datas[0].datetime.datetime(0)
        print('%s, %s' % (dt.strftime('%Y-%m-%d %H:%M:%S'), txt))


  • @fintrading Got it! Thanks...


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.