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

Missing iterations on next()

  • Hi,
    I am facing problem when not all candles iterate using next().
    Imported OHLCV data has 30 ticks, but once I call EMA in init(),
    next() called only 1 time.
    Which is correct for EMA with default settings, but I need next() called 30 times (per ticks quantity).
    I need it to create custom indicator however, so for missing EMA results I can get something like 'NaN'.

    import backtrader as bt
    class Test(bt.Indicator):
        lines = ('dummy',)
        def __init__(self):
            self.ema = bt.indicators.EMA()
        def next(self):
    ohlcv_data = bt.feeds.GenericCSVData(dataname='test.csv', headers=False, volume=-1, openinterest=-1)
    cerebro = bt.Cerebro()


    Adding kwarg


    to bt.feeds.GenericCSVData might fix the problem entirely

  • Please take a look at the following docs:


    • backtrader calls next when all buffers (indicators, data feeds) can deliver at least data point. A 100-bar moving average will obviously only deliver when it has 100 data points from the data feed.

    This means that when entering next, the data feed will have 100 data points to be examined and the moving average just 1 data point

    • backtrader offers prenext as hook to let the developer access things before the aforementioned guarantee can be met. This is useful for example when several data feeds are in play and they start date is different. The developer may want some examination or action be taken, before all guarantees for all data feeds (and associated indicators) are met and next is called for the first time.

Log in to reply