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