diff options
| -rw-r--r-- | python/markit/rates.py | 69 |
1 files changed, 44 insertions, 25 deletions
diff --git a/python/markit/rates.py b/python/markit/rates.py index 700a9304..3720c4ad 100644 --- a/python/markit/rates.py +++ b/python/markit/rates.py @@ -12,41 +12,60 @@ import zipfile from yieldcurve import YC, ql_to_jp from pickle import dumps -def downloadMarkitIRData(download_date=datetime.date.today(), - currency="USD"): + +def downloadMarkitIRData(download_date=datetime.date.today(), currency="USD"): conn = dbconn("serenitasdb") - basedir = os.path.join(os.environ['DATA_DIR'], "Yield Curves") + basedir = os.path.join(os.environ["DATA_DIR"], "Yield Curves") filename = f"InterestRates_{currency}_{download_date:%Y%m%d}" - if not os.path.exists(os.path.join(basedir, filename + '.xml')): - r = requests.post(f'http://www.markit.com/news/{filename}.zip') - if "zip" in r.headers['content-type']: + if not os.path.exists(os.path.join(basedir, filename + ".xml")): + r = requests.post(f"http://www.markit.com/news/{filename}.zip") + if "zip" in r.headers["content-type"]: with zipfile.ZipFile(BytesIO(r.content)) as z: - z.extractall(path=os.path.join(os.environ['DATA_DIR'], "Yield Curves")) + z.extractall(path=os.path.join(os.environ["DATA_DIR"], "Yield Curves")) else: raise ValueError(r.content.decode().rstrip()) - tree = ET.parse(os.path.join(os.environ['DATA_DIR'], "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()} - ql_yc = YC(currency=currency, MarkitData=MarkitData, evaluation_date=MarkitData['effectiveasof']) + tree = ET.parse( + os.path.join(os.environ["DATA_DIR"], "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(), + } + ql_yc = YC( + currency=currency, + MarkitData=MarkitData, + evaluation_date=MarkitData["effectiveasof"], + ) jp_yc = ql_to_jp(ql_yc) sql_str = f"INSERT INTO {currency}_curves VALUES(%s, %s) ON CONFLICT DO NOTHING" with conn.cursor() as c: - c.execute(sql_str, (MarkitData['effectiveasof'], - lz4.block.compress(jp_yc.__getstate__()))) - instruments = MarkitData['deposits'] + MarkitData['swaps'] + c.execute( + sql_str, + (MarkitData["effectiveasof"], lz4.block.compress(jp_yc.__getstate__())), + ) + instruments = MarkitData["deposits"] + MarkitData["swaps"] names = sql.SQL(", ").join([sql.Identifier(r[0]) for r in instruments]) - values = sql.SQL(", ").join(sql.Placeholder() * (len(instruments) + 1)) # +1 for effective_date - insert_str = (sql.SQL(f"INSERT INTO {currency}_rates(effective_date, {{}}) VALUES({{}}) " \ - "ON CONFLICT DO NOTHING"). - format(names, values)) + values = sql.SQL(", ").join( + sql.Placeholder() * (len(instruments) + 1) + ) # +1 for effective_date + insert_str = sql.SQL( + f"INSERT INTO {currency}_rates(effective_date, {{}}) VALUES({{}}) " + "ON CONFLICT DO NOTHING" + ).format(names, values) with conn.cursor() as c: - c.execute(insert_str, [MarkitData['effectiveasof']] +[r[1] for r in instruments]) + c.execute( + insert_str, [MarkitData["effectiveasof"]] + [r[1] for r in instruments] + ) conn.commit() conn.close() |
