aboutsummaryrefslogtreecommitdiffstats
path: root/python/book_bbg2.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/book_bbg2.py')
-rw-r--r--python/book_bbg2.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/python/book_bbg2.py b/python/book_bbg2.py
new file mode 100644
index 00000000..b3c53de1
--- /dev/null
+++ b/python/book_bbg2.py
@@ -0,0 +1,80 @@
+from serenitas.utils.env import DAILY_DIR
+from serenitas.utils.remote import SftpClient
+from zoneinfo import ZoneInfo
+import datetime
+import csv
+from trade_dataclasses import CDSDeal
+from decimal import Decimal
+
+_funds = {"SERENITAS_CGMF": "SERCGMAST", "BOWDOINST": "BOWDST"}
+_fcms = {"Bank of America, N.A.": "BAML", "Goldman Sachs": "GS"}
+
+
+def download_files(date: datetime.date):
+ sftp = SftpClient.from_creds("bbg")
+ dst = DAILY_DIR / str(date) / "bbg_tickets"
+ if not dst.exists():
+ dst.mkdir()
+ EST = ZoneInfo("US/Eastern")
+
+ def by_date(f, date):
+ local_dt = datetime.datetime.fromtimestamp(
+ f.st_mtime, tz=datetime.timezone.utc
+ ).astimezone(EST)
+ return local_dt.date() == date
+
+ sftp.download_files("/", dst, (lambda f: by_date(f, date),))
+
+
+def get_indic_data(conn, redcode, tenor):
+ sql_str = (
+ "SELECT maturity, coupon "
+ "FROM index_desc "
+ "WHERE tenor=%s AND redindexcode=%s "
+ )
+ with conn.cursor() as c:
+ c.execute(sql_str, (redcode, tenor))
+ return c.fetchone()
+
+
+def cdx_booking_process(path, conn):
+ with open(path) as fh:
+ reader = csv.DictReader(fh)
+ for line in reader:
+ tenor = line["Security"].rsplit(" ", 1)[-1].lower() + "r"
+ maturity, coupon = get_indic_data(conn, tenor, line["Red Code"])
+ trade = CDSDeal(
+ fund=_funds[line["Account"]],
+ folder="*",
+ portfolio="CURVE",
+ security_id=line["Red Code"],
+ security_desc=line["Security"],
+ traded_level=Decimal(line["Price (Dec)"]),
+ notional=line["Quantity"],
+ fixed_rate=coupon * 0.01,
+ trade_date=datetime.datetime.strptime(
+ line["Trade Dt"], "%m/%d/%Y"
+ ).date(),
+ maturity=maturity,
+ currency=line["Curncy"],
+ protection="Buyer" if line["Side"] == "B" else "Seller",
+ upfront=line["Principal"],
+ cp_code="BNPBNY",
+ account_code=_fcms[line["Client FCM"]],
+ )
+ trade.stage()
+ CDSDeal.commit()
+
+
+def book_trades(conn, date=datetime.date.today()):
+ download_files(date)
+ for p in (DAILY_DIR / str(date) / "bbg_tickets").glob("CDX*"):
+ cdx_booking_process(p, conn)
+
+
+if __name__ == "__main__":
+ from serenitas.utils.db import serenitas_pool
+
+ d = datetime.date(2022, 2, 7)
+ conn = serenitas_pool.getconn()
+ book_trades(conn, d)