quantlab.universe.SP500MembershipFetcher#

class quantlab.universe.SP500MembershipFetcher(cache_dir)[source]#

Bases: IndexMembershipFetcher

Point-in-time S&P 500 membership from a GitHub CSV anchor and Wikipedia.

The anchor is the datasets/s-and-p-500-companies CSV, which carries a Date added column. PIT_COVERAGE_START is the earliest row of the Wikipedia change table (1976-07-01), not the earlier coverage the page’s prose claims; queries before it are refused.

Examples

>>> fetcher = SP500MembershipFetcher(cache_dir="data/reference/_cache")
>>> intervals = fetcher.build_intervals()  # downloads anchor and change log
>>> intervals.columns
['symbol', 'start_date', 'end_date', 'end_date_is_inferred']
CATEGORY = 'sp500_constituent'#

The catalog category this index is stored under. Typed as the literal so an unknown token is a type error rather than a runtime surprise.

CHANGES_TABLE_ATTRS = {'id': 'changes'}#

Optional pd.read_html(attrs=...) pre-filter for a page that marks its change-log table with an id or class.

DATE_HEADER = 'Effective Date'#

The EXPECTED_SOURCE_HEADER entry carrying the effective date.

EXPECTED_SOURCE_HEADER = ('Effective Date', 'Added Ticker', 'Added Security', 'Removed Ticker', 'Removed Security', 'Reason', 'Refs')#

Exact flattened header (see _flatten_header) of the change-log table. It both selects the table and names the columns read from it.

fetch_anchor()[source]#

Download the constituents CSV and return [symbol, date_added].

Examples

>>> fetcher = SP500MembershipFetcher(cache_dir="/tmp/cache")
>>> anchor = fetcher.fetch_anchor()  # downloads from GitHub
>>> anchor.columns
['symbol', 'date_added']