Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Bug: order_optimal_portfolio tosses small weights

This has been driving me nuts. I've been trying to order portfolios consisting of the entire QTradableStocksUS universe (2000+ stocks) and ending up under-leveraged and with only 600 positions. It's not an issue with partial fills. I've boiled it down to a bug in order_optimal_portfolio.

To illustrate, here's an algorithm that uses rank & demean to create a nice even gradient of portfolio weights from a nonsense alpha factor.

pipLen is the number of values returned by pipeline. posLen is the number of positions held at the end of the day. As you can see, the number of positions held is already lagging the number of position weights fed into order_optimal_portfolio.

6 responses

What happens if we decrease all the weights by 1/10th and increase the starting capital from $10mm to $100mm? You'd think the effect on portfolio construction would be negligible. However, this is not the case. It doesn't order anything, because it clips away any weights below some 0.01%.

Using the old order_target... functions instead of order_optimal_portfolioand it works exactly as one would expect.

There's something wrong with order_optimal_portfolio. How is anybody constructing diversified portfolios with a smooth gradient of portfolio weights if the function simply tosses all the positions weighted lower than 0.01%? 0.01% times ~2000 QTU stocks means that up to 20% of an intended portfolio ($2mm out of a $10mm book) could be unaccounted for. That's not an insignificant contribution!

In case run_optimization() might shed some light on it.

The magic number is .0001. If the absolute value of a weight is less than .0001 it will effectively be 'rounded' to zero. That's what's causing the behavior you are seeing.

Before getting into the 'why' let's look at the 'what'. For a $10M portfolio, the order_optimal_portfolio method will effectively not open any position with an absolute weight less than .0001 or $1000 (.0001 x $10,000,000). It will also close any existing position with a value less than $1000. For a stock that trades at $20 that's 50 shares. That of course is a percentage. For a $1M portfolio that minimum is only $100.

Why is this? This was introduced to 'fix' the issue of the optimizer leaving only a few shares of a stock in one's portfolio. This issue had the undesirable consequence of potentially leaving a stock, which may no longer be in the QTradableUniverse (QTU), in one's portfolio. Having too many stocks for too long not in the QTU can disqualify an algo from the contest. So, now the order_optimal_portfolio method will close any positions with a portfolio weight less than .0001 to avoid this. The behavior now also acts as a behind the scenes 'housekeeper' closing (or never opening) very small positions.

This minimum weight is actually implemented as adding an extra penalty on these target-zero positions. It isn't a "hard constraint" that a position can never be under .0001. If the optimizer cannot find any other solution to satisfy all the constraints there is the potential for some positions to be weighted less than .0001. It effectively acts as a "soft constraint".

I made a change to the algo above which "clips" all the weights to a value a little more than .0001. This ensures all the target weights are above that magic number and do not get 'rounded' to zero. The algo now runs as expected. The number of securities ordered by order_optimal_portfolio is generally equal to the number of securities returned by the pipe. There are times where the portfolio positions are greater than the pipeline however, these are due to close orders not completely filling by the end of the day.

Hope that helps explain the sometimes baffling behavior of the optimizer.

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.

Awesome explanation Dan, as always. Thank you!

Thanks, Dan. I agree that a $1000 position in a $10mm portfolio is insignificant. However, once you have 600 $1000 positions disappearing, it starts causing unintended consequences with the gross leverage and also some effect on reducing the amount of diversification present.

I tend to use the opt.CannotHold(set(context.portfolio.positions) - set(qtu)) constraint to make sure I'm not holding any non-QTU positions. I also make sure their weights are set to 0.

Thanks for the clip example code! That'll probably help. I'll just round up to the cutoff for weights that are close enough.