`ValueError: x and y must be the same size` when calling mplfinance

`ValueError: x and y must be the same size` when calling mplfinance

import mplfinance as mpf
import talib as ta
import matplotlib.pyplot as plt
import numpy as np
%matplotlib notebook
test=df
WMA20 = ta.WMA(test['close'], timeperiod=20)
WMA60 = ta.WMA(test['close'], timeperiod=60)
WMA100 = ta.WMA(test['close'], timeperiod=100)
WMA200 = ta.WMA(test['close'], timeperiod=200)

# Set buy signals if current price is higher than 50-day MA
test['Buy'] = (test['close'] > WMA20) & (test['close'].shift(1) <= WMA20)

#plot
tcdf =test[['close']]
tcdf=tcdf.reset_index()
tcdf['date'] = tcdf['date'].apply(lambda x: x.value)

for i in range(len(test['Buy'])):
    if test['Buy'][i]==True:
       
        apd = mpf.make_addplot(tcdf.iloc[i],type='scatter',markersize=20,marker='o')


mpf.plot(test,addplot=apd, type='candle',volume=True)

我运行这段代码并得到这个错误 ---> 33 mpf.plot(测试,addplot=apd,类型='candle',体积=真) ValueError:x 和 y 的大小必须相同

我该如何解决

tcdf:

    date    close
0   1597622400000000000     16.560
1   1597708800000000000     16.120
2   1597795200000000000     15.834
3   1597881600000000000     17.842
4   1597968000000000000     16.387
5   1598054400000000000     18.936
6   1598140800000000000     18.170
7   1598227200000000000     18.074
8   1598313600000000000     17.023
9   1598400000000000000     17.322
10  1598486400000000000     17.649
11  1598572800000000000     18.294

测试:

time    open    high    low     close   volume  year    month   day     hour    Day_of_week     Buy     BelowMA
date                                                    
2020-08-17  00:00:00    15.499  16.956  15.228  16.560  1297237.309     2020    8   17  0   0   False   False
2020-08-18  00:00:00    16.560  17.578  16.010  16.120  968575.523  2020    8   18  0   1   False   False
2020-08-19  00:00:00    16.119  17.080  15.465  15.834  987213.085  2020    8   19  0   2   False   False
2020-08-20  00:00:00    15.825  17.949  15.807  17.842  915874.788  2020    8   20  0   3   False   False
2020-08-21  00:00:00    17.842  19.854  16.361  16.387  2428489.231     2020    8   21  0   4   False   False
2020-08-22  00:00:00    16.368  19.191  15.623  18.936  1969925.069     2020    8   22  0   5   False   False
2020-08-23  00:00:00    18.935  19.757  17.715  18.170  1223037.344     2020    8   23  0   6   False   False
2020-08-24  00:00:00    18.187  19.467  17.900  18.074  835648.518  2020    8   24  0   0   False   False
2020-08-25  00:00:00    18.068  18.261  16.132  17.023  1116590.644     2020    8   25  0   1   False   False
2020-08-26  00:00:00    17.023  18.040  16.837  17.322  1003044.736     2020    8   26  0   2   False   False
2020-08-27  00:00:00    17.324  18.200  16.420  17.649  1141649.079     2020    8   27  0   3   False   False
... .

我不知道为什么会出现这个错误

此行为错误实数

tcdf['date'] = tcdf['date'].apply(lambda x: x.value)

当它不存在时我得到错误 必须是实数,不是 Timestamp

问题是您一次只用一个数据点调用 make_addplot()make_addplot() 调用中也不需要日期索引;只有数据。

此外,传入make_addplot()的数据长度(行数)必须与传入plot()的长度(行数)相同。

make_addplot() 应该 而不是 在循环中。

尝试替换这部分代码:

tcdf =test[['close']]
tcdf=tcdf.reset_index()
tcdf['date'] = tcdf['date'].apply(lambda x: x.value)

for i in range(len(test['Buy'])):
    if test['Buy'][i]==True:
       
        apd = mpf.make_addplot(tcdf.iloc[i],type='scatter',markersize=20,marker='o')

具有以下内容:

tcdf = test['close'].copy()

for i in range(len(test['Buy'])):
    if not test['Buy'].iloc[i]:
       tcdf.iloc[i] = float('nan')

apd = mpf.make_addplot(tcdf,type='scatter',markersize=20,marker='o')