Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Need Help Building a Pipeline of the Top 10 Weighted Stocks in the S&P500

As the title suggests, I'm looking for a way to extract the top 10 holdings in the S&P500 (possibly from this url: http://portfolios.morningstar.com/fund/holdings?t=SPY). The algorithm will ideally rebalance at the start of each month and place an equal weight on each of the top 10 holdings. Thank you for any and all help.

2 responses

Have you done something to get the top 10 holdings? I was planning to do the same with the bottom 20, and rebalancing every 12 months.

The S&P 500 index is weighted by float-adjusted market capitalization.
So if you take top 10 by market capitalization from Q500US they should in most cases be in top 10 holdings in S&P 500 index.

Try something like this:

from quantopian.pipeline import Pipeline, factors, filters, classifiers  
from quantopian.algorithm import attach_pipeline, pipeline_output  
from quantopian.pipeline.data import Fundamentals

def initialize(context):  
    schedule_function(trade, date_rules.every_day(), time_rules.market_open(minutes = 65))  
    market_cap = Fundamentals.market_cap.latest  
    top_market_cap = market_cap.top(10, mask = filters.Q500US())  
    attach_pipeline(Pipeline(columns = {'market_cap': market_cap}, screen = top_market_cap), 'stock_set')

def trade(context, data):  
    output = pipeline_output('stock_set')  
    stocks = output.sort_values('market_cap', ascending = False).head(10).index  
    print stocks  

and compare to morningstar data