Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Statistics Stop Calculating, Universe Shrinks

I have managed to thoroughly break Quantopian under a specific set of circumstances. If I change the start date of this backtest, the problem will go away. The problem appears to start at the end of the quarter, leading me to suspect something with set_universe.

The symptom is that all of the sudden after June 30, 2009 the frames seem to go back to daily (or maybe I only get one frame per day). Orders seem to be frozen, and statistics stop updating.

EDIT: The frames are still minutely - that was a red herring. But I did notice that my universe shrinks suddenly to 1 stock and I have orders that never seem to fill. I can get things moving again by cancelling the order for "BGM" from within the algo - then my universe fills back up and my P&L begins to update again. BGM is some kind of General Motors stock from around the bankruptcy, so this is probably sending the system into a weird state.

4 responses

So my latest version cancels any open orders at the beginning of each day and makes a "black list" of troublesome securities. This seems to kill off any weirdness and lets the P&L calculation proceed normally.

I apologize for the obtuse code and the lack of comments - I am currently in the process of re-implementing many of my past algorithms using a shared code library. Hopefully some still can find it useful, even if my code seems needlessly complex.

Hi Ray,

This certainly looks suspicious. Thanks for letting us know, we're digging into it and I'll let you know once I have more information.

Cheers,
Alisa

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.

What a tough algo! After running a variety of backtests and combinations of set_universe, it looks like your algorithm doesn't recover after the end of a quarter if one of those stocks went bankrupt.

In the example that you posted, your algo maintained its positions in SOQ and GNLB after the universe rollover. It wasn't able to close these positions because these stocks stopped trading during this period and we couldn't sell the stock. In the backtester, the last-known price gets forward filled. The year 2009 had many stocks exit the exchanges as companies went bankrupt!

I attached a backtest that ran from January - August 2009. I expected the algo to close the positions at the end of the quarter (March, June) and buy the new universe the following day. However, after one universe change (March 31 or June 30 as you pointed out) the algo doesn't buy the new stocks.

I dug through the code by heavily logging the information and it looks like after March 31, the line "checkFrameConditions(context, data, sellAllAtQuarter=True)" always returns true. Digging further, this line gets evaluated to true each bar after the universe rolls over:

if not context.last_frame is None and get_datetime().date() == context.last_frame.date()  

As a result, the algo exits the run of handle_data() and it doesn't reach the reallocate_stocks method to buy a new universe.

If you change the set_universe to use higher-volume stocks ( 61-63%) these survived the financial downturn and kept trading. And your algorithm continues buying/selling orders. Hope this helps and if any you have any follow-up questions, I'd be happy to take another look.

Ahh, thank you - that explains it perfectly. In other words, my algo gets hung up on the bankrupt stock because the trade never goes through, and since I only hold bankrupt stocks, my P&L calculation stays constant. This also explains why cancelling the order lets me move on with life.

Thank you for the thorough analysis. Going forward, I'll have to remember to not simply check for open orders, but also remember to cancel them if they persist too long. This is probably more realistic anyway, since I believe IB cancels any leftover orders at the end of each trading day.