matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.48k stars 613 forks source link

mplfinance warn if user passes an empty data set. #656

Open Sladerix opened 6 months ago

Sladerix commented 6 months ago

Hi everyone I got this error while trying to achive a graph with signal markers... if I add a scatter plot I get this error:

E ValueError: zero-size array to reduction operation maximum which has no identity

initially I thought to be my fault... but then I tried some examples out there and all behaves the same.

You can try executing those examples to see the error https://code.luasoftware.com/tutorials/algo-trading/mplfinance-plot-marker-on-price-chart

DanielGoldfarb commented 6 months ago

From the Traceback, you can see the error begins on the following line in mplfinance:

 File "/home/dino/code/mplfinance/src/mplfinance/plotting.py", line 1108, in _addplot_columns
      ymhi = math.log(max(math.fabs(np.nanmax(yd)),1e-7),10)

The numpy function np.nanmax() attempts to determine the maximum value in an array, ignoring any nan values. The problem is that all the values in up_markers and down_markers are nan: Adding the following to the code:

print("all([np.isnan(x) for x in up_plot['data']]) = ",end='')
        print( all([np.isnan(x) for x in up_plot['data']]) )
print("all([np.isnan(x) for x in down_plot['data']]) = ",end='')
        print( all([np.isnan(x) for x in down_plot['data']]) )

gives:

all([np.isnan(x) for x in up_plot['data']]) = True
all([np.isnan(x) for x in down_plot['data']]) = True

This is not a bug in mplfinance, but a problem of passing essentially no data as an addplot.

I could have mplfinance check for all nan, but I would still have it throw an exception, perhaps with a more explicit message as to what is wrong. (I definitely would not allow mplfinance to ignore the fact that the user has passed in no data for the addplots, as this could be even more confusing to the user when they don't see their addplots in the plot). That said, in situations like this (bad data) I have been reluctant to add more and more checks to mplfinance as it is already slow enough as it is. So for now, I mostly leave it up to the user to confirm that their data is valid.

The message (E ValueError: zero-size array to reduction operation maximum which has no identity) you are seeing is coming from numpy as a result of the line of code shown in the Traceback ... so this was not difficult to track down: I simply looked at the line of code and determined what in that line of code could possibly be a zero-size array