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/

    Loading one data series and run cerebro.run(runonce=False) Got Error Message

    General Code/Help
    2
    3
    1118
    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.
    • C
      chaT123 last edited by

      The below code has error message below when I load only one series of OHLC data (In Pandas DataFrame format). But it doesnt have problem when I add a second series of data. i.e with two data series it works.

      And when I use "runonce=True" setting, it works also.

      So it ONLY doesn't work when I load only one series of data and use "runonce=False" set up. Does anyone know what is wrong here?

      "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\linebuffer.py", line 163, in getitem
      return self.array[self.idx + ago]
      IndexError: array index out of range

      class TestStrategy(bt.Strategy):
           def __init__(self):
      
              self.closePx = self.datas[0].close
             
              
      
          def next(self):
      
             # The LINE BELOW POP UP ERROR MSG ABOVE
              print(self.stats.broker.value[0])
             
              # ***************************************************************
      
          def stop(self):
              pass
      
      data = bt.feeds.PandasData(dataname=dailyOHLCDF, open='Open', high='High', low='Low', close = 'Close', volume = 'Volume'\
      , openinterest=None)
      
      
      cerebro = bt.Cerebro(stdstats=False)
      
      cerebro.adddata(data)
      cerebro.addobserver(bt.observers.Broker, plot = False)
      cerebro.broker.setcash(500000.0)
      cerebro.addstrategy(TestStrategy)
      thestrats = cerebro.run(runonce=False)
      1 Reply Last reply Reply Quote 0
      • C
        chaT123 last edited by

        I juse tested self.broker.getvalue() is working.

        But self.stats.broker.value[0] not working. So I can just use self.broker.getvalue() right? My goal is to print out broker value every day.

        1 Reply Last reply Reply Quote 0
        • B
          backtrader administrators last edited by

          An Observer operates after things have happened, because it is observing the overall consequences of your actions and the calculations of indicators.

          In iterations next + 1, accessing stats.broker.value[0] will give you the broker value of the previous iteration.

          In next + 0 (the 1st time), there is still no data there if you have no indicators forcing you to have a minimum period. And if you have them, you will get and old value as pointed out above.

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