aboutsummaryrefslogtreecommitdiffstats
path: root/python/book_bbg.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/book_bbg.py')
-rw-r--r--python/book_bbg.py49
1 files changed, 6 insertions, 43 deletions
diff --git a/python/book_bbg.py b/python/book_bbg.py
index 4856ae5d..e12646cb 100644
--- a/python/book_bbg.py
+++ b/python/book_bbg.py
@@ -1,52 +1,16 @@
from serenitas.utils.remote import SftpClient
-import datetime
+from trade_dataclasses import Deal, DealType, BbgDeal
import csv
-from trade_dataclasses import CDSDeal, BondDeal, BbgDeal, _funds, _cdx_cp, _fcms
-from decimal import Decimal
from stat import S_ISREG
-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):
- reader = csv.DictReader(path)
- for line in reader:
- trade = CDSDeal(
- fund=_funds[line["Account"]],
- folder="*",
- portfolio="UNALLOCATED",
- security_id=line["Red Code"],
- security_desc=line["Security"].removesuffix(" PRC"),
- traded_level=Decimal(line["Price (Dec)"]),
- notional=line["Quantity"],
- fixed_rate=float(line["Coupon"]) * 0.01,
- trade_date=datetime.datetime.strptime(line["Trade Dt"], "%m/%d/%Y").date(),
- maturity=datetime.datetime.strptime(line["Mat Dt"], "%m/%d/%Y").date(),
- currency=line["Curncy"],
- protection="Buyer" if line["Side"] == "B" else "Seller",
- upfront=line["Principal"],
- cp_code=_cdx_cp[line["Brkr"]],
- account_code=_fcms[line["Client FCM"]],
- )
- trade.stage()
- CDSDeal.commit()
-
-
-def bond_booking_process(file_handle, index):
+def trade_booking_process(file_handle, index, asset_class):
+ deal = Deal[DealType(asset_class)]
for row in csv.DictReader(file_handle):
line = {"bbg_ticket_id": index, **row}
- trade = BondDeal.from_bbg_line(line)
+ trade = deal.from_bbg_line(line)
trade.stage()
- BondDeal.commit()
+ deal.commit()
def get_bbg_id(name):
@@ -56,14 +20,13 @@ def get_bbg_id(name):
if __name__ == "__main__":
import time
- booker = {"BOND": bond_booking_process, "CDX": cdx_booking_process}
sftp = SftpClient.from_creds("bbg")
while True:
for f in sftp.client.listdir_iter("/"):
if S_ISREG(f.st_mode):
if (bbg_id := get_bbg_id(f.filename)) not in BbgDeal._cache:
with sftp.client.open(f.filename) as fh:
- booker[f.filename.split("-", 1)[0]](fh, bbg_id)
+ trade_booking_process(fh, bbg_id, f.filename.split("-", 1)[0])
BbgDeal._cache[bbg_id] = None
time.sleep(60)