aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/mtm_upload.py60
-rw-r--r--python/trade_dataclasses.py56
2 files changed, 56 insertions, 60 deletions
diff --git a/python/mtm_upload.py b/python/mtm_upload.py
index e48a26b6..358a878d 100644
--- a/python/mtm_upload.py
+++ b/python/mtm_upload.py
@@ -6,10 +6,10 @@ from serenitas.utils.env import DAILY_DIR
from serenitas.utils.remote import SftpClient
from serenitas.analytics.dates import next_business_day
import datetime
+from trade_dataclasses import CDSDeal
HEADERS = {
"tranche": [
- "Markit Field Name",
"Swap ID",
"Allocation ID",
"Description",
@@ -43,8 +43,7 @@ HEADERS = {
"Settlement Amount",
"Trader",
"Executing Broker",
- "Dealer",
- "Trade ID",
+ "Dealer Trade ID",
"Notes",
"Parent Transaction Code",
"Parent Trade Date",
@@ -84,58 +83,9 @@ HEADERS = {
def tranche_trades(conn):
- with conn.cursor() as c:
- trades = []
- c.execute(
- "SELECT * FROM cds where attach is not NULL and trade_date > %s and id=3445",
- (datetime.date(2020, 12, 1),),
- )
- for row in c:
- obj = row._asdict()
- rename_keys(
- obj,
- {
- "dealid": "Swap ID",
- "cp_code": "Broker Id",
- "trade_date": "Trade Date",
- "effective_date": "Effective Date",
- "maturity": "Maturity Date",
- "notional": "1st Leg Notional",
- "fixed_rate": "1st Leg Rate",
- "upfront": "Initial Payment",
- "security_id": "RED",
- "orig_attach": "Attachment Point",
- "orig_detach": "Exhaustion Point",
- "currency": "Currency Code",
- "upfront_settle_date": "First Payment Date",
- "cp_code": "Broker Id",
- },
- )
- if obj["Initial Payment"] >= 0:
- obj["Transaction Code"] = "Receive"
- else:
- obj["Initial Payment"] = abs(obj["Initial Payment"])
- obj["Transaction Code"] = "Pay"
- obj["Swap ID"] = "test_1"
- obj["Trade ID"] = obj["Swap ID"]
- obj["Product Type"] = "TRN"
- obj["Transaction Type"] = "NEW"
- obj["Protection"] = "Buy" if obj["protection"] == "Buyer" else "Sell"
- # obj["Trader"] = "Serenitas_Trader"
- obj["Entity Matrix"] = "Publisher"
- obj["Definitions Type"] = "ISDA2014Credit"
- obj["Independent Amount (%)"] = obj["initial_margin_percentage"]
- if "ITRX" in obj["security_desc"]:
- obj["Include Contractual Supplement"] = "Y"
- obj["Contractual Supplement"] = "StandardiTraxxEuropeTranche"
- # Temporary Static Values
- obj["Account Abbreviation"] = "Serenitas-test1"
- # obj["Initial Payment Currency"] = "USD"
- # obj["First Payment Date"] = "2022-01-18"
- # obj["Broker Id"] = "0000571T"
- # obj['DTCC Ineligible'] = 'N'
- obj["Master Document Date"] = "2016-02-17"
- trades.append(obj)
+ trades = []
+ obj = CDSDeal.from_tradeid(3472).to_markit()
+ trades.append(obj)
return trades
diff --git a/python/trade_dataclasses.py b/python/trade_dataclasses.py
index f66ca804..dad7421d 100644
--- a/python/trade_dataclasses.py
+++ b/python/trade_dataclasses.py
@@ -8,6 +8,7 @@ from psycopg2.extensions import register_adapter, AsIs
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
Fund = Literal["SERCGMAST", "BRINKER", "BOWDST"]
Portfolio = Literal[
@@ -124,12 +125,14 @@ class Deal:
super().__init_subclass__()
cls._table_name = table_name
cls._sql_fields = list(cls.__annotations__)
- place_holders = ",".join(["%s"] * len(cls.__annotations__))
- columns = ",".join(c for c in cls._sql_fields)
- cls._sql_insert = (
- f"INSERT INTO {cls._table_name}({columns}) VALUES({place_holders})"
+ _sql_insert_fields = list(
+ c for c in cls.__annotations__ if c not in ("id", "dealid")
)
- cls._sql_select = f"SELECT {columns} FROM {cls._table_name} WHERE id=%s"
+ insert_place_holders = ",".join(["%s"] * len(_sql_insert_fields))
+ insert_columns = ",".join(c for c in _sql_insert_fields)
+ select_columns = ",".join(c for c in cls._sql_fields)
+ cls._sql_insert = f"INSERT INTO {cls._table_name}({insert_columns}) VALUES({insert_place_holders})"
+ cls._sql_select = f"SELECT {select_columns} FROM {cls._table_name} WHERE id=%s"
def stage(self):
self._insert_queue.append([getattr(self, f) for f in self._sql_fields])
@@ -151,6 +154,9 @@ class Deal:
@dataclass
class CDSDeal(Deal, table_name="cds"):
+ id: field(default=None)
+ dealid: field(default=None)
+ initial_margin_percentage: field(default=None)
fund: Fund
account_code: str
cp_code: str
@@ -189,6 +195,46 @@ class CDSDeal(Deal, table_name="cds"):
)
index.direction = self.protection
+ def to_markit(self):
+ obj = self.__dict__
+ rename_keys(
+ obj,
+ {
+ "dealid": "Swap ID",
+ "cp_code": "Broker Id",
+ "trade_date": "Trade Date",
+ "effective_date": "Effective Date",
+ "maturity": "Maturity Date",
+ "notional": "1st Leg Notional",
+ "fixed_rate": "1st Leg Rate",
+ "upfront": "Initial Payment",
+ "security_id": "RED",
+ "orig_attach": "Attachment Point",
+ "orig_detach": "Exhaustion Point",
+ "currency": "Currency Code",
+ "upfront_settle_date": "First Payment Date",
+ "cp_code": "Broker Id",
+ "fund": "Account Abbreviation",
+ },
+ )
+ if obj["Initial Payment"] >= 0:
+ obj["Transaction Code"] = "Receive"
+ else:
+ obj["Initial Payment"] = abs(round(obj["Initial Payment"], 2))
+ obj["Transaction Code"] = "Pay"
+
+ obj["Trade ID"] = obj["Swap ID"]
+ obj["Product Type"] = "TRN"
+ obj["Transaction Type"] = "NEW"
+ obj["Protection"] = "Buy" if obj["protection"] == "Buyer" else "Sell"
+ obj["Entity Matrix"] = "Publisher"
+ obj["Definitions Type"] = "ISDA2014Credit"
+ obj["Independent Amount (%)"] = obj["initial_margin_percentage"]
+ if "ITRX" in obj["security_desc"]:
+ obj["Include Contractual Supplement"] = "Y"
+ obj["Contractual Supplement"] = "StandardiTraxxEuropeTranche"
+ return obj
+
@dataclass
class BondDeal(Deal, table_name="bonds"):