diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/cds_rebook.py | 41 | ||||
| -rw-r--r-- | python/trade_dataclasses.py | 103 |
2 files changed, 66 insertions, 78 deletions
diff --git a/python/cds_rebook.py b/python/cds_rebook.py index 0be9cb26..6a3d45b5 100644 --- a/python/cds_rebook.py +++ b/python/cds_rebook.py @@ -41,23 +41,40 @@ def new_version_quotes( (auction_date,), ) for index, series, version, oldfactor, newfactor in c1: + c2.execute( - "INSERT into index_quotes_pre(date, index, series, version, " - "tenor, close_price, source)" - "SELECT date, index, series, version+1, " - "tenor, (%s*close_price + %s)/%s, 'SRNTAS'" - "FROM index_quotes_pre " + "SELECT tenor, close_price FROM index_quotes_pre " "WHERE index=%s AND series=%s AND version=%s AND date=%s AND source='MKIT'", - ( - oldfactor, - (newfactor - oldfactor) * adj_recovery, - newfactor, + (index, series, version, auction_date), + ) + w = oldfactor / newfactor + + to_insert = [] + for tenor, closeprice in c2: + new_price = w * closeprice + (1 - w) * adj_recovery + ind = CreditIndex( index, series, - version, - auction_date, - ), + tenor, + version + 1, + value_date=auction_date, + freeze_version=True, + ) + try: + ind.price = new_price + except ValueError: + continue + to_insert.append((tenor, new_price, ind.spread)) + c2.executemany( + "INSERT INTO index_quotes_pre(date, index, series, version, " + "tenor, close_price, close_spread, source) " + "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", + [ + (auction_date, index, series, version + 1, *t, "SRNTAS") + for t in to_insert + ], ) + conn.commit() diff --git a/python/trade_dataclasses.py b/python/trade_dataclasses.py index 9887c139..3186b7dc 100644 --- a/python/trade_dataclasses.py +++ b/python/trade_dataclasses.py @@ -2,6 +2,7 @@ from dataclasses import dataclass, field, fields, Field from enum import Enum from io import StringIO from headers import DealType, MTM_HEADERS, HEADERS +from csv_headers.citco import GIL from typing import ClassVar, Tuple, Union from decimal import Decimal from typing import Literal @@ -436,12 +437,26 @@ class CitcoDeal: _citco_headers = None _citco_trade_headers = None _sftp = SftpClient.from_creds("citco") - product_type: str + product_key = () - def __init_subclass__(cls, deal_type, **kwargs): + def __init_subclass__(cls, deal_type, product_key, **kwargs): super().__init_subclass__(deal_type, **kwargs) cls._citco_headers = GIL cls._citco_trade_headers = GTL + cls.product_key = product_key + + def get_dealid(self): + filter_clause = " AND ".join([f"{k}=%s" for k in self.product_key]) + sql_str = ( + f"SELECT id, dealid, committed FROM {self.table_name} WHERE {filter_clause}" + ) + with self._conn.cursor() as c: + c.execute( + sql_str, + tuple([getattr(self, k) for k in self.product_key]), + ) + if results := c.fetchone(): + (self.id, self.dealid, self.committed) = results @classmethod def citco_upload(cls): @@ -1235,10 +1250,11 @@ class TrancheType(IntEnum): @dataclass class TrancheProduct( - CitcoDeal, + CitcoProduct, Deal, deal_type=DealType.TrancheProduct, table_name="citco_tranche", + product_key=("underlying_security_id", "attach", "detach"), insert_ignore=( "id", "dealid", @@ -1273,15 +1289,8 @@ class TrancheProduct( default=None, metadata={"insert": False, "citco": "UniqueIdentifier"} ) - def get_dealid(self): - sql_str = "SELECT id, dealid, committed from citco_tranche where underlying_security_id = %s and attach = %s and detach = %s" - with self._conn.cursor() as c: - c.execute(sql_str, (self.underlying_security_id, self.attach, self.detach)) - if results := c.fetchone(): - (self.id, self.dealid, self.committed) = results - def __post_init__(self): - sql_str = "SELECT issue_date, maturity, coupon, index, series FROM index_version LEFT JOIN index_Maturity USING (series, INDEX) WHERE tenor='5yr' AND redindexcode=%s;" + sql_str = "SELECT issue_date, maturity, coupon, index, series FROM index_desc WHERE tenor='5yr' AND redindexcode=%s" with self._conn.cursor() as c: c.execute(sql_str, (self.underlying_security_id.removesuffix("_5"),)) ( @@ -1314,10 +1323,18 @@ class TrancheProduct( @dataclass class SwaptionProduct( - CitcoDeal, + CitcoProduct, Deal, deal_type=DealType.SwaptionProduct, table_name="citco_swaption", + product_key=( + "underlying_security_id", + "strike", + "expiration", + "callput", + "birth_date", + "death_date", + ), insert_ignore=( "id", "dealid", @@ -1348,28 +1365,10 @@ class SwaptionProduct( default=None, metadata={"insert": False, "citco": "UniqueIdentifier"} ) - def get_dealid(self): - sql_str = "SELECT id, dealid, committed from citco_swaption where instrument_type=%s and underlying_security_id =%s and strike =%s and expiration=%s and callput=%s and birth_date=%s and death_date=%s" - with self._conn.cursor() as c: - c.execute( - sql_str, - ( - self.instrument_type, - self.underlying_security_id, - self.strike, - self.expiration, - self.callput, - self.birth_date, - self.death_date, - ), - ) - if results := c.fetchone(): - (self.id, self.dealid, self.committed) = results - def __post_init__(self): self.get_dealid() if self.underlying_id_source == "RED": - sql_str = "SELECT issue_date, maturity, coupon, index, series FROM index_version LEFT JOIN index_Maturity USING (series, INDEX) WHERE tenor='5yr' AND redindexcode=%s;" + sql_str = "SELECT issue_date, maturity, coupon, index, series FROM index_desc WHERE tenor='5yr' AND redindexcode=%s" with self._conn.cursor() as c: c.execute(sql_str, (self.underlying_security_id.removesuffix("_5"),)) ( @@ -1419,10 +1418,11 @@ _citco_ratesource = {"SOFRRATE": 17819} @dataclass class IRSProduct( - CitcoDeal, + CitcoProduct, Deal, deal_type=DealType.IRSProduct, table_name="citco_irs", + product_key=("birth_date", "death_date", "float_index", "fixed_rate"), insert_ignore=("id", "dealid", "security_desc"), ): birth_date: datetime.date = field(metadata={"citco": "Birth_date"}) @@ -1449,24 +1449,9 @@ class IRSProduct( default=None, metadata={"insert": False, "citco": "UniqueIdentifier"} ) - def get_dealid(self): - sql_str = "SELECT id, dealid, committed from citco_irs where birth_date=%s and death_date=%s and float_index=%s and fixed_rate=%s" - with self._conn.cursor() as c: - c.execute( - sql_str, - ( - self.birth_date, - self.death_date, - self.float_index, - self.fixed_rate, - ), - ) - if results := c.fetchone(): - (self.id, self.dealid, self.committed) = results - self.security_desc = f"SWAP IRS {self.float_index}-{self.fixed_rate}" - def __post_init__(self): self.get_dealid() + self.security_desc = f"SWAP IRS {self.float_index}-{self.fixed_rate}" def to_citco(self): if not self.id: @@ -1503,10 +1488,11 @@ class IRSProduct( @dataclass class TRSProduct( - CitcoDeal, + CitcoProduct, Deal, deal_type=DealType.TRSProduct, table_name="citco_trs", + product_key=("birth_date", "death_date", "funding_index", "underlying_security"), insert_ignore=("id", "dealid"), ): birth_date: datetime.date = field(metadata={"citco": "Birth_date"}) @@ -1534,25 +1520,10 @@ class TRSProduct( default=None, metadata={"insert": False, "citco": "UniqueIdentifier"} ) - def get_dealid(self): - sql_str = "SELECT id, dealid, committed from citco_trs where birth_date=%s and death_date=%s and funding_index=%s and underlying_security=%s" - with self._conn.cursor() as c: - c.execute( - sql_str, - ( - self.birth_date, - self.death_date, - self.funding_index, - self.underlying_security, - ), - ) - if results := c.fetchone(): - (self.id, self.dealid, self.committed) = results - _citco_trs = {"4J623JAA8": "IBOXHY_TRS"} - self.security_desc = _citco_trs[self.underlying_security] - def __post_init__(self): self.get_dealid() + _citco_trs = {"4J623JAA8": "IBOXHY_TRS"} + self.security_desc = _citco_trs[self.underlying_security] def to_citco(self): if not self.id: |
