aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/book_bbg.py4
-rw-r--r--python/trade_dataclasses.py44
2 files changed, 21 insertions, 27 deletions
diff --git a/python/book_bbg.py b/python/book_bbg.py
index a0809d26..cc7b6ca7 100644
--- a/python/book_bbg.py
+++ b/python/book_bbg.py
@@ -29,8 +29,8 @@ def run():
with sftp.client.open(f.filename) as fh:
try:
Deal[DealType(deal_type)].process(fh, bbg_id)
- except TypeError as e:
- logger.warning(f"{bbg_id} missing cp in db")
+ except ValueError as e:
+ logger.warning(e)
pass
else:
BbgDeal._cache[bbg_id] = None
diff --git a/python/trade_dataclasses.py b/python/trade_dataclasses.py
index f46b6f5a..27c59cc0 100644
--- a/python/trade_dataclasses.py
+++ b/python/trade_dataclasses.py
@@ -324,6 +324,19 @@ class BbgDeal:
trade.stage()
type(trade).commit()
+ @classmethod
+ def get_cp_code(cls, bbg_code, code_type):
+ with cls._conn.cursor() as c:
+ c.execute(
+ "SELECT cp_code from bbg_ticket_mapping where bbg_code=%s and code_type=%s",
+ (bbg_code, code_type),
+ )
+ try:
+ (cp_code,) = c.fetchone()
+ except TypeError:
+ raise ValueError(f"missing {bbg_code} in the db for {code_type}")
+ return cp_code
+
class MTMDeal:
_mtm_queue: ClassVar[list] = []
@@ -440,8 +453,8 @@ class CDSDeal(
@classmethod
def from_bbg_line(cls, line: dict):
- with cls._conn.cursor() as c:
- if line["Coupon"] == "":
+ if line["Coupon"] == "":
+ with cls._conn.cursor() as c:
c.execute(
"SELECT coupon, index, series, tenor FROM index_desc "
"WHERE redindexcode=%s AND maturity =%s",
@@ -453,12 +466,8 @@ class CDSDeal(
coupon, index, series, tenor = c.fetchone()
line["Security"] = desc_str(index, series, tenor.removesuffix("yr"))
line["Coupon"] = coupon
- c.execute(
- "SELECT cp_code from bbg_ticket_mapping where bbg_code=%s and code_type='CDX'",
- (line["Brkr"],),
- )
- (cp_code,) = c.fetchone()
cls._bbg_insert_queue.append(list(line.values()))
+ cp_code = cls.get_cp_code(line["Brkr"], "CDS")
return cls(
fund=_funds[line["Account"]],
folder="*",
@@ -508,12 +517,7 @@ class BondDeal(BbgDeal, Deal, deal_type=DealType.Bond, table_name="bonds"):
)
results = c.fetchone()
line["asset_class"] = results[0] if results else None
- c.execute(
- "SELECT cp_code from bbg_ticket_mapping where bbg_code=%s and code_type='BOND'",
- (line["Brkr"],),
- )
- (cp_code,) = c.fetchone()
-
+ cp_code = cls.get_cp_code(line["Brkr"], "BOND")
return cls(
faceamount=Decimal(line["Quantity"]),
price=Decimal(line["Price (Dec)"]),
@@ -742,12 +746,7 @@ class SpotDeal(
@classmethod
def from_bbg_line(cls, line: dict):
- with cls._conn.cursor() as c:
- c.execute(
- "SELECT cp_code from bbg_ticket_mapping where bbg_code=%s and code_type='FX'",
- (line["Counterparty Deal Code"],),
- )
- (cp_code,) = c.fetchone()
+ cp_code = cls.get_cp_code(line["Counterparty Deal Code"], "FX")
if line["Side"] == "B":
key1, key2 = "buy", "sell"
else:
@@ -825,12 +824,7 @@ class FxSwapDeal(
@classmethod
def from_bbg_line(cls, line: dict):
- with cls._conn.cursor() as c:
- c.execute(
- "SELECT cp_code from bbg_ticket_mapping where bbg_code=%s and code_type='FX'",
- (line["Counterparty Deal Code"],),
- )
- (cp_code,) = c.fetchone()
+ cp_code = cls.get_cp_code(line["Counterparty Deal Code"], "FX")
if line["Side"] == "S":
key1, key2 = "buy", "sell"
else: