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/

    bta-lib adx() => Error (??!)

    bta-lib
    3
    8
    809
    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.
    • randietrades
      randietrades last edited by

      Hi there. I'm having trouble getting bta-lib's adx() to work. Would greatly appreciate any help I can get from this group.

      Here's a small piece of code that demonstrates the problem, and below the code is the output I get from running it.

      C O D E:
      https://gist.github.com/randie/5cac0dbf845b8b323a311ed32abea2c7

      import pandas as pd
      import btalib as bta
      
      df = pd.read_csv("data/tickers/AMD.csv", parse_dates=True, index_col='Date')
      
      df['rsi'] = bta.rsi(df).df  # works
      df['adx'] = bta.adx(df).df  # does not work
      
      print(df.tail())
      

      O U T P U T:

      #
      # Output with adx() line commented out => works
      #
      (sandbox) 10:38 • screener • master • d5822cb ✗
      ❯ /opt/anaconda3/envs/sandbox/bin/python /Users/randie/projects/screener/bta-adx.py
                       Open       High        Low      Close  Adj Close    Volume        rsi
      Date                                                                                  
      2021-03-19  78.489998  79.339996  77.589996  79.059998  79.059998  42247100  42.284132
      2021-03-22  79.989998  81.269997  79.220001  80.300003  80.300003  39648600  44.939105
      2021-03-23  80.129997  80.339996  77.949997  78.379997  78.379997  40466600  41.737588
      2021-03-24  77.550003  78.800003  76.400002  76.480003  76.480003  43753600  38.792404
      2021-03-25  75.849998  76.949997  74.959999  76.220001  76.220001  44663000  38.393151
      
      #
      # Output with adx() line back in => does not work!
      #
      (sandbox) 10:38 • screener • master • d5822cb ✗
      ❯ /opt/anaconda3/envs/sandbox/bin/python /Users/randie/projects/screener/bta-adx.py
      Traceback (most recent call last):
        File "/Users/randie/projects/sandbox/python/screener/adx.py", line 8, in <module>
          df['adx'] = bta.adx(df).df  # does not work
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
          b_init(self, *args, **kwargs)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 97, in __init__
          self._pdm = self._smoother(pdm, **smoothargs)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
          b_init(self, *args, **kwargs)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 52, in __init__
          self.o.smacc = _ewm._lfilter(alpha=1.0, beta=(p - 1) / p)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/meta/lines.py", line 343, in _lfilter
          return self._apply(_sp_lfilter)  # trigger __getattr__ for _apply
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/meta/lines.py", line 379, in call_op
          result[self._minidx:] = r = op(*sargs, **kwargs)  # run/store
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 469, in _apply
          return self._apply_blockwise(homogeneous_func, name)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 384, in _apply_blockwise
          return self._apply_series(homogeneous_func, name)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 373, in _apply_series
          result = homogeneous_func(values)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 461, in homogeneous_func
          result = calc(values)
        File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 455, in calc
          return func(x, start, end, min_periods)
      TypeError: _sp_lfilter() takes 1 positional argument but 4 were given
      
      run-out 2 Replies Last reply Reply Quote 1
      • run-out
        run-out @randietrades last edited by

        @randietrades I'm getting the same error.

        RunBacktest.com

        1 Reply Last reply Reply Quote 0
        • E
          elvis last edited by

          I am also getting the same error. Probably the library is wrong or badly made, I deduce that because nobody apparently knows anything about how to solve the error ...

          1 Reply Last reply Reply Quote 0
          • run-out
            run-out @randietrades last edited by

            @randietrades Try running in the terminal or anything outside Jupyter. I suspect it's a notebook thing. Working for me in Pycharm.

            RunBacktest.com

            randietrades 1 Reply Last reply Reply Quote 0
            • randietrades
              randietrades @run-out last edited by

              @run-out Thanks for your response.

              It's not a notebook thing- I am running in the terminal, not a Jupyter notebook.

              A little surprised it works in Pycharm but fails in other environments because the error is a TypeError regarding positional args which makes me think it's either a coding error or some library version incompatibility? ¯\_(ツ)_/¯

              Btw, I'm running python v3.8.8 from the Anaconda distribution. What version of python are you running? Can you post your output, the one that works in Pycharm?

              From my terminal:

              (sandbox) 20:32 • screener • master • d5822cb ✗
              ❯ hardpath $(which python)
              /opt/anaconda3/envs/sandbox/bin/python3.8
              
              (sandbox) 20:32 • screener • master • d5822cb ✗
              ❯ python bta-adx.py
              Traceback (most recent call last):
                File "bta-adx.py", line 7, in <module>
                  df['adx'] = bta.adx(df).df  # does not work
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
                  b_init(self, *args, **kwargs)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 97, in __init__
                  self._pdm = self._smoother(pdm, **smoothargs)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
                  b_init(self, *args, **kwargs)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 52, in __init__
                  self.o.smacc = _ewm._lfilter(alpha=1.0, beta=(p - 1) / p)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/meta/lines.py", line 343, in _lfilter
                  return self._apply(_sp_lfilter)  # trigger __getattr__ for _apply
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/meta/lines.py", line 379, in call_op
                  result[self._minidx:] = r = op(*sargs, **kwargs)  # run/store
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 469, in _apply
                  return self._apply_blockwise(homogeneous_func, name)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 384, in _apply_blockwise
                  return self._apply_series(homogeneous_func, name)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 373, in _apply_series
                  result = homogeneous_func(values)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 461, in homogeneous_func
                  result = calc(values)
                File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 455, in calc
                  return func(x, start, end, min_periods)
              TypeError: _sp_lfilter() takes 1 positional argument but 4 were given
              
              run-out 1 Reply Last reply Reply Quote 0
              • run-out
                run-out @randietrades last edited by

                @randietrades

                I ran this in terminal in a virtual environment. BTALIB is 1.0.0 for all.

                Here is the log:

                (venv) runout@runout-acer:~/projects/scratch$ python3
                Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
                [GCC 9.3.0] on linux
                Type "help", "copyright", "credits" or "license" for more information.
                >>> import btalib
                >>> btalib.__version__
                '1.0.0'
                >>> import pandas as pd
                >>> pd.__version__
                '1.0.4'
                >>> df = pd.read_csv("data/2006-day-001.txt", parse_dates=True, index_col='Date')
                >>> adx = btalib.ADX(df).df
                >>> print(f"ADX: \n{adx.tail()}")
                ADX: 
                                  adx
                Date                 
                2006-12-21  18.249594
                2006-12-22  17.009876
                2006-12-27  16.336722
                2006-12-28  15.879341
                2006-12-29  15.302485
                

                As you can see adx running no problem. But I notice my pandas version is a bit old. So I upgrade:

                ...
                Installing collected packages: pandas
                  Attempting uninstall: pandas
                    Found existing installation: pandas 1.0.4
                    Uninstalling pandas-1.0.4:
                      Successfully uninstalled pandas-1.0.4
                Successfully installed pandas-1.2.3
                
                

                Then I run the same script again as above.

                Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
                [GCC 9.3.0] on linux
                Type "help", "copyright", "credits" or "license" for more information.
                >>> import btalib
                >>> btalib.__version__
                '1.0.0'
                >>> import pandas as pd
                >>> pd.__version__
                '1.2.3'
                >>> df = pd.read_csv("data/2006-day-001.txt", parse_dates=True, index_col='Date')
                >>> adx = btalib.ADX(df).df
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
                    b_init(self, *args, **kwargs)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 97, in __init__
                    self._pdm = self._smoother(pdm, **smoothargs)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
                    b_init(self, *args, **kwargs)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 52, in __init__
                    self.o.smacc = _ewm._lfilter(alpha=1.0, beta=(p - 1) / p)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/meta/lines.py", line 343, in _lfilter
                    return self._apply(_sp_lfilter)  # trigger __getattr__ for _apply
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/btalib/meta/lines.py", line 379, in call_op
                    result[self._minidx:] = r = op(*sargs, **kwargs)  # run/store
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 469, in _apply
                    return self._apply_blockwise(homogeneous_func, name)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 384, in _apply_blockwise
                    return self._apply_series(homogeneous_func, name)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 373, in _apply_series
                    result = homogeneous_func(values)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 461, in homogeneous_func
                    result = calc(values)
                  File "/home/runout/projects/scratch/venv/lib/python3.8/site-packages/pandas/core/window/rolling.py", line 455, in calc
                    return func(x, start, end, min_periods)
                TypeError: _sp_lfilter() takes 1 positional argument but 4 were given
                

                And now I have your error.

                And just to triple check, if I revert pandas to 1.0.4

                >>> import btalib
                >>> btalib.__version__
                '1.0.0'
                >>> import pandas as pd
                >>> pd.__version__
                '1.0.4'
                >>> df = pd.read_csv("data/2006-day-001.txt", parse_dates=True, index_col='Date')
                >>> adx = btalib.ADX(df).df
                >>> print(f"ADX: \n{adx.tail()}")
                ADX: 
                                  adx
                Date                 
                2006-12-21  18.249594
                2006-12-22  17.009876
                2006-12-27  16.336722
                2006-12-28  15.879341
                2006-12-29  15.302485
                >>> 
                

                Try installing an older version of pandas and see if that helps.

                RunBacktest.com

                randietrades 1 Reply Last reply Reply Quote 1
                • randietrades
                  randietrades @run-out last edited by

                  @run-out Thanks again for responding 👍🏼

                  I tried pandas v1.0.4 (the version that worked for you), but some other error came up 🤨 This time the error looks like it's in numpy- what version of numpy is installed in your venv? Mine is v1.20.1.

                  Here's my output:

                  (sandbox) 11:47 • screener • master • d5822cb ✗
                  ❯ python
                  Python 3.8.8 (default, Feb 24 2021, 13:46:16)
                  [Clang 10.0.0 ] :: Anaconda, Inc. on darwin
                  Type "help", "copyright", "credits" or "license" for more information.
                  >>> import btalib
                  >>> btalib.__version__
                  '1.0.0'
                  >>> import pandas as pd
                  >>> pd.__version__
                  '1.0.4'
                  >>> df = pd.read_csv("data/tickers/AMD.csv", parse_dates=True, index_col='Date')
                  >>> adx = btalib.ADX(df).df
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicator.py", line 152, in __call__
                      b_init(self, *args, **kwargs)
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/indicators/directionalmove.py", line 92, in __init__
                      upmove = self.i.high.diff(periods=self.p._period)  # can apply diff
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/btalib/meta/lines.py", line 140, in real_standard_op
                      result = pd.Series(np.nan, index=self._series.index)
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/series.py", line 305, in __init__
                      data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True)
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/construction.py", line 465, in sanitize_array
                      subarr = construct_1d_arraylike_from_scalar(value, len(index), dtype)
                    File "/opt/anaconda3/envs/sandbox/lib/python3.8/site-packages/pandas/core/dtypes/cast.py", line 1452, in construct_1d_arraylike_from_scalar
                      subarr = np.empty(length, dtype=dtype)
                  TypeError: Cannot interpret '<attribute 'dtype' of 'numpy.generic' objects>' as a data type
                  
                  1 Reply Last reply Reply Quote 0
                  • randietrades
                    randietrades last edited by

                    @run-out I found the library version incompatibility! I was running with numpy v1.20.1, the latest version. I installed an older version, the version of numpy available right around when btalib v1.0.0 was released, numpy v1.18.2. It worked 🙌🏼 So that means bta-lib needs to be updated to work with the latest versions of pandas and numpy.

                    Thanks again for taking the time to work this out with me 👍🏼🌷

                    Here's my output now:

                    (sandbox) 13:13 • screener • master • d5822cb ✗
                    ❯ python bta-adx.py
                    numpy v1.18.2
                    pandas v1.0.4
                    btalib v1.0.0
                                     Open       High        Low  ...    Volume        rsi        adx
                    Date                                         ...
                    2021-03-22  79.989998  81.269997  79.220001  ...  39648600  44.939105  26.393616
                    2021-03-23  80.129997  80.339996  77.949997  ...  40466600  41.737588  26.112061
                    2021-03-24  77.550003  78.800003  76.400002  ...  43753600  38.792404  26.252038
                    2021-03-25  75.849998  76.949997  74.959999  ...  44735100  38.393151  26.729348
                    2021-03-26  76.620003  77.500000  75.029999  ...  49109400  41.367524  26.915669
                    
                    [5 rows x 8 columns]
                    
                    1 Reply Last reply Reply Quote 1
                    • 1 / 1
                    • First post
                      Last post
                    Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors