diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/markit/rates.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/python/markit/rates.py b/python/markit/rates.py index 470cfcc0..29a39465 100644 --- a/python/markit/rates.py +++ b/python/markit/rates.py @@ -11,6 +11,59 @@ from serenitas.analytics.bbg_helpers import retrieve_data from serenitas.analytics.dates import prev_business_day +def downloadMarkitOISData(conn, download_date=datetime.date.today(), currency="USD"): + base_dir = DATA_DIR / "Yield Curves" + url = f"https://rfr.ihsmarkit.com/InterestRates_USD_{download_date:%Y%m%d}.zip?email=ghorel@lmcg.com" + curve_file = base_dir / f"InterestRatesOIS_{currency}_{download_date:%Y%m%d}.xml" + if not curve_file.exists(): + r = requests.get(url) + if "zip" in r.headers["content-type"]: + with zipfile.ZipFile(BytesIO(r.content)) as z: + for f in z.filelist: + if f.filename.endswith("xml"): + f.filename = curve_file.name + z.extract(f, path=base_dir) + else: + raise ValueError(r.content.decode().rstrip()) + + tree = ET.parse(curve_file) + ois = zip( + [e.text for e in tree.findall(".//ois/*/tenor")], + [float(e.text) for e in tree.findall(".//ois/*/parrate")], + ) + effectiveasof = tree.find(".//effectiveasof").text + MarkitData = { + "ois": list(ois), + "effectiveasof": datetime.date.fromisoformat(effectiveasof), + } + ql_yc = YC( + currency=currency, + MarkitData=MarkitData, + evaluation_date=MarkitData["effectiveasof"], + ) + jp_yc = ql_to_jp(ql_yc) + sql_str = f"INSERT INTO {currency}_OIS_curves VALUES(%s, %s) ON CONFLICT DO NOTHING" + with conn.cursor() as c: + c.execute( + sql_str, + (MarkitData["effectiveasof"], jp_yc.__getstate__()), + ) + instruments = MarkitData["ois"] + 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}_OIS_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] + ) + conn.commit() + + def downloadMarkitIRData(conn, download_date=datetime.date.today(), currency="USD"): base_dir = DATA_DIR / "Yield Curves" curve_file = base_dir / f"InterestRates_{currency}_{download_date:%Y%m%d}.xml" |
