How to use technical indicators of TA-Lib with pandas in python
Asked Answered
G

4

15

I am new to python and pandas and mainly learning it to diversify my programming skills as well as of the advantage of python as a general programme language. In this programme I am using it to fetch historical data's from yahoo and do some technical analysis using functions in talib

import pandas_datareader.data as web
import datetime
import talib as ta

start = datetime.datetime.strptime('12/1/2015', '%m/%d/%Y')
end = datetime.datetime.strptime('2/20/2016', '%m/%d/%Y')
f = web.DataReader('GOOG', 'yahoo', start, end)
print 'Closing Prices'
print f['Close'].describe()
print f.Close
print ta.RSI(f.Close,2)
print ta.SMA(f.Close,2)
print ta.SMA(f.Volume,4)
print ta.ATR
print ta.ATR(f.High,f.Low,f.Close,3)

the above code works till print f.Close but then it shows this error

 print ta.RSI(f.Close,2)
TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Series)

I have used R and its libraries for technical analysis of stocks and It have an inbuilt library called Quantmod which makes technical analysis easier and with fewer codes.

library(quantmod)
symbol=getSymbols(AAPL)
SMA=SMA(Cl(Symbol),2)

is there any similar libraries available for Python?.

Gluttonize answered 14/3, 2016 at 11:34 Comment(2)
As far as I remember input to ta.xxx should be numpy array, not pandas Series. Try ta.RSI(f.Close.values,2). The same, .values property, applies to the other talib indicatorsLeanto
Thanks Sergey. Its working pretty goodMeninges
W
7

Try with;

print ta.RSI(np.array(f.Close))
Winther answered 22/7, 2017 at 15:25 Comment(0)
S
3

Try with

ta.RSI(f["Close"].values)
Sturm answered 29/4, 2018 at 13:14 Comment(1)
Welcome to Stack Overflow, please read the following: stackoverflow.com/help/how-to-answerBenitabenites
M
3

Problem is you are trying to call SMA / RSI etc functions with pandas series but if you go through the TALIB documentation it shows that they require a numpy array as parameter.

So you can use this :

Close=np.array(f['close'][1:])
Modclose=np.zeroes(len(Close))
For i in range(len(Close)):
       Modclose[i]=float(Close[i])

ta.SMA(Modclose,timestamp)

The second parameter in SMA is optional though.

Hope this helps.

Marks answered 30/4, 2018 at 13:14 Comment(0)
C
0

ta.RSI expects array of values to process but it gets dataframe. So, you have to convert dataframe to array. Try this:

print ta.RSI(f.Close.values, 2)

Colner answered 10/8, 2018 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.