aboutsummaryrefslogtreecommitdiffstats
path: root/python/rebook_bonds.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/rebook_bonds.py')
-rw-r--r--python/rebook_bonds.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/python/rebook_bonds.py b/python/rebook_bonds.py
new file mode 100644
index 00000000..4892972a
--- /dev/null
+++ b/python/rebook_bonds.py
@@ -0,0 +1,62 @@
+from serenitas.ops.trade_dataclasses import BondDeal
+from serenitas.utils.db import dbconn
+import datetime
+from copy import copy
+
+
+def book_bonds(asset_class, old_custodian, new_custodian, trade_date):
+ conn = BondDeal._conn
+ trades = []
+ with conn.cursor() as c:
+ c.execute(
+ "SELECT * FROM list_positions_by_account(%s, %s, True, %s) a LEFT JOIN list_marks(%s) b ON a.figi=b.identifier LEFT JOIN portfolio_folder_mapping pfm ON a.strategy::text=pfm.clean_folder; ",
+ (trade_date, asset_class, old_custodian, trade_date),
+ )
+ for row in c:
+ d = row._asdict()
+ d["principal_payment"] = None
+ d["accrued_payment"] = None
+ d["asset_class"] = asset_class
+ d["cusip"] = d["identifier"]
+ d["trade_date"] = trade_date
+ d["settle_date"] = trade_date
+ d["cp_code"] = "INTERCO"
+ d["faceamount"] = d["notional"]
+ buy = BondDeal.from_dict(**d, buysell=True)
+ sell = BondDeal.from_dict(**d, buysell=False)
+ buy.stage()
+ sell.stage()
+ conn.commit()
+
+
+def book_allocations(asset_class, old_custodian, new_custodian, trade_date):
+ conn = BondDeal._conn
+ _insert_queue = []
+ with conn.cursor() as c:
+ c.execute(
+ "UPDATE bonds SET emailed=True, allocated=True WHERE asset_class=%s AND trade_date=%s AND cp_code='INTERCO' AND not allocated AND not emailed",
+ (asset_class, trade_date),
+ )
+ for row in c:
+ _insert_queue.append(
+ (
+ row.id,
+ row.faceamount,
+ new_custodian if row.buysell else old_custodian,
+ True,
+ )
+ )
+ c.executemany(
+ "INSERT INTO bondallocation (tradeid, notional, code, submitted) VALUES (%s, %s, %s, %s)",
+ _insert_queue,
+ )
+ conn.commit()
+
+
+if __name__ == "__main__":
+ old_custodian = "BAC"
+ new_custodian = "UMB"
+ move_date = datetime.date(2022, 11, 14)
+ for asset_class in ("Subprime", "CRT", "CLO"):
+ book_bonds(asset_class, old_custodian, new_custodian, move_date)
+ book_allocations(asset_class, old_custodian, new_custodian, move_date)