Although I don’t consider myself a newbie, I would regard myself as inexperienced with Python. I am new to MPLFinance.
I am tryting to plot trade entry points on a forex mplfinance chart. I have the basic chart running OK. My data is coming from a DataFrame, which is populated from a CSV import. One of the columns called ‘Golden_Cross’ contains the word ‘long’, which is the signal I want to plot with an arrow.
I am trying to emulate the percentB_belowzero
function in the mplfinance documentation. I understand the jist of what the function does, but some of the details is lost to me. e.g. what is the significance of price[date]
, and what is previous
used for? This may not be important. The below code is what I have so far:
import pandas as pd
import mplfinance as mpf
import math
def go_long(df):
signal = [math.nan] * len(df)
print("len(df) = " + str(len(df)))
print("len(signal) = " + str(len(signal)))
for i in range(1, len(df)):
if df['Golden_Cross'].iloc[i] == "long":
signal.append(df['close'].iloc[i] * 0.99)
else:
signal.append(math.nan)
return signal
if __name__ == '__main__':
df = pd.read_csv('GBPUSD_MA_Crossover2.csv', index_col=0, parse_dates=True)
df.index.name = 'date'
long_signal = go_long(df)
apds = [ mpf.make_addplot(df[['fast_ma', 'slow_ma']], panel=0, title='GBPUSD'),
# mpf.make_addplot((df['dmi']),panel=1, title='DMI'),
mpf.make_addplot(long_signal, type = 'scatter', panel=0, markersize=200, marker='^')
]
mpf.plot(df, type= 'candle', tight_layout=True, datetime_format='%Y-%m-%d', volume=False, show_nontrading=False, addplot=apds)
When I run the above code I get the following stack trace (sorry about the formatting):
> len(df) = 381 len(signal) = 381
> Traceback (most recent call last):
> File "/data/stuart/Projects/Python/BackTesting/Scratches/charting1.py",
> line 25, in <module>
> mpf.plot(df, type= 'candle', tight_layout=True, datetime_format='%Y-%m-%d', volume=False, show_nontrading=False,
> addplot=apds)
File
> "/data/stuart/Projects/Python/Env/lib/python3.12/site-packages/mplfinance/plotting.py",
> line 808, in plot
> ax = _addplot_columns(panid,panels,ydata,apdict,xdates,config,colcount)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "/data/stuart/Projects/Python/Env/lib/python3.12/site-packages/mplfinance/plotting.py",
> line 1139, in _addplot_columns
> ax.scatter(xdates, ydata, s=size, marker=mark, color=color, alpha=alpha, edgecolors=edgecolors, linewidths=linewidths,label=label)
>
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File
> "/data/stuart/Projects/Python/Env/lib/python3.12/site-packages/matplotlib/__init__.py",
> line 1473, in inner
> return func(
> ^^^^^ File "/data/stuart/Projects/Python/Env/lib/python3.12/site-packages/matplotlib/axes/_axes.py",
> line 4787, in scatter
> raise ValueError("x and y must be the same size") ValueError: x and y must be the same size
I don’t understand the reference to ValueError: x and y must be the same size
. My signal
list and df
Dataframe are the same size (see the print statement output).
Can someone please help me get this code running correctly please.
Best regards, StuartM