【问题标题】:How to use technical indicators of TA-Lib with pandas in pythonpython中如何将TA-Lib的技术指标与pandas一起使用
【发布时间】:2016-06-29 10:13:51
【问题描述】:

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

上面的代码在print f.Close 之前一直有效,但随后显示此错误

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

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

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

有没有可用于 Python 的类似库?

【问题讨论】:

  • 据我所知,ta.xxx 的输入应该是 numpy 数组,而不是 pandas 系列。试试ta.RSI(f.Close.values,2)。同样,.values 属性适用于其他 talib 指标
  • 谢谢谢尔盖。它工作得很好

标签: python pandas ta-lib technical-indicator


【解决方案1】:

试试看;

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

【讨论】:

    【解决方案2】:

    试试

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

    【讨论】:

    【解决方案3】:

    问题是您尝试使用 pandas 系列调用 SMA / RSI 等函数,但如果您浏览 TALIB 文档,它表明它们需要一个 numpy 数组作为参数。

    所以你可以使用这个:

    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)
    

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

    希望这会有所帮助。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-29
        • 2018-02-06
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-05-14
        • 2023-02-19
        相关资源
        最近更新 更多