The asset universe is a collection of historical daily returns of user-specified stocks and alternetive assets. It downloads historical data from the following sources:
Install the IB API to your Python environment.
Open and log in to Interactive Brokers Trader Workstation with the API enabled:
- In Configuration, go to API > Settings
- Check "Enable ActiveX and Socket Clients"
- Check "Read-Only API"
- Set the "Socket port" to 7496
Install Asset Universe using pip
:
git clone URL
cd assetuniverse
python3 -m pip install .
Define your contracts (stocks and/or futures) as a list
of AssetUniverse.AssetUniverseContract
class:
from assetuniverse import AssetUniverseContract, AssetUniverse
contracts = []
contract = AssetUniverseContract(
secType = 'FUT',
currency = 'USD',
exchange = 'GLOBEX',
localSymbol = 'ESU1', # "Local Name" on the IB details page (link below)
data_source = 'TWS'
)
contracts.append(contract)
contract = AssetUniverseContract(
secType='FUT',
currency='USD',
exchange='ECBOT',
localSymbol='ZB SEP 21',
data_source='TWS'
)
contracts.append(contract)
contract = AssetUniverseContract(
symbol='SPY',
secType='STK',
currency='USD',
exchange='SMART',
data_source='TWS'
)
contracts.append(contract)
contract = AssetUniverseContract(
symbol='AAPL',
secType='STK',
currency='USD',
exchange='SMART',
data_source='TWS'
)
contracts.append(contract)
contract = AssetUniverseContract(
symbol='SBUX',
secType='STK',
currency='USD',
exchange='SMART',
data_source='Yahoo Finance' # Alternate data source is Yahoo Finance
)
contracts.append(contract)
# Instantiate asset universe - will start the download
days = 365
end = datetime.date.today()
start = end - datetime.timedelta(days=days)
AU = AssetUniverse(start, end, contracts, offline=False)
# Plot and calculate correlations
AU.plotprices()
print(AU.correlation_matrix())
print(AU.correlation_matrix(['SPY', 'ESU1']))
Here is an example of the details for the ES futures contract.