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

Datetime.time won't give me the correct time.



  • Hi, I am new to the backtrader comminity. I am currently having trouble getting the time using datetime.time or datetime.datetime. Below is my code and result.

    class TestStrategy(bt.Strategy):
    	 params = (('myparam', 27), 
    			('exitbars', 5), 
    			('period',24), 
    			('pop_change_1h', 50),
    			('pop_change_4h', 50),
    			('pop_change_24h', 50),
    			('pop_lim_1h', 50),
    			('pop_lim_4h', 50),
    			('pop_lim_24h', 50),
    			)
    
    	 def log(self, txt, dt=None):
    		dt = dt or self.datas[0].datetime.datetime(0)
    		print('%s, %s' %(dt.isoformat(), txt))
    
    	 def __init__(self,Name):
       	# Keep a reference to the popularity line in the data[0] dataseries
              self.popularity = self.datas[0].close
              self.name = Name
              self.s = yprice(self.name,'')
    
    	 def next(self):
    	 	self.log(self.datas[0].datetime.datetime(0))
           
    if __name__ == '__main__':	
    	# Create a cerebro entity
        cerebro = bt.Cerebro()
    
    
        path = "/mnt/c/users/liang/desktop/backtesting/small_data/*.csv"
        for datapath in glob.glob(path):
    	    data = bt.feeds.GenericCSVData(
    	    	dataname= datapath,
    	    	dtformat= ('%Y-%m-%d %H:%M:%S UTC'),
    	    	nullvalue=0.0,
    		    datetime=0,
    		    time=-1,
    		    high=-1,
    		    low=-1,
    		    open=-1,
    		    close=1, #this is actully popularity
    		    volume=-1,
    		    openinterest=-1)
    		
    	    #print(datapath[50:-15])
    	    # Add the Data Feed to Cerebro
    	    cerebro.adddata(data)
    	    cerebro.addstrategy(TestStrategy, Name=(datapath[50:-15]))
    	    print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
    	    cerebro.run()
    
    	    print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    Result:
    85aad52c-9ba3-40a7-b546-8092cf0f5ac8-image.png

    My csv format is 436b22eb-9677-46b5-aa7d-1a8525a9a14e-image.png
    I always get 23:59:59 as my time. Can someone show me how to get the correct time that I can use in my strategy? I am wondering if I need to use a custom Data feed Class. Please correct me if I am worng. Thanks!



  • You need to specify timeframe for the data feed. bt treats all datafeeds as daily by default.



  • Thank you so much! That solved my problem. To thoes who has the same problem, remember also to improt timeframe from backtrader!


Log in to reply
 

});