1
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])) },
4

1 回答 1

1
  • 根据向基本 mplfinance plot() 添加图绘制多个附加数据集部分
    • 除了下面的示例,对于数据框中的两列,文档显示了许多为参数配置适当对象的方法addplot
    • apdict = [mpf.make_addplot(data['Upper Band']), mpf.make_addplot(data['Lower Band'])]也可以。请注意,它是 a list,而不是 a tuple
  • mplfinance/例子
# add multiple additional data sets
apdict = mpf.make_addplot(data[['Upper Band', 'Lower Band']])

# plot
mpf.plot(data, volume=True, addplot=apdict)

在此处输入图像描述

于 2021-06-15T01:36:04.367 回答