Create a gist now

Instantly share code, notes, and snippets.

how to access the Empirasign abs/mbs API using Python in a functional manner
# -*- coding: utf-8 -*-
#
# api_examples_func.py
"""this small library illustrates how to access the Empirasign abs/mbs
API using Python in a functional manner
"""
import hashlib #needed to compute request signatures
import datetime
import json
import urllib2
import urllib
#user-specific parameters
APP_ID = 'MY_APP_ID'
PW = 'MY_PASS'
PROXY_SERVER = '' # e.g. proxy.mycompany.net:8080
def get_bond(uid, d0=None, d1=None, fmt='json', bid_side=True,
prepend_price32=True,
api_url='http://www.empirasign.com/api/cusip/'):
"""get bwic and dealer runs data for a specific cusip/ISIN/BBG Ticker
d0 and d1 are not required, they indicated start and end (inclusive)
of date range to search, must be datetime.date type
fmt can be json or csv (if you ask us to support xml, we will think
less of you)
"""
#compute the request signature
dt = datetime.date.today().strftime("%Y%m%d")
sig_keys = [APP_ID, uid, dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'cusip': uid, 'app_id': APP_ID, 'req_sig': req_sig,
'bid_side': bid_side, 'quote': prepend_price32}
if d0 and d1:
url_params['d0'] = d0.strftime("%Y%m%d")
url_params['d1'] = d1.strftime("%Y%m%d")
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url += "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_bonds(uids, d0=None, d1=None, fmt='json',
api_url='http://www.empirasign.com/api/bonds/'):
"""get bwic and dealer runs data for a list of cusips/ISINs/BBG Tickers
the maxium length of uids processed by server is 200, items 201 and
higher will be ignored.
d0 and d1 are not required, they indicated start and end (inclusive)
of date range to search, must be datetime.date type
fmt can be json or csv
"""
#compute the request signature
dt = datetime.date.today().strftime("%Y%m%d") #YYYYMMDD for cur date
sig_keys = [APP_ID, ",".join(uids), dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
req_params = {'app_id': APP_ID, 'req_sig': req_sig, 'bonds': ",".join(uids)}
if d0 and d1:
req_params['d0'] = d0.strftime("%Y%m%d")
req_params['d1'] = d1.strftime("%Y%m%d")
if fmt.lower() != 'json':
req_params['csv'] = True
req_params = urllib.urlencode(req_params)
req = urllib2.Request(api_url, data=req_params)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
# deprecated (can only do one date)
def get_bwics_old(sector, dt, fmt='json',
api_url="http://www.empirasign.com/api/bwic/"):
"""get summary level data for BWICs in a given sector. This
query does not return actual market data so it does not count
against the daily quota
dt: the date you want the BWICs data for (can only query back 30 days)
sector: must be on of the following or server will return an error
agency, cmbs, nonag, abs, naresi, conabs, clo, euro, eu-clo, eu-resi,
eu-abs, eu-cmbs, agarm, agcmo, snr, agcmbs, am, aj, io, mezz, unk,
alta-15, alta-30, alta-a, prime-15, prime-30, prime-a, auto, card,
equip, heloc, heq, mh, other, student, subprime, utility
sector descriptions can be found here: www.empirasign.com/api-mbs/
fmt can be json or csv
"""
# compute the request signature
# the trick here is the dt for the request signature and dt for the
# query parameter are the one and the same
dt = dt.strftime("%Y%m%d")
sig_keys = [APP_ID, sector, dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'sector': sector, 'app_id': APP_ID, 'dt': dt, 'req_sig': req_sig}
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_bwics(sector, d0, d1=None, fmt='json',
api_url="http://localhost:8000/api/bwics/"):
"""get summary level data for BWICs in a given sector. This
query does not return actual market data so it does not count
against the daily quota
d0: start of date range (30 day lookback)
d1: end of date range
when d0 = d1, this returns same results as get_bwics_old()
sector: must be on of the following or server will return an error
agency, cmbs, nonag, abs, naresi, conabs, clo, euro, eu-clo, eu-resi,
eu-abs, eu-cmbs, agarm, agcmo, snr, agcmbs, am, aj, io, mezz, unk,
alta-15, alta-30, alta-a, prime-15, prime-30, prime-a, auto, card,
equip, heloc, heq, mh, other, student, subprime, utility
sector descriptions can be found here: www.empirasign.com/api-mbs/
fmt can be json or csv
"""
# compute the request signature
# the trick here is the dt for the request signature and dt for the
# query parameter are the one and the same
if d0 and d1:
d0 = d0.strftime("%Y%m%d")
d1 = d1.strftime("%Y%m%d")
sig_keys = [APP_ID, sector, d0, d1, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'sector': sector, 'app_id': APP_ID, 'd0': d0,
'd1': d1, 'req_sig': req_sig}
else:
# search on single date and pass dt as arg name
d0 = d0.strftime("%Y%m%d")
sig_keys = [APP_ID, sector, d0, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'sector': sector, 'app_id': APP_ID, 'dt': d0,
'req_sig': req_sig}
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_offers(dt, dealer=None, sector=None, fmt='json',
api_url="http://www.empirasign.com/api/offers/"):
"""Get all offers for a specific date, or for a dealer/dector/dt combo
only dt parameter is required as a datetime.date type
if only dt provided, a list of dealer and sector combos of dealers
runs available for that date will be returned. This particular quiery
as it returns no market data has no query cost.
if dt, sector and dealer are provided, then the result set will be that
particular dealer's run for a the given sector and date
fmt can be json or csv
"""
# compute the request signature
dt = dt.strftime("%Y%m%d")
sig_keys = [APP_ID, dt, PW]
if sector and dealer:
sig_keys = [APP_ID, dealer, sector, dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'app_id': APP_ID, 'dt': dt, 'req_sig': req_sig}
if sector and dealer:
url_params.update({'sector': sector, 'dealer': dealer})
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_events(n=15, fmt='json',
api_url="http://www.empirasign.com/api/mbsfeed/"):
"""get the latest db events (new bwic, new price talk, or
new trade color) from the news feed
this function has no required parameters
n is the n latest events
fmt can be json or csv
"""
# compute the request signature
dt = datetime.date.today().strftime("%Y%m%d")
sig_keys = [APP_ID, str(n), dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'app_id': APP_ID, 'n': n, 'req_sig': req_sig}
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_api_status(fmt='json', api_url="http://www.empirasign.com/api/mystatus/"):
"""check API status and see how many requests are left
fmt can be json or csv
"""
dt = datetime.date.today().strftime("%Y%m%d")
#compute the request signature
sig_keys = [APP_ID, dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'app_id': APP_ID, 'req_sig': req_sig}
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
def get_query_log(dt=None, fmt='json',
api_url="http://www.empirasign.com/api/query_log/"):
"""get a log of queries made on a given date
if provided, dt must be a datetime.date type
fmt can be json or csv
"""
# compute request signature
if not dt:
dt = datetime.date.today().strftime("%Y%m%d")
else:
dt = dt.strftime("%Y%m%d")
sig_keys = [APP_ID, dt, PW]
req_sig = hashlib.sha1("".join(sig_keys)).hexdigest()
url_params = {'app_id': APP_ID, 'dt': dt, 'req_sig': req_sig}
if fmt.lower() != 'json':
url_params['csv'] = True
url_params = urllib.urlencode(url_params)
api_url = api_url + "?" + url_params
req = urllib2.Request(api_url)
if PROXY_SERVER:
req.set_proxy(PROXY_SERVER.split("://")[-1], 'http')
resp = urllib2.urlopen(req)
if fmt.lower() == 'json':
return json.loads(resp.read())
return resp.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment