QuantConnect
US ETF Constituents
Introduction
The US ETF Constituents dataset by QuantConnect tracks the constituents and weighting of US Equities in 2,650 ETF listings. The data starts in June 2009 and is delivered on a daily basis (monthly basis before January 2015). This dataset is created by tracking the host ETF websites and can be delayed by up to 1 week.
This dataset depends on the US Equity Security Master dataset because the US Equity Security Master dataset contains information on splits, dividends, and symbol changes.
For more information about the US ETF Constituents dataset, including CLI commands and pricing, see the dataset listing.
About the Provider
QuantConnect was founded in 2012 to serve quants everywhere with the best possible algorithmic trading technology. Seeking to disrupt a notoriously closed-source industry, QuantConnect takes a radically open-source approach to algorithmic trading. Through the QuantConnect web platform, more than 160,000 quants are served every month.
Getting Started
The following snippet demonstrates how to request data from the US ETF Constituents dataset:
def initialize(self) -> None: self.universe_settings.asynchronous = True # Use the following method for a Classic Algorithm self._universe = self.add_universe(self.universe.etf("SPY", Market.USA, self.universe_settings, self.etf_constituents_filter)) symbol = Symbol.create("SPY", SecurityType.EQUITY, Market.USA) # Use the following method for a Framework Algorithm self.add_universe_selection(ETFConstituentsUniverseSelectionModel(symbol, self.universe_settings, self.etf_constituents_filter)) def etf_constituents_filter(self, constituents: List[ETFConstituentUniverse]) -> List[Symbol]: # Add all Symbols of the ETFConstituentUniverse return [x.symbol for x in constituents]
Example Applications
The ETF Constituents dataset provides an excellent source of tradable universes for strategies without selection bias. When you use an ETF universe, the original ETF can serve as an excellent benchmark for your strategy performance. Other use cases include the following:
- Creating an index-tracking algorithm for customized passive portfolio management
- Performing statistical arbitrage with the base ETF
For more example algorithms, see Examples.
Requesting Data
To add US ETF Constituents data to your algorithm, call the add_universe
and universe.etf
methods. To select which constituents occupy the universe, provide the ETF Symbol
and a selection function.
class ETFConstituentUniverseAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2018, 1, 1) self.set_end_date(2020, 8, 25) self.set_cash(100000) self.universe_settings.asynchronous = True self._universe = self.add_universe(self.universe.etf("SPY", self.universe_settings, self.etf_constituents_filter))
For more information about universe settings, see Settings.
Accessing Data
To access the US ETF Constituent data, use the ETFConstituentUniverse
objects in your selection function. The data is available in daily resolution. The Symbol
objects you return from your selection function defines the universe constituents.
def etf_constituents_filter(self, constituents: List[ETFConstituentUniverse]) -> List[Symbol]: for c in constituents: self.debug(f'{c.end_time} :: {c.last_update} :: {c.weight} :: {c.shares_held} :: {c.market_value}') return [x.symbol for x in constituents]
Historical Data
You can get historical universe data in an algorithm and in the Research Environment.
Historical Universe Data in Algorithms
To get historical universe data in an algorithm, call the history
method with the Universe
object and the lookback period. If there is no data in the period you request, the history result is empty.
# DataFrame example where the columns are the ETFConstituentUniverse attributes: df_history = self.history(self.universe, 30, Resolution.DAILY, flatten=True) # Series example where the values are lists of ETFConstituentUniverse objects: series_history = self.history(self.universe, 30, Resolution.DAILY) for (universe_symbol, time), constituents in series_history.items(): for constituent in constituents: self.log(f'{constituent.symbol} weight at {constituent.end_time}: {constituent.weight}')
Historical Universe Data in Research
To get historical universe data in research, call the universe_history
method with the Universe
object and the lookback period. The universe_history
returns the filtered universe. If there is no data in the period you request, the history result is empty.
# DataFrame example where the columns are the ETFConstituentUniverse attributes: df_history = qb.universe_history(universe, qb.time-timedelta(30), qb.time, flatten=True) # Series example where the values are lists of ETFConstituentUniverse objects: series_history = qb.universe_history(universe, qb.time-timedelta(30), qb.time) for (universe_symbol, time), constituents in series_history.items(): for constituent in constituents: print(f"{constituent.symbol} weight at {constituent.end_time}: {constituent.weight}")
You can call the history
method in Research.
Example Applications
The ETF Constituents dataset provides an excellent source of tradable universes for strategies without selection bias. When you use an ETF universe, the original ETF can serve as an excellent benchmark for your strategy performance. Other use cases include the following:
- Creating an index-tracking algorithm for customized passive portfolio management
- Performing statistical arbitrage with the base ETF
For more example algorithms, see Examples.