Back to Community
dealing with securities that expire?

It has come up a few times here that the backtester does not automatically manage securities that expire, within the dates of the backtest. Recent experience in dealing with larger universes of securities and expansion of the tradeable universe suggests that this issue will become more prevalent.

The problem is that if a position is held in a security beyond its expiration date, the position cannot be closed (and its value in the portfolio becomes increasingly murky). There are some work-arounds, but it seems that an option should be available within the backtester to manage such securities. Kinda messy (which is probably why there is no built-in backtester code):

  • Do nothing (user will manage via custom code).
  • Exclude all securities that will expire within backtest
    dates (look-ahead bias, right?).
  • Automatically close positions upon expiration (biased?).
  • Automatically close positions N minutes/days prior to expiration (potentially biased?).
  • More complicated logic, based on price, volume, etc. and expiration date, in an attempt to match exchange rules for dropping securities.

"Automatically close" means override the backtester, use the last known price, and close out, as if there had been a trade event (i.e. force an order, even though the volume is zero).

Also, shouldn't there be a warning at the end of a backtest, "Backtest may be invalid, due to expired securities." It would also be handy to add a field that flags securities within the portfolio that have expired (and have that field available in the Quantopian research platform, via get_backtest()).

Is there a standard way to handle expirations in the world of backtesting?

When trading with real money at IB, and a security expires that is held in a Q portfolio, what happens?

Is this on the Q to-do list? If so, what is the priority? When might it be addressed (days/weeks/months/years/never)?

12 responses

At Ameritrade years ago I invested in a solar company that dropped to zero before I noticed, and now I know of no way to remove from the list.

To avoid that here, maybe:

if data[stock].price < .50:    # Sell if price fell to 50 cents a share  
    order_target_percent(stock, 0)  

I've been thinking about this one recently. I'm currently leaning towards your third choice, which is to close positions that are delisted. If we do it at end-of-day on the last day it's listed, I don't think we have any look-ahead bias problems. The problem we have, though, is value. If something is delisted, in a lot of cases the last-traded price is going to be too high. Even if the last-traded price is decent, it's not clear how you'd model slippage, either. We could just model the price of a delisted stock as zero. That would be the most conservative choice. However, it would be overly harsh for the stocks that keep trading on the OTC. Plus, there are stocks that get delisted and then get re-listed after they clear up whatever got them in trouble in the first place (like earnings restatements). Not an easy problem.

It's on the list, but I think of it as more of a months/years fix, not a days/weeks fix. It will go up the list if we see it interfering more with strategies. In general, so far, I see it more as a nuisance than a major impediment to accuracy.

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.

Does it affect reported leverage?

@Dan, I would think you'd want to 1) Assume a last day, $0.00 price delisting action. 2) Close such positions and drop from a strategy's positions collection. 3) Any strategy that wants to be proactive about dropping about-to-be-listed, or under $1.00 price securities can still do so -- prior to the drop date. Currently hung securities maintain some erroneous price, that hangs around corrupting leverage and position counts. Imagine some strategy that just bought 1 share of the top 500 securities of the NASDAQ every month. And sold off those that weren't in the top 500. By the end of the test you'd have hundreds of orphaned positions.

Seems a hack slippage model could be set up to handle applying the $0.00 price at the end date, but if I'm not mistaken, there is no way to force an order to go through unless there was an "event" for the stock, right? It would have to be a kind of fundamental override of how the backtester works. Or is there some way to modify the price data for a given stock, so a trade for $0.00 shows up at the end date?

Related also to when a company changes its symbol?
Example in early May, FCZA changed to CIVB, result: "Something went wrong and your live algorithm has stopped."

I'd suggest having a function one can use to set the back-tester default behavior of action when a security gets delisted. Two options would be: 1) close the position at $0.00 price, or 2) close it at the last day close pice, then it's the developer choice to use or not the function and how. Keeping those delisted positions in the portfolio creates a nightmare for measuring the real leverage of the account.

My sense is that if there were a way to force closing out of positions at a specified price ($0 or whatever--take your pick), it would be a fix. It would be a matter of hitting the end date (or N days/minutes prior) and closing. Then, I think that 100% of the stragglers could be eliminated. Does anyone know how to do this? Maybe there is a hack?

Backtesting in the Quantopian research environment appears to be fundamentally different, in that the entire price history for all securities of interest is loaded into RAM (presumably) prior to the backtest. This means that the price history can be edited, in the case of securities that will expire within the backtest period. For example, a price of zero (or whatever one wants) at the expiration date, with a large traded volume, could be inserted into the bar data, so that an order could be forced, to close out a position at a specified price.

@ Dan,

there are stocks that get delisted and then get re-listed after they clear up whatever got them in trouble in the first place (like earnings restatements).

Wouldn't the stock appear with a new security ID in the Quantopian database, if this happened?

I notice https://github.com/quantopian/zipline/pull/460 -- is this a teaser that this is going to get handled properly soon? It's turning into a real problem for algorithms based on fundamental screening...

Well, "properly" is a relative term. For backtesting, I see how code could be written to identify the last trade for a given security, and triggering a close out of positions in that security. How would this work for Quantopian paper trading? And Interactive Brokers paper and real-money trading?

As a specific example, say I'm trading XYZ via Quantopian/IB, with real money. It is announced that XYZ is scheduled for a definite delisting on some future date. Would I be notified by Quantopian? IB? Would my algo have access to this info. (e.g. via the security object)? Or would I need to put it in manually (e.g. either via fetcher or by stopping the algo and hard-coding)? Upon delisting, would I be able to transact XYZ via my algo? When backtesting, if the end_date passes, there's no way to transact, right? Is this the same for real-money trading? Do I end up having to manually trade XYZ somehow (e.g. go into my IB account and click some buttons)?

The other thing is that I'm not so sure that this suspension/delisting business is an irrelevant, messy "corner case." One has to think that there are hedge-fund-relevant strategies around suspension/delisting events. Wouldn't such stocks get dumped by indices, ETFs, mutual funds, etc. and become less correlated with the overall market? And it's not always rinky-dink, no-name companies. For example, http://money.cnn.com/2010/06/16/news/fannie_freddie_delisting/ and http://www1.nyse.com/press/1243851624885.html. Maybe my intuition is off, but the Q hedge fund and associated research, backtesting, and trading apparatus is gonna need to be ideally suited to handle thinly traded, odd-ball, corner-case, whacky stuff...trading in household names like AAPL and SPY won't work.

We've made an update to how the backtester handles delisted securities and removes them from your portfolio. You can learn more about it here.

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.