Hi Crawford,
I played with this a bit. I think the ranking problem you were running into had to do with the filters set in before_trading_starts. I had some concerns about this template when I wrote it, regarding the >0 filters being done in before_trading_starts, sorry about that. In the attached version, I moved those into initialize, so they can be done as part of the pipeline. This ensures that the ranks stocks will not included anything where factor 1 or factor 2 is <0.
# create my market cap factor
mkt_cap = MarketCap()
# create 3 filters - one to limit to top 2000 by market cap
# the others to remove any factor 1 and factor 2 less than 0
top_2000 = mkt_cap.top(2000)
factor_1_filter = factor1 > 0
factor_2_filter = factor2 > 0
# create a combined filter
total_filter = (top_2000 & factor_1_filter & factor_2_filter)
# use the combined filter to set the screen
pipe.set_screen(total_filter)
Additionally, I made some changes to the logging.
- On line 112, I logged the total size of the output dataframe with
log.info("\n" + str(len(context.output))) this lets me how many stocks we get back on any given day.
- On lines, 138 and 143, I changed how we were logging information about when we were longing and shorting stocks. Before we were running into issues with exceeding the number of log lines on any given day, so you never saw when the shorts were happening. Now this should be more apparent.
I think this answers your ranking and shorting questions, however the very weird thing, that I cannot explain, is that I am getting a different number of securities in my pipeline output each time I run it, for the same days. When I start my backtest on 1-5-15 the number of securities returned on that day changes.....this shouldn't be happening as best I can tell. I'm going to ask someone else to take a look and see if we can come up with an explanation.
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.