Backtrader Community

    • 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/

    Get time in Indicator

    General Code/Help
    1
    1
    23
    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.
    • Yi-Ming Lai
      Yi-Ming Lai last edited by

      I want to calculate accumulate sum of trading volume at 9:30 AM each day, and do it in next
      method of Indicator class, like this:

      class MyLine(bt.Indicator):
          lines = ('AccVol',)
          params = (('bar_count', 30),)
          
          def __init__(self):
              self.time=self.data0.datetime.time()
      
          def next(self):
              print(self.time)
              # calculate accumulate volume at specific time of each day
      
      
      class MyStrategy(bt.Strategy):
          params = dict(
              bar_count=30,
          )
      
          def __init__(self):
              self.MyLine = MyLine(self.data, bar_count=self.p.bar_count)
          def next(self):
              print(self.data.datetime.time())
      
      
      cerebro = bt.Cerebro(runonce=False)
      all_stock = np.load('stockdata.npy', allow_pickle='TRUE').item()
      dataframe = all_stock['1101']
      data = bt.feeds.PandasData(dataname=dataframe, fromdate=datetime.datetime(2022, 11, 1), todate=datetime.datetime(2022, 11, 30), timeframe=bt.TimeFrame.Minutes)
      cerebro.adddata(data)
      cerebro.addstrategy(MyStrategy)
      cerebro.run(runonce=False)
      

      However, the print(self.time) in next method of MyLine class always print 13:30:00 (which is market closing time of each day), desipte the print(self.data.datetime.time()) in next method of MyStrategy could correctly print every minute.

      What's wrong with my code, or is there better suggestion?

      Thank you for any help you can provide.

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