import datetime from dataclasses import dataclass from serenitas.utils.db import dbconn from serenitas.utils.exchange import ExchangeMessage from .misc import _sma_recipients, _cc_recipients from exchangelib import FileAttachment import pandas as pd from io import StringIO from typing import ClassVar @dataclass class SMA: date: datetime.date fund: ClassVar[str] _conn: ClassVar = dbconn("dawndb") _em: ClassVar = ExchangeMessage() def __init_subclass__(cls, fund): cls.fund = fund def get_positions(self): df_blotter = pd.read_sql_query( "SELECT * FROM risk_positions(%s, NULL, %s)", self._conn, params=(self.date, self.fund), index_col=["identifier"], ) cds_positions = pd.read_sql_query( "SELECT * FROM list_cds_marks_pre(%s, NULL, %s)", self._conn, params=(self.date, self.fund), index_col=["security_id"], ) tranche_positions = pd.read_sql_query( "SELECT id, security_id, security_desc, maturity, a.notional, " "protection, orig_attach, orig_detach, tranche_factor, clean_nav, " "accrued, cp_code, cpty_id from list_cds(%s, %s) a " "LEFT JOIN tranche_risk ON id=tranche_id AND date=%s " "WHERE orig_attach IS NOT NULL", self._conn, params=(self.date, self.fund, self.date), index_col=["id"], ) return df_blotter, cds_positions, tranche_positions def email_positions(self): attachments = [] for name, df in zip(("bonds", "cds", "tranches"), (self.get_positions())): buf = StringIO() df.to_csv(buf) attachments.append( FileAttachment( name=f"{self.date} {name}.csv", content=buf.getvalue().encode() ) ) buf.close() self._em.send_email( f"{self.fund} {self.date} EOD positions ", "", to_recipients=_sma_recipients[self.fund], cc_recipients=_cc_recipients[self.fund], attach=attachments, ) class IsoselSMA(SMA, fund="ISOSEL"): pass class BowdstSMA(SMA, fund="BOWDST"): pass