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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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)
|