Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Stuck with Strange Error

Hello all! I am new to the community and developing my first python program to help me analyze stocks.

I am seeking to use TA-Lab and the very first thing I am using I am getting a strange error. Here's some background:

The dataframe (df) has the High, Low, Close, Open price data. I convert each pandas.series to a numpy.ndarray and store the data in the variables h, l, c, o as shown below:

h = df['HIGH'].to_numpy()  
l = df['LOW'].to_numpy()  
c = df['CLOSE'].to_numpy()  
o = df['OPEN'].to_numpy()  

I calculate the RANGE (which works great):

df['RANGE'] = df['HIGH'] - df['LOW']  

And I attempt to use the TA Lib TRANGE function:

from talib.abstract import *  
df['TRUE_RANGE'] = TRANGE(high=h, low=l, close=c)  

The output error says:

---------------------------------------------------------------------------  
TypeError                                 Traceback (most recent call last)  
<ipython-input-8-8cab3f3750b9> in <module>  
--> 126 df['TRUE_RANGE'] = TRANGE(high=h, low=l, close=c)  
talib/_abstract.pxi in talib._ta_lib.Function.__call__()  
talib/_abstract.pxi in talib._ta_lib.Function.__call_function()

TypeError: Argument 'high' has incorrect type (expected numpy.ndarray, got NoneType)  

I do verify that h is a numpy.ndarray:

type(h)  
numpy.ndarray  

So what am I doing wrong? Please help!

1 response

It seems like you aren't working on the Quantopian platform because the following import wouldn't be valid. Also the pandas series to_numpy method is new as of version 0.24. (Quantopian uses version 0.18)

from talib.abstract import *  

However, assuming you are starting out with pricing in a pandas dataframe, you don't need to convert the data to numpy arrays before using them as arguments in the talib functions. The following should work just fine.

from talib import TRANGE 

# Get some pricing data in a pandas dataframe  
prices = get_pricing('AAPL')  
prices.head(5)

# Add a column to our prices df for 'range' and talib 'true range'  
prices['range'] = prices['high'] - prices['low']  
prices['true_range'] = TRANGE(high=prices.high, low=prices.low, close=prices.close_price)  
prices.head(5)  

Check if that works for you. See attached notebook.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.