Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Possible Quantopian bug: I have a position inside my position

My backtest is failing with an error:

Something went wrong. Sorry for the inconvenience. Try using the built-in debugger to analyze your code. If you would like help, send us an email.  
KeyError: Position({'amount': 223, 'last_sale_price': 28.18, 'cost_basis': 24.1472879949976, 'sid': Equity(6836, symbol=u'SGP', asset_name=u'SCHERING PLOUGH CORP', exchange=u'NEW YORK STOCK EXCHANGE', start_date=Timestamp('1993-01-04 00:00:00+0000', tz='UTC'), end_date=Timestamp('2009-11-03 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None)})  
There was a runtime error on line 220.  

Here are excerpts relevant to this from my code:

def is_equity(stock):  
    return type(stock).__name__.find('Equity') != -1

def is_position(stock):  
    return type(stock).__name__.find('Position') != -1

def close_deselected_positions(context, data, closed):  
    for stock in context.portfolio.positions:  
        if not is_equity(stock):  
            log.info('Stock has unexpected type ' + str(type(stock)))  
            log.info('Stock is identified as position: ' + str(is_position(stock)))  
            log.info('Stock sid: ' + str(type(stock.sid)))  
        if is_position(stock):  
            stock = stock.sid  
            if not is_equity(stock):  
                log.info('* Stock still has unexpected type ' + str(type(stock)))  
                log.info('* Stock is identified as position: ' + str(is_position(stock)))  
        if (stock not in data) or (stock not in context.stocks):  
            log.info('Closing deselected position ' + str(stock))  
            order_target_value(stock, 0.0)  
            closed.append(stock)  
    return closed  

And finally here's the related log fragment. Note that even though I try to access the sid field of a position, I fail. And presumably my dead stock code isn't catching this particular position because I can't seem to access the sid field. I would really appreciate any help with this.

2009-11-24close_deselected_positions:138INFOStock has unexpected type <class 'zipline.protocol.Position'>  
2009-11-24close_deselected_positions:139INFOStock is identified as position: True  
2009-11-24close_deselected_positions:140INFOStock sid: <type 'zipline.assets._assets.Equity'>  
2009-11-24close_deselected_positions:147INFOClosing deselected position Equity(6836 [SGP])  
2009-11-24close_deselected_positions:138INFOStock has unexpected type <class 'zipline.protocol.Position'>  
2009-11-24close_deselected_positions:139INFOStock is identified as position: True  
2009-11-24close_deselected_positions:140INFOStock sid: <class 'zipline.protocol.Position'>  
2009-11-24close_deselected_positions:144INFO* Stock still has unexpected type <class 'zipline.protocol.Position'>  
2009-11-24close_deselected_positions:145INFO* Stock is identified as position: True  
2009-11-24close_deselected_positions:147INFOClosing deselected position Position({'amount': 223, 'last_sale_price': 28.18, 'cost_basis': 24.1472879949976, 'sid': Equity(6836, symbol=u'SGP', asset_name=u'SCHERING PLOUGH CORP', exchange=u'NEW YORK STOCK EXCHANGE', start_date=Timestamp('1993-01-04 00:00:00+0000', tz='UTC'), end_date=Timestamp('2009-11-03 00:00:00+0000', tz='UTC'), first_traded=None, auto_close_date=None)})  

Thanks,

Sunil

4 responses

I am getting different behavior in the full backtest vs build. When I do a build my code behaves as expected. I am able to access the sid within a position. But when I do a full backtest attempting to access the sid field of a position, I get a position. The backtest fails. I am quite frustrated with this situation. Can anyone please help?

Sunil

I have a little bit more to share here... It appears under some circumstances context.portfolio.positions is a list of positions, rather than a map of stock -> position. That would explain why for stock in context.portfolio.positions sometimes gives me positions rather than stocks, but doesn't get me any closer to resolving this issue.

Sunil

Ouch! I hope you hear back from Quantopian about this. If positions really is changing between a map and a list, that would be .. sigh.

Well, the good news is that this might be a case of user error. I'm not sure what I might have been doing wrong previously, but I am not able to reproduce this problem with more recent iterations of my algorithm. I can't get through a full back test yet, though, since other bugs are causing my universe to grow too large, so I can't definitely say the problem's gone. I'm really hoping I don't see this issue again, though!

Sunil