Repository navigation
Expand file tree
/
Copy pathfetch_core_data.py
More file actions
141 lines (113 loc) · 6.07 KB
/
Copy pathfetch_core_data.py
File metadata and controls
141 lines (113 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""BLOOMBERG TERMINAL REQUIRED: runnable examples for the core data fetchers.
Run on the Bloomberg machine. Pick a case and run: each calls one fetcher with
representative arguments and prints the result. One case per fetcher, gated behind the
Locals enum and the run_local dispatcher. Option chain fetching has its own
example in fetch_option_chain.py.
"""
# packages
from enum import Enum
import pandas as pd
# bbg
import bbg_fetch as bbg
class Locals(Enum):
"""Available core-fetcher example workflows."""
FIELD_TIMESERIES_PER_TICKERS = 1 # fetch_field_timeseries_per_tickers
FIELDS_TIMESERIES_PER_TICKER = 2 # fetch_fields_timeseries_per_ticker
FUNDAMENTALS = 3 # fetch_fundamentals
ACTIVE_FUTURES = 4 # fetch_active_futures
CONTRACT_TABLE = 5 # fetch_futures_contract_table
IMPLIED_VOL_TIME_SERIES = 6 # fetch_vol_timeseries
LAST_PRICES = 7 # fetch_last_prices
BOND_INFO = 8 # fetch_bonds_info
CDS_INFO = 9 # fetch_cds_info
BALANCE_DATA = 10 # fetch_balance_data
TICKERS_FROM_ISIN = 11 # fetch_tickers_from_isins
DIVIDEND_HISTORY = 12 # fetch_dividend_history
DIV_YIELDS = 13 # fetch_div_yields
INDEX_MEMBERS = 14 # fetch_index_members_weights
ISSUER_ISINS_FROM_BONDS = 15 # fetch_issuer_isins_from_bond_isins
TICKER_HELPERS = 16 # instrument_to_active_ticker, contract_to_instrument
VOL_SURFACE = 17 # fetch_vol_surface
def run_local(local: Locals) -> None:
"""Dispatch one fetcher example and print its result."""
pd.set_option('display.max_columns', 500)
if local == Locals.FIELD_TIMESERIES_PER_TICKERS:
# one field across many tickers, dict keys queried and values used as column labels
df = bbg.fetch_field_timeseries_per_tickers(tickers={'SPX Index': 'SPX', 'NKY Index': 'NKY'},
field='PX_LAST',
start_date=pd.Timestamp('01Jan2020'))
print(df)
elif local == Locals.FIELDS_TIMESERIES_PER_TICKER:
# many fields for one ticker
df = bbg.fetch_fields_timeseries_per_ticker(ticker='ES1 Index',
fields=('PX_OPEN', 'PX_HIGH', 'PX_LOW', 'PX_LAST'))
print(df)
elif local == Locals.FUNDAMENTALS:
# reference (BDP) fundamentals, dict-mapped column labels
df = bbg.fetch_fundamentals(tickers=['AAPL US Equity', 'MSFT US Equity'],
fields={'security_name': 'Name', 'gics_sector_name': 'Sector'})
print(df)
elif local == Locals.ACTIVE_FUTURES:
# front and second generic contracts (two series)
front, second = bbg.fetch_active_futures(generic_ticker='ES1 Index')
print(front.tail())
print(second.tail())
elif local == Locals.CONTRACT_TABLE:
# the futures contract chain with carry, generic number and timestamp
df = bbg.fetch_futures_contract_table(ticker='ES1 Index')
print(df)
elif local == Locals.IMPLIED_VOL_TIME_SERIES:
# delta-quoted implied vol history plus the underlying
df = bbg.fetch_vol_timeseries(ticker='SPX Index', vol_fields=bbg.IMPVOL_FIELDS_DELTA)
print(df.tail())
elif local == Locals.LAST_PRICES:
# last prices; default tickers are the FX_DICT crosses
prices = bbg.fetch_last_prices()
print(prices)
elif local == Locals.BOND_INFO:
# reference data for bonds by ISIN
df = bbg.fetch_bonds_info(isins=['US03522AAJ97', 'US126650CZ11'])
print(df)
elif local == Locals.CDS_INFO:
# 5y CDS spread tickers for equity issuers
df = bbg.fetch_cds_info(equity_tickers=['ABI BB Equity', 'CVS US Equity'])
print(df)
elif local == Locals.BALANCE_DATA:
# balance-sheet and credit fundamentals
df = bbg.fetch_balance_data(tickers=['ABI BB Equity', 'T US Equity', 'JPM US Equity'])
print(df)
elif local == Locals.TICKERS_FROM_ISIN:
# resolve ISINs to primary-exchange composite tickers
tickers = bbg.fetch_tickers_from_isins(isins=['US88160R1014', 'IL0065100930'])
print(tickers)
elif local == Locals.DIVIDEND_HISTORY:
# per-event dividend history
df = bbg.fetch_dividend_history(ticker='TIP US Equity')
print(df)
elif local == Locals.DIV_YIELDS:
# per-event amounts, trailing-12m sums, and trailing-12m yields (three frames)
divs, divs_1y, divs_yield = bbg.fetch_div_yields(tickers={'SPY US Equity': 'SPY',
'TIP US Equity': 'TIP'})
print(divs_yield.tail())
elif local == Locals.INDEX_MEMBERS:
# index constituents; INDX_MEMBERS returns tickers without weights
members = bbg.fetch_index_members_weights(index='SPX Index', field='INDX_MEMBERS')
print(members)
elif local == Locals.ISSUER_ISINS_FROM_BONDS:
# bond ISIN -> issuer equity ISIN
issuer = bbg.fetch_issuer_isins_from_bond_isins(bond_isins=['XS3034073836', 'USY0616GAA14'])
print(issuer)
elif local == Locals.TICKER_HELPERS:
# string helpers, no Bloomberg call
print(bbg.instrument_to_active_ticker(instrument='ES1 Index', num=2)) # -> 'ES2 Index'
print(bbg.contract_to_instrument(future='ES1 Index')) # -> 'ES'
elif local == Locals.VOL_SURFACE:
# implied vol surface for one date: tenor rows x moneyness columns, in percent.
# non_null_share flags whether the 80 and 120 columns actually populate.
surface = bbg.fetch_vol_surface(ticker='SPX Index', scaler=None) # swap to 'KOSPI2 Index' as needed
print(surface.to_string())
print(surface.notna().mean().rename('non_null_share').to_string())
else:
raise NotImplementedError(f"unsupported local: {local}")
if __name__ == '__main__':
run_local(local=Locals.LAST_PRICES)