ETFs#

Retrieve data for exchange-traded fund (ETF) instruments.

Initialize SigTech

Import Python libraries:

import datetime as dtm
import numpy as np
import pandas as pd

import sigtech.framework as sig

Initialize the SigTech framework environment:

env = sig.init()

List available ETF instruments#

ETF instruments are instances of the sig.ExchangeTradedFund class.

To list ETF object names, call the sig.ExchangeTradedFund.get_names class method:

Input:

etf_names = sig.ExchangeTradedFund.get_names()
etf_names[:5]

Output:

['AGG UP EQUITY',
 'BIL UP EQUITY',
 'BND UP EQUITY',
 'BNDX UP EQUITY',
 'DBC UP EQUITY']

Retrieve an ETF instrument#

To retrieve an ETF instrument, pass its object name to sig.obj.get:

Input:

agg = sig.obj.get('AGG UP EQUITY')
agg

Output:

AGG UP EQUITY <class 'sigtech.framework.instruments.equities.ExchangeTradedFund'>[139953121183952]

Retrieve a dictionary of static data describing an ETF instrument:

Input:

agg.data_dict()

Output:

{'_product_type': 'Exchange Traded Fund',
 'instrument_id': None,
 '_data_source_all': [],
 '_available_data_points': ['EOD'],
 '_default_data_point': 'EOD',
 'price_factor': 1.0,
...}

Retrieve the instrument group for an ETF:

Input:

group = agg.group()
group

Output:

NYSEARC(T) EQUITY INSTRUMENT GROUP <class 'sigtech.framework.instruments.equities.EquityGroup'>[140328852910736]

List the names of instruments in NYSEARC(T) EQUITY INSTRUMENT GROUP:

Input:

group.query_instrument_names()[:5]

Output:

['AGG UP EQUITY',
 'BIL UP EQUITY',
 'BND UP EQUITY',
 'BNDX UP EQUITY',
 'DBC UP EQUITY']

Retrieve the exchange on which an ETF is traded:

Input:

exchange = sig.obj.get(agg.exchange().name)
exchange

Output:

NYSEARC(T) EXCHANGE GROUP <class 'sigtech.framework.instruments.base.Exchange'>[139953149826448]

Retrieve an ETF instrument’s exchange holiday calendar#

Retrieve the object name of the exchange’s market holiday calendar:

Input:

exchange.holidays

Output:

'NYSEARC(T) CALENDAR'

Retrieve the corresponding holiday calendar object, an instance of sig.HolidayCalendar:

Input:

calendar = sig.obj.get(exchange.holidays)
calendar

Output:

NYSEARC(T) CALENDAR <class 'sigtech.framework.infra.calendar.calendar.HolidayCalendar'>[139915073725392]

View the dates in the calendar:

Input:

calendar.holidays[:5]

Output:

[datetime.date(1973, 1, 1),
 datetime.date(1973, 1, 25),
 datetime.date(1973, 1, 25),
 datetime.date(1973, 2, 19),
 datetime.date(1973, 4, 20)]

Retrieve an ETF instrument’s time series data#

Retrieve an ETF instrument’s time series data:

Python:

agg.history().tail()

Output:

2024-07-15    98.05
2024-07-16    98.45
2024-07-17    98.52
2024-07-18    98.25
2024-07-19    98.05
Name: (LastPrice, EOD, AGG UP EQUITY), dtype: float64

List available time series data fields for an ETF instrument:

Input:

agg.history_fields

Output:

['OPEN', 'HIGH', 'LOW', 'LastPrice', 'Volume']

Retrieve time series data for a specified field:

Input:

field_name = "HIGH"
agg.history(field_name).tail()

Output:

2024-07-15    98.28
2024-07-16    98.45
2024-07-17    98.57
2024-07-18    98.56
2024-07-19    98.10
Name: (HIGH, EOD, AGG UP EQUITY), dtype: float64

Create a pandas DataFrame from specified time series data fields, and plot the data:

df_agg = pd.concat({
    'High': agg.history('HIGH').loc['2023':],
    'Low': agg.history('LOW').loc['2023':]
}, axis=1)
df_agg.plot(title='AGG UP Equity High / Low');

Create a bar chart showing an ETF’s volume data over time:

volume_agg = agg.history('Volume').loc['2024-05':]
volume_agg.index = [x.date() for x in volume_agg.index]
volume_agg.plot(title='AGG UP EQUITY Volume', kind='bar');

Compare multiple ETFs using pandas#

SigTech time series data is compatible with NumPy and pandas data structures and methods.

Use pandas to compare multiple ETFs:

Input:

ptf_etfs = ['AGG UP EQUITY', 'SPY UP EQUITY', 'VEA UP EQUITY']
etf_histories = pd.concat({
    x: sig.obj.get(x).history() for x in ptf_etfs
}, axis=1).dropna()
etf_histories.columns = [x.split('_')[0] for x in etf_histories.columns]
print(etf_histories.tail())

Output:

            AGG UP EQUITY  SPY UP EQUITY  VEA UP EQUITY
2024-07-15          98.05         561.53          51.11
2024-07-16          98.45         564.86          51.41
2024-07-17          98.52         556.94          51.12
2024-07-18          98.25         552.66          50.62
2024-07-19          98.05         548.99          50.32

Display a plot of ETF performance over time:

(etf_histories / etf_histories.iloc[0]).plot(title='ETF Performance');