Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sladerix opened this issue Jan 2, 2024 · 1 comment
Open

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

Sladerix opened this issue Jan 2, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@Sladerix
Copy link

Sladerix commented Jan 2, 2024

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

@Sladerix Sladerix added the bug Something isn't working label Jan 2, 2024
@DanielGoldfarb
Copy link
Collaborator

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

@DanielGoldfarb DanielGoldfarb added enhancement New feature or request and removed bug Something isn't working labels Jan 5, 2024
@DanielGoldfarb DanielGoldfarb changed the title Bug Report: zero-size array to reduction operation maximum which has no identity mplfinance warn if user passes an empty data set. Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants