Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Seems to be an error when ordering some stocks

order_value(stock,rebalance_budget) #error when ordering certain stocks which expire soon, need to look into this

KeyError: Security(14868, symbol='BETM', security_name='AMERICAN WAGERING INC', exchange='NASDAQ GLOBAL MARKET', start_date=Timestamp('1996-05-10 00:00:00+0000', tz='UTC'), end_date=Timestamp('2006-03-30 00:00:00+0000', tz='UTC'), first_traded=None)

run this in the debugger: order_value(sid(14868),1) or this data[sid(14868)].price both fail

i get the same error if try to do order_target_percent. The only time i don`t get the error is when i use order(stock,1)
I am running the test between 2002 and 2015, this occurs at the very beginning when the date is 2002-01-03

Any ideas ?

If I implement some logic to deal with the stocks which have end_dates isn`t that a bias toward the future?
How will this work in a live trading environment, will it crash the algorithm when stocks are being taken of the market ?

Many thanks,

4 responses

i handled the exception, i`m not sure if this would be valid for real trading though

for stock in context.fundamental_df:
if (stock not in context.portfolio.positions):
#log.info("Ordering %s" % (stock.symbol))
#add/update the stock to the list
try: #workaround for the ordering bug
stock_price = data[stock].price
context.mypositions[stock] = context.days
order(stock,rebalance_budget/stock_price) #use order otherwise get error for certain stocks
except Exception, e:
log.info('exception when ordering % ' % e)

Hi Attila,

You're running into a common problem presented (as is often the case) when there's an illiquid security or we don't have data for that security at that bar. You can fix it by using something like 'if stock in data: order_X' in your code or view here https://www.quantopian.com/help#api-common-errors for more information.

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.

Thanks Seong.

Hi Seong,

Perhaps you could elaborate on what's going on in the back-end. My understanding is that once a given stock has its first historical trade of the backtest, forward-filling of price data is automatic (see https://www.quantopian.com/posts/thinly-traded-stocks-why-no-gaps). So, there will be no empty bars going forward from that first bar. But then, at some point, if the stock stops trading, do you stop forward filling? Or is the stock just dropped altogether from the data object? Or something else?

And what if I use 'history'? When does the filling of prices in data start? I realize that the DataFrame returned by history may or may not be forward-filled depending on the options; I'm wondering if the use of history will change the start date of the filling of prices in the object 'data'.

Grant