Example of using the v3api directly

This notebook gives examples of using the API directly.


In [1]:
from tia.bbg import LocalTerminal
import pandas as pd

Reference Data Requests


In [2]:
# Single SID, Multiple Valid Fields
resp = LocalTerminal.get_reference_data('MSFT US EQUITY', ['PX_LAST', 'GICS_SECTOR_NAME', 'VOLATILITY_30D'])
resp.as_frame()


Out[2]:
PX_LAST GICS_SECTOR_NAME VOLATILITY_30D
MSFT US EQUITY 44.09 Information Technology 38.422119

In [3]:
# Get the response as a dict
resp.as_map()


Out[3]:
defaultdict(<type 'dict'>, {'MSFT US EQUITY': {'VOLATILITY_30D': 38.42211904056546, 'GICS_SECTOR_NAME': 'Information Technology', 'PX_LAST': 44.09}})

In [4]:
# Single SID, Invalid Fields
# Ability to ignore errors
resp = LocalTerminal.get_reference_data('MSFT US EQUITY', ['PX_LAST', 'GICS_SECTOR_NAME', 'BAD FIELD'], ignore_field_error=1)
resp.as_frame()


Out[4]:
PX_LAST GICS_SECTOR_NAME BAD FIELD
MSFT US EQUITY 44.09 Information Technology NaN

In [5]:
# Multiple SID, Invalid Fields
# allows for non-homogeneous security types to be batched together
resp = LocalTerminal.get_reference_data(['ED1 COMDTY', 'MSFT US EQUITY'], ['PX_LAST', 'GICS_SECTOR_NAME'], ignore_field_error=1)
resp.as_frame()


Out[5]:
PX_LAST GICS_SECTOR_NAME
ED1 COMDTY 99.7325 NaN
MSFT US EQUITY 44.0900 Information Technology

In [6]:
# Retrieve data without override
LocalTerminal.get_reference_data('SPX INDEX', 'CUST_TRR_RETURN_HOLDING_PER').as_frame()


Out[6]:
CUST_TRR_RETURN_HOLDING_PER
SPX INDEX 17.00294

In [7]:
# Retrieve data with override (1 month total return)
dt = pd.datetools.BDay(-21).apply(pd.datetime.now()).strftime('%Y%m%d')
LocalTerminal.get_reference_data('SPX INDEX', 'CUST_TRR_RETURN_HOLDING_PER', CUST_TRR_START_DT=dt).as_frame()


Out[7]:
CUST_TRR_RETURN_HOLDING_PER
SPX INDEX 4.471417

HISTORICAL DATA


In [8]:
# Single SID, Multiple Valid Fields
resp = LocalTerminal.get_historical('MSFT US EQUITY', ['PX_OPEN', 'PX_LAST'], start='1/1/2014', end='3/1/2014')
resp.as_frame().head()


Out[8]:
MSFT US EQUITY
PX_OPEN PX_LAST
date
2014-01-02 37.350 37.16
2014-01-03 37.200 36.91
2014-01-06 36.850 36.13
2014-01-07 36.325 36.41
2014-01-08 36.000 35.76

In [9]:
# Multiple SIDs, Multiple Valid Fields
resp = LocalTerminal.get_historical(['IBM US EQUITY', 'MSFT US EQUITY'], ['PX_OPEN', 'PX_LAST'], start='1/1/2014', end='3/1/2014')
resp.as_frame().head()


Out[9]:
IBM US EQUITY MSFT US EQUITY
PX_OPEN PX_LAST PX_OPEN PX_LAST
date
2014-01-02 187.21 185.53 37.350 37.16
2014-01-03 185.83 186.64 37.200 36.91
2014-01-06 187.15 186.00 36.850 36.13
2014-01-07 186.39 189.71 36.325 36.41
2014-01-08 189.33 187.97 36.000 35.76

In [10]:
# Weekly data
resp = LocalTerminal.get_historical(['IBM US EQUITY', 'MSFT US EQUITY'], ['PX_OPEN', 'PX_LAST'], 
                                         start='1/1/2014', end='3/1/2014', period='WEEKLY')
resp.as_frame().head()


Out[10]:
IBM US EQUITY MSFT US EQUITY
PX_OPEN PX_LAST PX_OPEN PX_LAST
date
2014-01-03 185.320 186.64 37.22 36.910
2014-01-10 187.150 187.26 36.85 36.040
2014-01-17 186.260 190.09 35.99 36.380
2014-01-24 190.230 179.64 36.82 36.805
2014-01-31 179.605 176.68 36.87 37.840

In [11]:
# format response as panel
resp.as_panel()


Out[11]:
<class 'pandas.core.panel.Panel'>
Dimensions: 2 (items) x 9 (major_axis) x 2 (minor_axis)
Items axis: IBM US EQUITY to MSFT US EQUITY
Major_axis axis: 2014-01-03 00:00:00 to 2014-02-28 00:00:00
Minor_axis axis: PX_OPEN to PX_LAST

RETRIEVING CURVES AND MEMBERS


In [12]:
# Retrieve the EURUSD Forward Curve
resp = LocalTerminal.get_reference_data('eurusd curncy', 'fwd_curve')
# must retrieve a frame from the first row
resp.as_frame().ix[0, 'fwd_curve'].head()


