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

Date Changing But Time not Changing



  • I think I've been following examples with very simple code, whatever I do I can't get the times to chage along with the dates while iterating, it keeps the time at 23:59:59. I can't figure out what I'm doing wrong. The code is as follows:

    class MyStrategy(bt.Strategy):
            def __init__(self):
                self.dataclose = self.datas[0].close
        
            def next(self):
                print(self.datas[0].datetime.date(0), self.datas[0].datetime.time(0))
    
    cerebro = bt.Cerebro()
    
    
    data = btfeeds.GenericCSVData(
        dataname='data.csv',
    
        dtformat=('%Y-%m-%d'),
        tmformat=('%H.%M.%S'),
        
        datetime=0,
        time=1,
        open=2,
        high=3,
        low=4,
        close=5,
        volume=6,
        openinterest=-1
    )
    
    cerebro.adddata(data)
    
    cerebro.addstrategy(MyStrategy)
    
    cerebro.run()
    

    The output from printing the date and time looks as follows:

    2010-03-22 23:59:59.999989
    2010-03-22 23:59:59.999989
    2010-03-22 23:59:59.999989
    2010-03-22 23:59:59.999989
    2010-03-22 23:59:59.999989
    2010-03-22 23:59:59.999989
    

    continuing all the way throughout. The csv file looks like this:

    ```
              date 	           time 	    open 	    high 	     low 	   close   ticks
    

    0 2010-03-01 00.24.00 0.89887 0.89887 0.89886 0.89886 2
    1 2010-03-01 00.25.00 0.89884 0.89888 0.89870 0.89881 20
    2 2010-03-01 00.26.00 0.89878 0.89883 0.89878 0.89883 7
    3 2010-03-01 00.27.00 0.89883 0.89883 0.89883 0.89883 1



  • For minute data tell cerebro that you are using minute data (timeframe) and how many minutes per bar (compression). Also your date data appears to be one value. Combine the formats.

    data = bt.feeds.GenericCSVData(
        dataname='data.csv',
        dtformat=("%Y-%m-%d %H.%M.%S"),
        timeframe=bt.TimeFrame.Minutes,
        compression=1,
        
        datetime=0,
        time=1,
        open=2,
        high=3,
        low=4,
        close=5,
        volume=6,
        openinterest=-1
    )
    


  • Ah, thanks so much for that. Works perfectly


Log in to reply
 

});