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/

    Cannot access the resampled data in self defined indicators

    General Code/Help
    2
    6
    312
    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.
    • Y
      yoghurtshawn last edited by

      I had a data source in Minutes time frame, and I resampled it into 15 minutes data. However, I cannot get the resample data value in my custom Indicators' __init__ function (it calls some talib functions). It says "Exception: inputs are all NaN". Is there anything wrong?

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

        @yoghurtshawn said in Cannot access the resampled data in self defined indicators:

        Is there anything wrong?

        Knowledge osmosis is not yet possible. No data, no code, no nothing ... no answer

        1 Reply Last reply Reply Quote 0
        • Y
          yoghurtshawn last edited by

          Sorry.

          b75ed2cb-9143-46af-9683-c6d07c823d64-image.png
          The data look like this, being 1MIN kline and have been added.

          Then, I resample it with cerebro.resample(data, timeframe=bt.TimeFrame.Minutes, compression=15), hoping to get klines of 15MIN.

          It can be accessed in bt.indicators.SimpleMovingAverage(self.datas[1].close, period=30) and it works fine.

          However, when the indicator is defined by myself, for example,

          class SMACustIndicator(Indicator):
              lines = ('sma_cust',)
              params = (
                  ('period', 30),
              )
              plotinfo = dict(subplot=False)
          
              def __init__(self):
                  self.sma_cust = talib.SMA(
                      pd.Series(self.data.close.array),
                      timeperiod=self.params.period
                  )
          
              def next(self):
                  cur = len(self)
                  self.lines.sma_cust[0] = self.sma_cust.iloc[cur-1, ]
          

          It raises "Exception: inputs are all NaN".

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

            @yoghurtshawn said in Cannot access the resampled data in self defined indicators:

                def __init__(self):
                    self.sma_cust = talib.SMA(
                        pd.Series(self.data.close.array),
                        timeperiod=self.params.period
                    )
            

            At that stage there is no data available, which means you are creating a pd.Series full of NaN, hence the result.

            ta-lib is integrated with backtrader, use the integration directly. Doc - Indicators - ta-lib

            1 Reply Last reply Reply Quote 0
            • Y
              yoghurtshawn last edited by yoghurtshawn

              Thanks! @backtrader

              One more question, do you mean, the data will only be filled at next() calls?

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

                No, I mean that you are initializing a pd.Series with an array which is full of NaN values.

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