Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
What is the ".rank" supposed to do?

Please bear with me...
This illustration can be replicated on "Build Algorithm" or debugging mode..
Please mark a debug tag (blue tag) on the line 104.

First I am going to disable all of the code below with "#".

    # Rank factor 1 and add the rank to our pipeline  
    factor1_rank = factor1.rank(mask=top_2000)  
    pipe.add(factor1_rank, 'f1_rank')  
    # Rank factor 2 and add the rank to our pipeline  
    factor2_rank = factor2.rank(mask=top_2000)  
    pipe.add(factor2_rank, 'f2_rank')  
    # Take the average of the two factor rankings, add this to the pipeline  
    combo_raw = (factor1_rank+factor2_rank)/2  
    pipe.add(combo_raw, 'combo_raw')  
    # Rank the combo_raw and add that to the pipeline  
    pipe.add(combo_raw.rank(mask=top_2000), 'combo_rank')  

because, I suspect the "rank" not working properly.
click "Build Algorithm"

When I try to call "context.output" in the debug screen....

I get below securities

Equity(24 [AAPL])
Equity(1091 [BRK_A])
Equity(5061 [MSFT])
Equity(26578 [GOOG_L])
Equity(46631 [GOOG])

And I get "factor_1" (pe ratio) and "factor_2" (debt/equity ratio) as expected. Very happy!

But now I will activate the codes in the above box. run again. This is when things gets unpredictable. Not only do I lose the securities.

I get whole new sets of securities.....

Equity(1091 [BRK_A])
Equity(16841 [AMZN])
Equity(25729 [CTRP])
Equity(26401 [CRM])
Equity(44747 [DATA])

"factor_1" and "factor_2" columns are filled with wierd numbers and I have no clue what "f1_rank" is for anymore.

Please help me understand what "rank" is doing. What I was anticipating was with the same 5 stocks and original "factor_1" (pe ratio) and "factor_2" (debt/equity ratio) respectively, plus "f1_rank" to display the rankings of "factor_1". So why is it not happening? What should I do to get what I want to do. Thank you

3 responses

Hi Ujae,
I think you are running into issues that this thread addresses. First, the example algo has some filtering happening on lines 103 and 104, this should be done in the pipeline. In the linked thread, I shared a new example with this modification.

Second, there is currently a bug when you are using more than one fundamental field in a custom factor. This should be resolved later today. I'll let you know when it is.

For some help on the rank vs set_screen question, I gave a detailed description of both here that might be useful.

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.

The bug I mentioned above has been resolved. Let me know if you have future questions.

Thank you for your response. It appears to be working now.