Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
ZeroDivisionError

Hi,

Can you help me, please?
I'm getting error: "11 Error Runtime exception: ZeroDivisionError: float division by zero" when running this code:

def initialize(context):  
    pass  
def handle_data(context, data):  
    stock = sid(24)  
    if not context.portfolio.positions:  
        log.debug('portfolio is empty, buying')  
        order(stock, 50)  
    else:  
        value = context.portfolio.positions_value  
        log.debug('trying to divide on positions_value %f' % value)  
        foo = 10000./value  

portfolio.positions_value is definitely not zero as 50 AAPL shares was bought on first iteration. Is it a bug or I'm missing something obvious?

Regards,
Ed

7 responses

Hello Ed,

Got it to work...have a look at the attached backtest. Hopefully it solves your problem.

Grant

Thank you for the help, Grant!

I still don't get why that error happened. If you run below code you'll see from the log that context.portfolio.positions_value is not zero starting from the second iteration:

def initialize(context):  
    context.stock = sid(24)

def handle_data(context, data):  
    log.debug(context.portfolio.positions)  
    if not context.portfolio.positions:  
        log.debug('portfolio is empty, buying')  
        order(context.stock, 50)  
    else:  
        log.debug('portfolio value: %f' % context.portfolio.positions_value)  
        # foo = 1000/context.portfolio.positions_value

However, if I uncomment last line it starts giving ZeroDivisionError.

You're welcome, Ed,

The behavior here is really weird. Seems like a bug. I was able to avoid the error by using numpy.divide, but the record function indicates an inf value (presumably from the same divide by zero problem). I'll send a note to the Quantopian guys asking that they have a look at this discussion thread.

Grant

Grant, thank you for helping me with this!

BTW, do they have some kind of issue tracker, where I can post bugs and feature requests?

This seems to work:

def initialize(context):  
    pass  
def handle_data(context, data):  
    context.stock = sid(24)  
    if context.portfolio.positions_value == 0:  
        log.debug('portfolio is empty, buying')  
        order(context.stock, 50)  
    else:  
        value = context.portfolio.positions_value  
        log.debug('trying to divide on positions_value %f' % value)  
        foo = 10000/value  
        print "foo = " + str(foo)  

P.

Yeah. Looks like a bug.

This code

def initialize(context):  
    context.count = 1

def handle_data(context, data):  
    log.debug('iteration %d' % context.count)  
    if not context.portfolio.positions:  
        log.debug('portfolio is empty, buying')  
        order(sid(24), 50)  
    elif context.portfolio.positions_value:  
        value = context.portfolio.positions_value  
        log.debug('trying to divide on positions_value %.2f' % value)  
        log.debug('foo = %.2f' % (1000./value))

    context.count += 1

also works and produces this output:

2008-01-04 handle_data:5 DEBUG iteration 1
2008-01-04 handle_data:7 DEBUG portfolio is empty, buying
2008-01-07 handle_data:5 DEBUG iteration 2
2008-01-07 handle_data:11 DEBUG trying to divide on positions_value 8879.00
2008-01-07 handle_data:12 DEBUG foo = 0.11
2008-01-08 handle_data:5 DEBUG iteration 3
2008-01-08 handle_data:11 DEBUG trying to divide on positions_value 8561.50

Ed,

There is no issue tracker for Quantopian users (although for zipline on github there is this an issue tracker).

Grant