Out[12]:
Security Description Settlement Date Bid Mid Ask
0 EURON Curncy 2015-02-26 0.084 0.1085 0.133
1 EURTN Curncy 2015-02-27 0.094 0.1120 0.130
2 EURSN Curncy 2015-03-02 0.231 0.2600 0.289
3 EUR1W Curncy 2015-03-06 0.680 0.7400 0.800
4 EUR1M Curncy 2015-03-31 3.600 3.7500 3.900

In [13]:
# OR 
resp.as_map()['eurusd curncy']['fwd_curve'].head()


Out[13]:
Security Description Settlement Date Bid Mid Ask
0 EURON Curncy 2015-02-26 0.084 0.1085 0.133
1 EURTN Curncy 2015-02-27 0.094 0.1120 0.130
2 EURSN Curncy 2015-03-02 0.231 0.2600 0.289
3 EUR1W Curncy 2015-03-06 0.680 0.7400 0.800
4 EUR1M Curncy 2015-03-31 3.600 3.7500 3.900

In [14]:
# Retrive the EURUSD Vol Surface
resp = LocalTerminal.get_reference_data('eurusd curncy', 'dflt_vol_surf_bid')
resp.as_frame().ix[0, 'dflt_vol_surf_bid'].head()


Out[14]:
Maturity Period Maturity as Percent of Year Delta Type Delta Volatility
0 1D 0.0037 ATM 0.50 9.5650
1 1D 0.0037 35D_CALL 0.35 9.3898
2 1D 0.0037 35D_PUT 0.65 9.6700
3 1D 0.0037 25D_CALL 0.25 9.2089
4 1D 0.0037 25D_PUT 0.75 9.7224

In [15]:
# More complex example
# Retrive all members of the S&P 500, then get price and vol data
resp = LocalTerminal.get_reference_data('spx index', 'indx_members')
members = resp.as_frame().ix[0, 'indx_members']
# append region + yellow key = 'US EQUITY'
members = members.icol(0).apply(lambda x: x.split()[0] + ' US EQUITY')
resp = LocalTerminal.get_reference_data(members, ['PX_LAST', 'VOLATILITY_30D'])
resp.as_frame().head()


Out[15]:
PX_LAST VOLATILITY_30D
A US EQUITY 42.06 26.464951
AA US EQUITY 15.74 38.774343
AAPL US EQUITY 132.17 29.148085
ABBV US EQUITY 60.87 38.109767
ABC US EQUITY 102.97 12.183109

In [16]:
# kind of pointless
resp.as_frame().describe()


Out[16]:
PX_LAST VOLATILITY_30D
count 502.000000 502.000000
mean 86.554861 26.793370
std 92.274786 10.935133
min 7.860000 2.057758
25% 43.022500 19.967708
50% 67.220000 24.565563
75% 97.326250 30.091746
max 1219.790000 92.775252

In [17]:
pxs = LocalTerminal.get_historical(members, 'PX_LAST')
f = pxs.as_frame()
f.columns = f.columns.get_level_values(0)
# Show first 5 rows for last 5 days
f.iloc[:5, -5:]


Out[17]:
VZ US EQUITY UNP US EQUITY XL US EQUITY CBS US EQUITY MU US EQUITY
date
2014-02-25 46.29 89.350 29.57 65.76 24.255
2014-02-26 46.35 88.605 29.82 65.54 24.220
2014-02-27 47.50 89.250 29.95 66.40 24.190
2014-02-28 47.58 90.190 30.40 67.08 24.190
2014-03-03 47.31 89.755 30.11 65.95 24.480

Intraday Tick Request


In [20]:
import datetime
sid = 'VOD LN EQUITY'
events = ['TRADE', 'AT_TRADE']
dt = pd.datetools.BDay(-1).apply(pd.datetime.now())
start = pd.datetime.combine(dt, datetime.time(13, 30))
end = pd.datetime.combine(dt, datetime.time(13, 35))
f = LocalTerminal.get_intraday_tick(sid, events, start, end, include_condition_codes=True).as_frame()
f.head()


Out[20]:
conditionCodes size time type value
0 AT 858 2015-02-24 13:30:02 AT_TRADE 226.85
1 AT 2000 2015-02-24 13:30:02 AT_TRADE 226.85
2 AT 2566 2015-02-24 13:30:02 AT_TRADE 226.85
3 AT 2562 2015-02-24 13:30:08 AT_TRADE 226.85
4 AT 2623 2015-02-24 13:30:08 AT_TRADE 226.85

Intraday Bar Request


In [21]:
import datetime
sid = 'IBM US EQUITY'
event = 'TRADE'
dt = pd.datetools.BDay(-1).apply(pd.datetime.now())
start = pd.datetime.combine(dt, datetime.time(13, 30))
end = pd.datetime.combine(dt, datetime.time(21, 30))
f = LocalTerminal.get_intraday_bar(sid, event, start, end, interval=60).as_frame()
f.head()


Out[21]:
close high low numEvents open time value volume
0 162.2500 162.70 161.51 4005 162.4900 2015-02-24 14:30:00 110345672 680888
1 163.3808 163.46 162.00 2921 162.2699 2015-02-24 15:30:00 66801220 410523
2 163.5100 163.74 163.05 2587 163.4000 2015-02-24 16:30:00 60114624 367710
3 163.8700 163.94 163.23 1850 163.5200 2015-02-24 17:30:00 47967724 293200
4 164.3600 164.49 163.87 2910 163.8836 2015-02-24 18:30:00 70343296 428363

In [ ]: