1
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)

I run this code and get this eror ---> 33 mpf.plot(test,addplot=apd, type='candle',volume=True) ValueError: x and y must be the same size

how I can fix it

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

test:

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
... .

I dont know why I get this eror

this line for error real number

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

when it is not exist i get error must be real number, not Timestamp

0

1 Answer 1

1

The problem is you are calling make_addplot() with only one data point at a time. There is also no need for the date index in the make_addplot() call; only the data.

Also, the length of data (number of rows) passed into make_addplot() must be the same as the length (number of rows) pass into plot().

make_addplot() should not be within the loop.

Try replacing this part of the code:

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')

with the following:

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')
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.