Back to Community
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.


1 response


Try this:

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