Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help with "ValueError: cannot convert float NaN to integer USER ALGORITHM:86, in handle_data"

Currently running into this issue:

ValueError: cannot convert float NaN to integer
USER ALGORITHM:86, in handle_data

How can I handle this? Thanks in advance.

2 responses

At this point in time the close for SPY is nan - there is no close price. Now it depends how you would like your algorithm to handle this.
One way would be to use 'price' instead of 'close' because 'price' is always forward filled

num_shares = math.floor(context.max_notional / data.current(context.stock, 'price'))  

This means your algorithm acts on the last known price.
Alternatively you could tell it to do nothing if there's no recent close, like so

    if np.isnan(data.current(context.stock, 'close')):  
        return

That makes sense, thanks Tentor!