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/

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

    General Code/Help
    2
    3
    63
    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.
    • Quanliang Xie
      Quanliang Xie last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

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

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        1 Reply Last reply Reply Quote 2
        • Quanliang Xie
          Quanliang Xie last edited by

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

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