Skip to main content
 首页 » 编程设计

python之TA-Lib Python Wrapper only length-1 数组错误

2025年05月04日38hnrainll

代码:

   def macd(prices): 
        print "Running MACD" 
        prices = np.asarray(prices) 
        print prices 
        macd, macdsignal, macdhist = MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9) 
        print "MACD "+macd 

解释:

我正在尝试对包含收盘价的 Python 列表进行一些分析。

我知道我必须在将列表交给 TA-Lib 之前转换列表,因为我已经看到所有这样做的示例。

然而,只能将长度为 1 的数组转换为 Python 标量

请您参考如下方法:

我像这样导入 talib 模块,就像在 TA-Libs website 中一样:

from talib.abstract import MACD 

然而,这在社区中是不受欢迎的,今天我找到了原因。一个模块命名空间破坏了其他模块命名空间,从而导致错误。这个放得好here .

所以我刚刚干净地导入了 talib:

import talib 

最终的代码是:

def macd(prices): 
        print "Running MACD" 
        prices = np.array(prices, dtype=float) 
        print prices 
        macd, macdsignal, macdhist = talib.MACD(prices, fastperiod=12, slowperiod=26, signalperiod=9) 
        print "MACD "+macd