Skip to content

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

Open

Description

@Sladerix

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

Activity

  1. DanielGoldfarb commented on Jan 4, 2024

    @DanielGoldfarb
    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

  2. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Jan 5, 2024
  3. 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.[/+] on Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions