What's wrong with this TA-Lib function call?

Question:

I’m trying to call TA-lib’s ADX function, which according to its documentation, has the following parameters:

ADX(high, low, close[, timeperiod=?])

    Average Directional Movement Index (Momentum Indicators)

    Inputs:
        prices: ['high', 'low', 'close']
    Parameters:
        timeperiod: 14
    Outputs:
        real

I am calling it like so:

from talib import abstract


params = {'timeperiod': 14}

indicator_fn = abstract.Function('ADX')

val = indicator_fn(0.5, 0.2, 0.3, **params)

print(val)

But it fails with:

Traceback (most recent call last):
  File "/home/stark/Work/test/test.py", line 11, in <module>
    val = indicator_fn(0.5, 0.2, 0.3, **params)
  File "talib/_abstract.pxi", line 398, in talib._ta_lib.Function.__call__
  File "talib/_abstract.pxi", line 277, in talib._ta_lib.Function.set_function_args
  File "talib/_abstract.pxi", line 462, in talib._ta_lib.Function.__check_opt_input_value
TypeError: Invalid parameter value for timeperiod (expected int, got float)

It doesn’t seem to make sense to me. timeperiod is clearly an int, no?

If I attempt to call it like so:

val = indicator_fn([0.5, 0.2, 0.3], timeperiod=14)

it fails with TypeError: Invalid parameter value for timeperiod (expected int, got list)

If I try

val = indicator_fn(prices=[0.5, 0.2, 0.3], timeperiod=14)

it fails with KeyError: 0.5

If I try:

val = indicator_fn(prices={'high': 0.5, 'low': 0.2, 'close': 0.3}, timeperiod=14)

it fails with TypeError: unhashable type: 'dict'

Any insights here are greatly appreciated!

Asked By: Edy Bourne

||

Answers:

Oh god, the way to call this ended up being:

val = indicator_fn({'high': np.asarray([0.5]), 'low': np.asarray([0.2]), 'close': np.asarray([0.3])}, timeperiod=14)

Its working now. /facepalm

Answered By: Edy Bourne

The 3 first args have to be arrays, e.g:

val = indicator_fn(np.asarray([0.5]), np.asarray([0.2]), np.asarray([0.3]), timeperiod=14)

According to the tests, indicator_fn = abstract.Function('ADX', timeperiod=14) might solve the issue as well.

The error message you’re getting is indeed very misleading, you might want to report that to the dev.

Answered By: Le Minaw

Solved mine as well for LINEARREG_ANGLE with the following snippet:

ANGLE = abstract.Function('LINEARREG_ANGLE')
slope = ANGLE({'close': np.asarray(ema), 'timeperiod': slope_period})
Answered By: deejiw
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.