pandas python中如何在pandas中使用TA-Lib的技术指标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35986436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:52:04  来源:igfitidea点击:

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

pythonpandasta-libtechnical-indicator

提问by Eka

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

我是 python 和 pandas 的新手,主要是学习它来丰富我的编程技能以及 python 作为通用程序语言的优势。在这个程序中,我使用它从雅虎获取历史数据并使用 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.Closebut then it shows this error

上面的代码工作到print f.Close但后来它显示了这个错误

 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 Quantmodwhich makes technical analysis easier and with fewer codes.

我已经使用 R 及其库对股票进行技术分析,它有一个内置的库,叫做Quantmod它使技术分析更容易,代码更少。

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

is there any similar libraries available for Python?.

是否有任何类似的可用于 Python 的库?

回答by Tim

Try with;

尝试;

print ta.RSI(np.array(f.Close))

回答by ywllyht

Try with

试试

ta.RSI(f["Close"].values)

回答by sayantan ghosh

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.

问题是您正在尝试使用 Pandas 系列调用 SMA/RSI 等函数,但是如果您查看 TALIB 文档,它会显示它们需要一个 numpy 数组作为参数。

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.

SMA 中的第二个参数是可选的。

Hope this helps.

希望这可以帮助。

回答by Hamza Sabir

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

ta.RSI 期望处理值数组,但它获取数据帧。因此,您必须将数据帧转换为数组。尝试这个:

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

打印 ta.RSI(f.Close.values, 2)