It is fairly common in Research to want to run analyses on well known indices. Here are code snippets that try to approximate the S&P 500, Russell 1000, and Russell 3000 Indices. If you want to know more about each index's construction check out the links below.
import datetime
from sqlalchemy import or_
fundamentals = init_fundamentals()
sp_500 = get_fundamentals(
query(fundamentals.valuation.market_cap,
fundamentals.company_reference.primary_exchange_id)
.filter(fundamentals.valuation.market_cap > 4e9)
.filter(fundamentals.company_reference.country_id == "USA")
.filter(or_(fundamentals.company_reference.primary_exchange_id == "NAS", fundamentals.company_reference.primary_exchange_id == "NYS"))
.order_by(fundamentals.valuation.market_cap.desc())
.limit(500),
today) # S&P 500 rebalances on an as needed basis, so we'll just use today
import datetime
from sqlalchemy import or_
fundamentals = init_fundamentals()
russell_1000 = get_fundamentals(
query(fundamentals.valuation.market_cap,
fundamentals.company_reference.primary_exchange_id)
.filter(fundamentals.company_reference.country_id == "USA")
.filter(fundamentals.valuation.market_cap > 30e6)
.filter(or_(fundamentals.company_reference.primary_exchange_id == "NAS", fundamentals.company_reference.primary_exchange_id == "NYS"))
.order_by(fundamentals.valuation.market_cap.desc())
.limit(1000),
"2015-05-31") # Russell reconstitutes on the last day in May
import datetime
from sqlalchemy import or_
fundamentals = init_fundamentals()
russell_3000 = get_fundamentals(
query(fundamentals.valuation.market_cap,
fundamentals.company_reference.primary_exchange_id,
fundamentals.company_reference.country_id)
.filter(fundamentals.company_reference.country_id == "USA")
.filter(fundamentals.valuation.market_cap > 30e6)
.filter(or_(fundamentals.company_reference.primary_exchange_id == "NAS", fundamentals.company_reference.primary_exchange_id == "NYS"))
.order_by(fundamentals.valuation.market_cap.desc())
.limit(3000),
"2015-05-31") # Russell reconstitutes on the last day in May