Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Getting some new 'KeyError' lately + algo disqualified.

I'm getting some new 'KeyError' lately... I was working on a custom asset allocation algo, so I thought it was probably my code, but this morning I got a 6-months+ running algo disqualified by the same error. It seems to happen when I order a 0% target on a stock that is no longer in the current universe.

I often use this code on top of my strategies for safety. It used to work fine until now...

 for stock in context.portfolio.positions:  
        if stock not in data:  
            order_target_percent(stock, 0)  

Anyone else with the same problem? Or am I doing this wrong?

Thanks!

4 responses

This is a new exception that they rolled out a few weeks ago. I got burned by it too. Trying to order a delisted stock will now throw an exception and halt your algo. :(

EDIT: actually I am not sure if this is the same problem or not, our error was: There was a runtime error on line 343. Order for xxx shares of Equity(...) at 2015-10-14 13:34:00+00:00 violates trading constraint AssetDateBounds() (Metadata: {'asset_end_date': Timestamp('2015-10-13 00:00:00+0000', tz='UTC')}).

Hi Charles,

We're looking into this now. At first glance, it doesn't seem to be the same error that Simon is referring to but I will let you know when we figure it out.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

I have always bumped into KeyErrors while trying to order things that are not in data. Having to check if a stock is in data is, by far, my biggest pet peeve with the system -- the stock exists, it's quoted, I should not have to wait arbitrarily until it's in data before placing an order. I'm still not entirely sure what determines whether something is or is not in data, perhaps if it's traded already today? But that's such an arbitrary and meaningless distinction; why can't I be the first trade of the day?

Simon.

Thanks Simon and Jamie,

It sounds like it's related to stock splits. Here's my error:

KeyError: Equity(49242, symbol=u'PYPL', asset_name=u'PAYPAL HLDGS INC COM W.I.', exchange=u'NASDAQ GLOBAL MARKET', start_date=Timestamp('2015-07-06 00:00:00+0000', tz='UTC'), end_date=Timestamp('2015-10-28 00:00:00+0000', tz='UTC'), first_traded=None)

There was a runtime error on line 139:
order_target_percent(stock, 0)