0

我刚刚了解了使用 yfinance 导入市场数据进行技术分析。yfinance 和 ta-lib 都已正确安装。我使用了 yf.download 方法并检查了数据是否为数据帧格式。这是在 Jupyter 笔记本上运行的。

import talib
import yfinance as yf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

data = yf.download("MSFT","2015-01-01")
type(data)
data.head()

# Everything up to this point seems fine. I then tried to use the data as the input parameters for ta-lib (ADX as one example).

AvgDaily = talib.ADX('High','Low','Close', timeperiod = 10)

错误是:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-886a1aac12f8> in <module>
----> 1 real = talib.ADX('High','Low','Close', timeperiod = 10)

~\anaconda3\lib\site-packages\talib\__init__.py in wrapper(*args, **kwargs)
     25 
     26             if index is None:
---> 27                 return func(*args, **kwargs)
     28 
     29             # Use Series' float64 values if pandas, else use values as passed

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

如果我只是尝试调用数据框中的值,它似乎很好:

data.High

4

1 回答 1

0

There's a new library that was built by Daniel Rodriguez, same fellow who built backtrader, that makes working with ta-lib a lot easier.

The library is called btalib.

Once installed you simply give it the full OHLCV dataframe, it knows which columns to use, and it will spitout the column you are looking for.

import yfinance as yf
import btalib

btalib.ADX(yf.download("msft", start="2019-01-01")).df

The return item is an object. Using '.df' gives you the output attributes signal which is a dataframe.

The docstrings are detailed. If there are attributes they can just be added in.

print(btalib.ADX(yf.download("msft", start="2019-01-01"), period=21).df[50:])

Date             
2019-03-15 20.281
2019-03-18 21.011
2019-03-19 21.832
2019-03-20 22.661
2019-03-21 23.739
2019-03-22 24.749
2019-03-25 25.457
2019-03-26 26.234
2019-03-27 26.520
2019-03-28 26.792
2019-03-29 27.179
2019-04-01 27.680
2019-04-02 28.218
2019-04-03 28.882
2019-04-04 29.226
2019-04-05 29.553
2019-04-08 29.588
2019-04-09 29.598
2019-04-10 29.773
2019-04-11 30.039
2019-04-12 30.318
2019-04-15 30.704
2019-04-16 30.865
2019-04-17 31.061
2019-04-18 31.582
2019-04-22 32.165
2019-04-23 32.983
于 2020-06-13T10:41:59.967 回答