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

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):
            print(bt.num2date(self.data.datetime.dt()))
    
    
    ohlcv_data = bt.feeds.GenericCSVData(dataname='test.csv', headers=False, volume=-1, openinterest=-1)
    cerebro = bt.Cerebro()
    cerebro.adddata(ohlcv_data)
    cerebro.addindicator(Test)
    cerebro.run()
    


  • https://backtrader.com/docu/datafeed/

    Adding kwarg

    timeframe=bt.Timeframe.Ticks
    

    to bt.feeds.GenericCSVData might fix the problem entirely



  • Please take a look at the following docs:

    https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/#next-and-prenext

    Quoting:

    • 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
 

});