Back to Community
KeyError with order_target_percent

Hi !

My algorithm ran into a KeyError during a backtest and I cannot find the solution.

Here is the log (at May 31, 2011 backtest time):

KeyError: Equity(32043, symbol='QVCB', asset_name='LIBERTY INTERACTIVE CORPORATION QVC GROUP', exchange='NASDAQ GLOBAL SELECT MARKET', start_date=Timestamp('2006-05-10 00:00:00+0000', tz='UTC'), end_date=Timestamp('2015-07-20 00:00:00+0000', tz='UTC'), first_traded=None)
There was a runtime error on line 70.

Here are the lines 69-70:

for stock in weights.index:  
        order_target_percent(stock, weights[stock])  

The variable weights is a Series object, weights[sid(32043)] is a positive float.

The universe is set up with a call to get_fundamentals()

Could it be due to a lack of trading activity for that stock on the May 31, 2011 ? Or is it related to the for loop ?

Thanks in advance for your help :-)

4 responses

So the most likely reason for the key error assuming the security object is actually in your data structure is that we are missing data for that security for the current bar. A more technical explanation is that the data parameter passed into handle_data is a data structure keyed by Security object, containing information about the given security. When that security is missing from data we raise a KeyError exception.

Here is a quick check you can do:

for stock in weights.index:  
    if stock in data:  
        order_target_percent(stock, weights[stock])  

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.

You were right, the security is missing from the data variable. Although it is present in the history() function.
Thanks a lot !

You should be able to submit an order, without causing an error, whether or not the security has traded in the latest time bar.

The problem is that the exchange wants to know how many shares you're ordering. Functions like order_target_percent need the current price to calculate the number of shares, and they probably use unprotected lookup in data, and fail when the price of that security is not there. Instead, those functions should fall back on forward-filled price obtained from history, rather than make users, many of whom are not highly experienced Python programmers, wrack their brains what they possibly did wrong.


I have the same problem !

Is there a way to test data to see if there is a price in it ? And if there is no price for stock, is there a way to give it one with history() ?