Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Calculating weighted market cap of a stock

Hi everyone!

I'm currently working on an economic hypothesis in regards to FANG (Facebook, Amazon, NFLX, Google) and one crucial number I need for each stock is 'weighted market cap' which is basically a market cap of a certain stock divided by the sum of all market caps of FANG.
So far my Pipeline has 2 columns (market cap of stock, returns of stock) I would like to add the third one for 'weighted market cap'. Any ideas on how to implement this since I've been struggling to find a good way also to isolate and get a market cap of an individual stock (all filters are usually more on a group of stocks).

#Function to calculate weigted market cap

def weigthed_mcap(stockSymbol, symbol_list):  
    market_cap_of_one_stock = Fundamentals.market_cap.latest and StaticAssets(symbols([stockSymbol]))  
    sum_of_all_market_caps = 0  
    for x in symbol_list:  
        sum_of_all_market_caps += Fundamentals.market_cap.latest and StaticAssets(symbols([x]))  
    return (market_cap_of_one_stock / sum_of_all_market_caps)

This is sample function I tried to create. This function gets passed 2 parameters. 'stockSymbol' is just string such as "FB" for example which tells us for what stock we need a weighted market cap for. 'symbol_list' is a list of strings such as ["FB","AMZN","NFLX","GOOG"] and we will iterate each of them to sum their market caps.

Error I get when doing this comes from the fact I mix StaticAssets and 'int', so is there any better way to get market cap of individual stock something on the lines of
Fundamentals.market_cap.(symbols("FB"))

Here is my pipeline:

def make_pipeline():  
    universe = QTradableStocksUS()  
    #Filter for FANG stocks  
    fang = StaticAssets(symbols(['FB', 'AMZN', 'NFLX', 'GOOG']))  
    fang_symbols = ['FB','AMZN','NFLX','GOOG']  
    #Function I'm trying to implement  
    fb_weighted = weigthed_mcap('FB', fang_symbols)  
    returns = Returns(window_length=30)  
    market_cap= morningstar.valuation.market_cap.latest  
    return Pipeline(  
        columns={  
            'returns':returns,  
            'market cap':market_cap, 

        },  
        screen = fang  
    )  

Thank you very much, any help would be greatly appreciated!

1 response

Anton,
Try this:

# Market Cap Weighted FANG portfolio 

from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline import Pipeline  
import quantopian.pipeline.filters as Filters  
import quantopian.pipeline.factors as Factors  
import numpy as np  
# -----------------------------------------------  
MY_STOCKS = symbols('FB', 'AMZN', 'NFLX', 'GOOG_L')  
# ----------------------------------------------  
def initialize(context):  
    schedule_function(trade, date_rules.month_start(), time_rules.market_close(minutes = 45))  
    stk_set = Filters.StaticAssets(MY_STOCKS)  
    mkt_cap = Factors.MarketCap(mask = stk_set)  
    pipe  = Pipeline(columns = {'mkt_cap': mkt_cap,}, screen = stk_set)  
    attach_pipeline(pipe, 'my_data')    

def trade(context, data):  
    secs = pipeline_output('my_data').index  
    market_cap = pipeline_output('my_data').mkt_cap  
    wt = np.array(market_cap)/sum(np.array(market_cap))

    for i in range(len(secs)):  
        order_target_percent(secs[i], wt[i])  
        record(**{secs[i].symbol + '_wt': wt[i]})

    record(leverage = context.account.leverage)