ZeroDivisionError: float division by zero

Does anyone know how to get around this error, "ZeroDivisionError: float division by zero"?

There are points in my algo during downturns where nothing will be bought (purposely), but I keep getting this error. I'm not really sure how to get around it. So when it tries to run this "context.long_weights = 1.0/len(context.longs)" with no stocks passing the filter, I receive the error.

Thanks

1 response

Jack,

Try this:

    context.long_weights = 1.0/len(context.longs) if len(context.longs) > 0  else 0.