Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Replicate SP500

I would like to replicate the S&P500 manually. I have a lot of python experience but I can't figure out how to get the total market cap of the top 500 companies to calculate the weights.

1 response

The builtin Q500US filter may or may not be a good starting point to replicate the SP500. However, getting the total market cap is as simple as using the sum method on the 'market_cap' column. See below and the attached algorithm.

    context.output = pipeline_output('my_pipe')  
    mkt_cap_sum = context.output['market_cap'].sum()