Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trouble respecting QTRadableStocksUS in my algorithm

Hey all,

I am relatively new to the Quantopian platform and still writing my first algorithms.
To my understanding, the attached algorithm is supposed to trade only stocks that pass the filter QTradableStocksUS.
However, in my backtest, it says that my algorithm does not meet the Tradable Universe criteria.

I have trouble complying to that specific criteria in my algorithms and have a hunch that it is a beginner's mistake par excellence.

Any help/advice is appreciated

4 responses

I believe the issue isn't exactly the algo isn't meeting the QTradableStocksUS requirement. By screening for these in your pipeline, and trading every day, you are ensuring that is met. I think the issue is that the algo doesn't meet the 'Low position concentration' requirement of no more than 5% of capital invested in any one asset. The message in the backtest is a bit misleading.

Note that your current algo only holds two securities. That makes sense. The objective is to maximize alpha. The only constraints are 1) it have a max leverage of 1.0 and 2) that it be dollar neutral. It simply invests in the top two securities with the highest alpha (one long and one short) . If it's trying to maximize alpha (without any other constraints) there would be no reason to invest in any securities with less than the highest alpha. This of course doesn't pass the contest requirement of less than 5% invested in any one asset.

Add a constraint to limit position concentration. There are several ways to do this but a simple one is:

    max_concentration = opt.PositionConcentration.with_equal_bounds(-.05, .05)

See attached backtest with this constraint added. It now passes the QTradableStocksUS requirement.

Thanks Dan for your response. Indeed, your proposed algorithm solves the issue. But I still have some questions if you don't mind.

I am trying to go over the several criterias for the contest and explore them one by one, the different ways to satisfy them...
I thought that the Tradable Universe criteria would be the easiest to comply to. For the first algorithm, I went over the different positions it held in the backtest and it has traded 6 stocks: NVR, RAD, FBC, TAT, GSS and CLWR.
I have to manually check if these stocks belong to the QTradableStocksUS universe, probably using a notebook.

So, in your opinion Dan, do you think it is a simple bug or one of the 6 stocks above are outside of Quantopian's universe?

Another thing I noticed: the line you added is supposed to limit the maximum percentage of the portfolio invested in a single stock, whether it is long or short, to 5%? I ran the backtest and the criteria "Position concentration" is not satisfied.
Which brings me to a more general question:

  • Is there some inherent margin of errors/differences/variance/incongruity... between the algorithm (in this case, defining constraint) and the results of the backtest (due to the way the backtest is simulated maybe)?

I come from a development background and I may be missing something related to the inner working of the Pipeline, or the way it simulates a real market.

I hope my last question was not too broad.

Thanks in advance

Your first question... do you think it is a simple bug or one of the 6 stocks above are outside of Quantopian's universe?

The stocks are probably all in QTradableStocksUS, however, as the post above shows, I feel it's simply the concentration is too high with only 6 stocks. It's not really a 'bug'. Just perhaps misleading. Use the QTradableStocksUS filter and rebalance daily (so the stocks are current) and you should meet the Tradable Universe criteria.

Second question.. Is there some inherent margin of errors/differences/variance/incongruity... between the algorithm (in this case, defining constraint) and the results of the backtest

There isn't any error or differences between the algorithm and the backtest. The backtest simply executes the algorithm. However, ones algorithm doesn't always act the way it appears. If you look at the above algo, it has a max position constraint of 5%. However, it fails because some security had a position size of 5.2%. How could that be? Simple. The algo uses the current minute prices to determine position sizing. It places market orders which get filled sometime over the following minutes. The price it fills at may not be the price at the current minute which the optimizer was using. Moreover, the position sizes used by the contest are calculated at the end of the day. If a particular stock went up in price (relative to other stocks) during the day then, at the end of the day it would have a higher position concentration. One places orders but the fill prices and the actual end of day prices are pretty volatile (the trick is to predict those). So, initially, maybe just set the position concentration to something lower like 4%.

You aren't missing anything big about how Zipline (the software behind the backtester) simulates the real markets. It does a pretty good job. Rather, think through the (sometimes unexpected) details on how orders are actually filled and how then position values can swing during the day.

Hope that helps.

Thanks Dan, your answers clear a lot of smoke. Especially around the way the simulations are run. I just want to get a practical/hands-on understanding of the algorithms, and the way they simulate the market, of which I know very little.

So, if I understood right, one of the companies is bought and reaches a point where it does not satisfy the filter QTradableStocksUS but is still being held because it was not subject to the rebalance function? Hence, I should rebalance somewhat frequently my portfolio?
If my rebalance function contains a logic where it would drop shares held of a stock just because that stock, on that particular day, does not satisfy the tradable universe filter, a type of overfitting?

Also, is it common and considered a usual practice to drop the constraints a bit to avoid such edge cases?

Don't bother much with the questions above (I kind of drifted there :D), I think that the tradable universe criteria won't be much of a problem, since it is never used alone.