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.