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



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


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


  • administrators

    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.