Backtrader Community

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

    TA-Lib indicator fails with KeyError:

    Indicators/Strategies/Analyzers
    2
    7
    2997
    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.
    • RandyT
      RandyT last edited by

      Was trying to use a TA-Lib indicator and received the following error:

      Using:

      self.xx = bt.talib.LINEARREG_ANGLE(self.data.close, timeperiod=10)
      

      Error:

      KeyError: <module 'backtrader.talib' from '/home/randy/.virtualenvs/backtrader3/lib/python3.6/site-packages/backtrader/talib.py'>
      

      TA-Lib 0.4.10 is installed. I've also tried using period instead of the standard timeperiod for TA-Lib.

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

        period or timeperiod play no role. The error indicates the KeyError to be in the module which should indicate that LINEARREG_ANGLE has not been found.

        Being the question if other things like for example a simple moving average are there or not ...

        The talib integration simply asks ta-lib which functions are available and dynamically creates the needed classes to wrap them in the ecosystem.

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

          @backtrader not sure I understand your response.

          Here is what I see:

          ipdb> print(bt.talib.LINEARREG_SLOPE.__doc__)
          LINEARREG_SLOPE([input_arrays], [timeperiod=14])
          
          Linear Regression Slope (Statistic Functions)
          
          Inputs:
              price: (any ndarray)
          Parameters:
              timeperiod: 14
          Outputs:
              real
          ipdb> self.foo = bt.talib.LINEARREG_SLOPE(self.data.close)
          *** KeyError: <module 'backtrader.talib' from '/home/randy/.virtualenvs/backtrader3/lib/python3.6/site-packages/backtrader/talib.py'>
          ipdb> self.foo = bt.talib.LINEARREG_SLOPE(self.data.close, timeperiod=14)
          *** KeyError: <module 'backtrader.talib' from '/home/randy/.virtualenvs/backtrader3/lib/python3.6/site-packages/backtrader/talib.py'>
          
          1 Reply Last reply Reply Quote 0
          • B
            backtrader administrators last edited by

            The question was if you had tried the same with a simple moving average, talib.SMA.

            Don't know exactly if the messages from ipdb match exactly those from the standard python interpreter, but the interpretation from

            KeyError: <module 'backtrader.talib'
            

            would be that no such thing happens to be in sys.modules, hence the KeyError. Of course that seems to defy the logic of being able to do:

            ipdb> print(bt.talib.LINEARREG_SLOPE.__doc__)
            

            and obtaining a result, which seems to indicate the symbol does actually exist in the module and seems to correspond to the actual talib indicator.

            The only thing that seems to be missing in the aforementioned information is in which context the code is being executed.

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

              Same results with the talib.SMA

              I am running the development branch

              ipdb> self.xx = bt.talib.SMA(self.data.close)
              *** KeyError: <module 'backtrader.talib' from '/home/randy/.virtualenvs/backtrader3/lib/python3.6/site-packages/backtrader/talib.py'>
              ipdb> print(bt.talib.SMA.__doc__)
              SMA([input_arrays], [timeperiod=30])
              
              Simple Moving Average (Overlap Studies)
              
              Inputs:
                  price: (any ndarray)
              Parameters:
                  timeperiod: 30
              Outputs:
                  real
              
              1 Reply Last reply Reply Quote 0
              • RandyT
                RandyT last edited by

                @backtrader
                It was easy enough to test this with master branch and it appears to be working fine there. So seems recent changes might have broken this.

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

                  New changes have only uncovered a typo in the ta-lib integration by which auto-generated wrappers had the module itself in the __module__ attribute instead of the module name. Corrected in the development branch

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