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/

    Missing iterations on next()

    Indicators/Strategies/Analyzers
    3
    3
    54
    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.
    • A
      aero108 last edited by

      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()
      
      hghhgghdf dfdf 1 Reply Last reply Reply Quote 0
      • hghhgghdf dfdf
        hghhgghdf dfdf @aero108 last edited by

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

        Adding kwarg

        timeframe=bt.Timeframe.Ticks
        

        to bt.feeds.GenericCSVData might fix the problem entirely

        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld last edited by

          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.
          1 Reply Last reply Reply Quote 2
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }