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

Clone Algorithm
10
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
"""
This example comes from a request in the forums. 
The post can be found here: https://www.quantopian.com/posts/ranking-system-based-on-trading-volume-slash-shares-outstanding

The request was: 

I am stuck trying to build a stock ranking system with two signals:
1. Trading Volume/Shares Outstanding.
2. Price of current day / Price of 60 days ago.
Then rank Russell 2000 stocks every month, long the top 5%, short the bottom 5%.

"""

from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar


# Create custom factor #1 Trading Volume/Shares Outstanding
class Factor1(CustomFactor):   
    
    # Pre-declare inputs and window_length
    inputs = [morningstar.valuation_ratios.pe_ratio] 
    window_length = 1
    
    # Compute factor1 value
    def compute(self, today, assets, out, pe_ratio):       
        out[:] = pe_ratio[-1]

# Create custom factor #2 Price of current day / Price of 60 days ago.        
class Factor2(CustomFactor):   
    
    # Pre-declare inputs and window_length
    inputs = [morningstar.balance_sheet.total_liabilities,
              morningstar.balance_sheet.stockholders_equity] 
    
    window_length = 1
    
    # Compute factor2 value
    def compute(self, today, assets, out, debt, equity):  
    
        out[:] = debt[-1] /equity[-1]
        
# Create custom factor to calculate a market cap based on yesterday's close
# We'll use this to get the top 2000 stocks by market cap
class MarketCap(CustomFactor):   
    
    # Pre-declare inputs and window_length
    inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding] 
    window_length = 1
    
    # Compute market cap value
    def compute(self, today, assets, out, close, shares):       
        out[:] = close[-1] * shares[-1]

def initialize(context):
    
    context.long_leverage = 0.50
    context.short_leverage = -0.50
    
    pipe = Pipeline()
    attach_pipeline(pipe, 'ranked_2000')
    
    #add the two factors defined to the pipeline
    factor1 = Factor1()
    pipe.add(factor1, 'factor_1') 
    factor2 = Factor2()
    pipe.add(factor2, 'factor_2')
    
    # Create and apply a filter representing the top 2000 equities by MarketCap every day
    # This is an approximation of the Russell 2000
    mkt_cap = MarketCap()
    top_2000 = mkt_cap.top(5) #2000
    pipe.set_screen(top_2000)
    
    # 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')      
            
    # Scedule my rebalance function
    schedule_function(func=rebalance, 
                      date_rule=date_rules.month_start(days_offset=0), 
                      time_rule=time_rules.market_open(hours=0,minutes=30), 
                      half_days=True)
    
            
def before_trading_start(context, data):
    # Call pipelive_output to get the output
    # Note this is a dataframe where the index is the SIDs for all securities to pass my screen
    # and the colums are the factors which I added to the pipeline
    context.output = pipeline_output('ranked_2000')
    #there are some NaNs in factor 2, I'm removing those
    ranked_2000 = context.output.fillna(0)
    ranked_2000 = context.output[context.output.factor_2 > 0]
      
    # Narrow down the securities to only the top 500 & update my universe
    context.long_list = ranked_2000.sort(['combo_rank'], ascending=False).iloc[:100]
    context.short_list = ranked_2000.sort(['combo_rank'], ascending=False).iloc[-100:]   
    
    update_universe(context.long_list.index.union(context.short_list.index)) 


def handle_data(context, data):  
    
     # Record and plot the leverage of our portfolio over time. 
    record(leverage = context.account.leverage)
    
    print "Long List"
    log.info("\n" + str(context.long_list.sort(['combo_rank'], ascending=True).head(10)))
    
    print "Short List" 
    log.info("\n" + str(context.short_list.sort(['combo_rank'], ascending=False).head(10)))

# This rebalancing is called according to our schedule_function settings.     
def rebalance(context,data):
    
    long_weight = context.long_leverage / float(len(context.long_list))
    short_weight = context.short_leverage / float(len(context.short_list))

    
    for long_stock in context.long_list.index:
        if long_stock in data:
            log.info("ordering longs")
            log.info("weight is %s" % (long_weight))
            order_target_percent(long_stock, long_weight)
        
    for short_stock in context.short_list.index:
        if short_stock in data:
            log.info("ordering shorts")
            log.info("weight is %s" % (short_weight))
            order_target_percent(short_stock, short_weight)
        
    for stock in context.portfolio.positions.iterkeys():
        if stock not in context.long_list.index and stock not in context.short_list.index:
            order_target(stock, 0)
There was a runtime error.
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.