diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/book_bbg.py | 61 | ||||
| -rw-r--r-- | python/trade_dataclasses.py | 23 |
2 files changed, 44 insertions, 40 deletions
diff --git a/python/book_bbg.py b/python/book_bbg.py index 6f8d6f17..369251b1 100644 --- a/python/book_bbg.py +++ b/python/book_bbg.py @@ -86,35 +86,23 @@ def cdx_booking_process(path, conn): CDSDeal.commit() -def bond_booking_process(path, fname, _cache): - df = pd.read_csv(path) - for _, line in df.iterrows(): - trade = BondDeal( - faceamount=Decimal(line["Quantity"]), - price=Decimal(line["Price (Dec)"]), - cp_code=_bond_cp[line["Brkr"]], - cusip=line["Cusip"], - identifier=line["Cusip"], - trade_date=datetime.datetime.strptime(line["Trade Dt"], "%m/%d/%Y"), - settle_date=datetime.datetime.strptime(line["SetDt"], "%m/%d/%Y"), - portfolio="UNALLOCATED", - asset_class=None, - description=line["Security"].removesuffix(" Mtge"), - buysell=line["Side"] == "B", - bbg_ticket_id=fname, - ) - trade.stage() - df["bbg_ticket_id"] = [fname] - try: - df.to_sql("bond_tickets", dawn_engine, if_exists="append", index=False) - except SQLAlchemyError as e: - error = str(e.__dict__["orig"]) - BondDeal._insert_queue.clear() - print(error) - else: - _cache[fname] = None - finally: - BondDeal.commit() +def bond_booking_process(path, fname): + with open(path) as fh: + reader = csv.DictReader(fh) + for line in reader: + line["bbg_ticket_id"] = fname + trade = BondDeal.from_bbg_line(line) + trade.stage() + try: + BondDeal.insert_bbg_line(line) + except Exception as e: + pass + BondDeal._insert_queue.clear() + print("error") + else: + _cache[fname] = None + finally: + BondDeal.commit() def get_bbg_id(name): @@ -133,17 +121,10 @@ if __name__ == "__main__": for f in filter( lambda f: all(filt(f) for filt in filters), sftp.client.listdir_iter("/") ): - if ("CDX" in f.filename) or ("BOND" in f.filename): - if get_bbg_id(f.filename) in _cache.keys(): + if "BOND" in f.filename: + bbg_id = get_bbg_id(f.filename) + if bbg_id in BondDeal._cache.keys(): continue else: - if "CDX" in f.filename: - # cds_booking_process(sftp.client.open(f"/{f.filename}"), conn, get_bbg_id(f.filename), _cache) - pass - elif "BOND" in f.filename: - bond_booking_process( - sftp.client.open(f"/{f.filename}"), - get_bbg_id(f.filename), - _cache, - ) + bond_booking_process(sftp.client.open(f"/{f.filename}"), bbg_id) time.sleep(60) diff --git a/python/trade_dataclasses.py b/python/trade_dataclasses.py index c6bd1353..ead0774d 100644 --- a/python/trade_dataclasses.py +++ b/python/trade_dataclasses.py @@ -9,6 +9,7 @@ from serenitas.analytics.dates import next_business_day, previous_twentieth from serenitas.analytics.index import CreditIndex from serenitas.utils.db import dbconn from process_queue import rename_keys +from lru import LRU Fund = Literal["SERCGMAST", "BRINKER", "BOWDST"] Portfolio = Literal[ @@ -120,6 +121,7 @@ class Deal: _sql_insert: ClassVar[str] _sql_select: ClassVar[str] _insert_queue: ClassVar[list] = [] + _cache: ClassVar[LRU] = LRU(128) def __init_subclass__(cls, table_name: str): super().__init_subclass__() @@ -254,3 +256,24 @@ class BondDeal(Deal, table_name="bonds"): portfolio: Portfolio = field(default=None) asset_class: AssetClass = field(default=None) bbg_ticket_id: str = None + + def from_bbg_line(self, line: dict): + self.faceamount = Decimal(line["Quantity"]) + self.price = Decimal(line["Price (Dec)"]) + self.cp_code = _bond_cp[line["Brkr"]] + self.cusip = line["Cusip"] + self.identifier = line["Cusip"] + self.trade_date = datetime.datetime.strptime(line["Trade Dt"], "%m/%d/%Y") + self.settle_date = datetime.datetime.strptime(line["SetDt"], "%m/%d/%Y") + self.portfolio = "UNALLOCATED" + self.asset_class = None + self.description = line["Security"].removesuffix(" Mtge") + self.buysell = line["Side"] == "B" + self.bbg_ticket_id = line["bbg_ticket_id"] + + def insert_bbg_line(self, line: dict): + bond_ticket_columns = ",".join(c for c in line.keys()) + bond_place_holders = ",".join(["%s"] * len(line.keys())) + sql_str = f"INSERT INTO bond_tickets({bond_ticket_columns}) VALUES({bond_place_holders})" + with self._conn() as c: + c.execute(sql_str, line.values()) |
