import yfinance as yf
msft = yf.Ticker('MSFT')
data = msft.history(period='6mo')
import mplfinance as mpf
data['30 Day MA'] = data['Close'].rolling(window=20).mean()
data['30 Day STD'] = data['Close'].rolling(window=20).std()
data['Upper Band'] = data['30 Day MA'] + (data['30 Day STD'] * 2)
data['Lower Band'] = data['30 Day MA'] - (data['30 Day STD'] * 2)
apdict = (
mpf.make_addplot(data['Upper Band'])
, mpf.make_addplot(data['Lower Band'])
)
mpf.plot(data, volume=True, addplot=apdict)
我试过上面的代码。但是我收到以下错误,我不明白出了什么问题。有人可以告诉我如何修复这个错误吗?谢谢。
>>> mpf.plot(data, volume=True, addplot=apdict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mplfinance/plotting.py", line 293, in plot
config = _process_kwargs(kwargs, _valid_plot_kwargs())
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mplfinance/_arg_validators.py", line 292, in _process_kwargs
raise TypeError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
TypeError: kwarg "addplot" validator returned False for value: "({'data': Date
2020-12-15 NaN
2020-12-16 NaN
2020-12-17 NaN
2020-12-18 NaN
2020-12-21 NaN
...
2021-06-08 255.290610
2021-06-09 256.115131
2021-06-10 256.886620
2021-06-11 258.091432
2021-06-14 259.828890
Name: Upper Band, Length: 124, dtype: float64, 'scatter': False, 'type': 'line', 'mav': None, 'panel': 0, 'marker': 'o', 'markersize': 18, 'color': None, 'linestyle': None, 'width': None, 'bottom': 0, 'alpha': 1, 'secondary_y': 'auto', 'y_on_right': None, 'ylabel': None, 'ylim': None, 'title': None, 'ax': None, 'yscale': None, 'stepwhere': 'pre'}, {'data': Date
2020-12-15 NaN
2020-12-16 NaN
2020-12-17 NaN
2020-12-18 NaN
2020-12-21 NaN
...
2021-06-08 239.371960
2021-06-09 239.340165
2021-06-10 240.447735
2021-06-11 240.784914
2021-06-14 240.278626
Name: Lower Band, Length: 124, dtype: float64, 'scatter': False, 'type': 'line', 'mav': None, 'panel': 0, 'marker': 'o', 'markersize': 18, 'color': None, 'linestyle': None, 'width': None, 'bottom': 0, 'alpha': 1, 'secondary_y': 'auto', 'y_on_right': None, 'ylabel': None, 'ylim': None, 'title': None, 'ax': None, 'yscale': None, 'stepwhere': 'pre'})"
'Validator' : lambda value: isinstance(value,dict) or (isinstance(value,list) and all([isinstance(d,dict) for d in value])) },