diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/markit/__main__.py | 11 | ||||
| -rw-r--r-- | python/markit/rates.py | 43 |
2 files changed, 52 insertions, 2 deletions
diff --git a/python/markit/__main__.py b/python/markit/__main__.py index 6f0d6c54..cb3ecf9f 100644 --- a/python/markit/__main__.py +++ b/python/markit/__main__.py @@ -10,6 +10,7 @@ import logging from common import root from .cds import download_cds_data, download_composite_data from .loans import download_facility, insert_facility, download_marks, update_facility +from .rates import downloadMarkitIRData from .import_quotes import insert_cds, insert_index, insert_tranche from pandas.tseries.offsets import BDay from sqlalchemy import create_engine @@ -23,8 +24,10 @@ group.add_argument("-l", "--loans", action="store_true", help="download markit loan data") group.add_argument("-c", "--cds", action="store_true", help="download markit cds data") +group.add_argument("-r", "--rates", action="store_true", + help="download markit IR data") parser.add_argument("-i", "--insert-only", action="store_true", - help="do not redownload data") + help="do not re-download data") parser.add_argument('workdate', nargs='?', type = lambda s: pd.datetime.strptime(s, "%Y-%m-%d").date()) args = parser.parse_args() @@ -64,7 +67,7 @@ if args.loans: update_facility(workdate, payload) logger.info('facility updated') -else: +elif args.cds: payload = {'user': 'GuillaumeHorel', 'password': 'password', 'version': '5', @@ -83,3 +86,7 @@ else: engine = create_engine('postgresql://serenitas_user@debian/serenitasdb') insert_index(engine, workdate) insert_tranche(engine, workdate) + +elif args.rates: + for curr in ["USD", "EUR"]: + downloadMarkitIRData(workdate, currency = curr) diff --git a/python/markit/rates.py b/python/markit/rates.py new file mode 100644 index 00000000..990a95e4 --- /dev/null +++ b/python/markit/rates.py @@ -0,0 +1,43 @@ +from db import dbconn +from common import root +import datetime +from io import BytesIO +import os +import pandas as pd +import requests +import xml.etree.ElementTree as ET +import zipfile + +def downloadMarkitIRData(download_date = datetime.date.today(), + currency = "USD"): + conn = dbconn("serenitasdb") + ## T+1 rates are published in the evening + effective_date = download_date + datetime.timedelta(days = 1) + basedir = os.path.join(root, "data", "Yield Curves") + filename = "InterestRates_{0}_{1:%Y%m%d}".format(currency, download_date) + if not os.path.exists(os.path.join(basedir, filename + '.xml')): + r = requests.get('http://www.markit.com/news/{0}.zip'.format(filename)) + if "zip" in r.headers['content-type']: + with zipfile.ZipFile(BytesIO(r.content)) as z: + z.extractall(path = os.path.join(root, "data", "Yield Curves")) + else: + return downloadMarkitIRData(download_date-datetime.timedelta(days=1)) + + tree = ET.parse(os.path.join(root, "data", "Yield Curves", filename + '.xml')) + deposits = zip([e.text for e in tree.findall('./deposits/*/tenor')], + [float(e.text) for e in tree.findall('./deposits/*/parrate')]) + swaps = zip([e.text for e in tree.findall('./swaps/*/tenor')], + [float(e.text) for e in tree.findall('./swaps/*/parrate')]) + effectiveasof = tree.find('./effectiveasof').text + MarkitData = {'deposits': list(deposits), + 'swaps': list(swaps), + 'effectiveasof': pd.Timestamp(effectiveasof).date()} + instruments = MarkitData['deposits'] + MarkitData['swaps'] + names = ",".join(['"{}"'.format(r[0]) for r in instruments]) + values = ",".join(["%s"] * (len(instruments) + 1)) # +1 for effective_date + insert_str = ("INSERT INTO {0}_rates(effective_date, {1}) VALUES({2}) ON CONFLICT DO NOTHING". + format(currency, names, values)) + with conn.cursor() as c: + c.execute(insert_str, [MarkitData['effectiveasof']] +[r[1] for r in instruments]) + conn.commit() + conn.close() |
