1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)
|