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!