aboutsummaryrefslogtreecommitdiffstats
path: root/python/api_quotes/quotes.py
blob: f795d5dfcf30e438bb88dfc532efab927434183f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from serenitas.ops.trade_dataclasses import Deal
from dataclasses import dataclass
import datetime
from typing import Literal
from serenitas.utils.db2 import dbconn

firmness = Literal["FIRM", "INDICATIVE"]


def maturity_dt(d):
    try:
        return datetime.date(
            int(d["maturityyear"]), int(d["maturitymonth"]), int(d["maturityday"])
        )
    except (
        ValueError,
        KeyError,
    ):  # Sometimes maturity isn't included but we still have tenor
        return


class MarkitQuoteKind:
    def __class_getitem__(cls, quote_type: str):
        match quote_type:
            case "CD":
                return SingleNameQuote
            case "ABS":
                return BondQuote
            case "TRS":
                return TRSQuote


class MarkitQuote(Deal, table_name=None, deal_type=None):
    def __init_subclass__(cls, deal_type, table_name: str, **kwargs):
        super().__init_subclass__(deal_type=deal_type, table_name=table_name, **kwargs)
        cls._sql_insert = cls._sql_insert.replace(
            "RETURNING *", "ON CONFLICT (quoteid) DO NOTHING RETURNING *"
        )
        cls.init_dbconn(dbconn("serenitasdb"))

    @classmethod
    def from_markit_line(cls, d):
        base_attributes = {
            "msg_id": d["message"]["id"],
            "quotedate": datetime.datetime.fromtimestamp(d["receiveddatetime"] / 1000),
            "quotesource": d["sourceshortname"],
        }
        return base_attributes

    @classmethod
    def clear(cls):
        cls._insert_queue.clear()

    @classmethod
    def already_uploaded(cls):
        with cls._conn.cursor() as c:
            c.execute(f"SELECT distinct msg_id as msg_id FROM {cls._table_name}")
            return set(row.msg_id for row in c)

    # TODO
    # @property
    # def message(self):
    #     return QuoteDetails.from_tradeid(self.msg_id)


@dataclass
class SingleNameQuote(
    MarkitQuote, table_name="markit_singlename_quotes", deal_type=None
):
    quoteid: int
    msg_id: str
    quotesource: str
    confidence: int
    redcode: str = None
    ticker: str = None
    maturity: datetime.date = None
    tenor: int = None
    runningcoupon: int = None
    bidconventionalspread: float = None
    bidupfront: float = None
    bidsize: float = None
    askconventionalspread: float = None
    askupfront: float = None
    asksize: float = None
    firmness: firmness = None
    quotedate: datetime.datetime = None

    @classmethod
    def from_markit_line(cls, d):
        base_attributes = super().from_markit_line(d)
        additional_attributes = {
            "maturity": maturity_dt(d),
            "tenor": f"{d['tenor']}Y",
        }
        d.update(base_attributes | additional_attributes)
        return cls.from_dict(**d)


@dataclass
class BondQuote(MarkitQuote, table_name="markit_bond_quotes", deal_type=None):
    quoteid: int
    msg_id: str
    quotesource: str
    confidence: int
    identifier: str = None
    cusip: str = None
    bidprice: float = None
    bidsize: float = None
    askprice: float = None
    asksize: float = None
    pricelevel: float = None
    subtype: str = None
    quotetype: str = None
    firmness: firmness = None
    quotedate: datetime.datetime = None

    @classmethod
    def from_markit_line(cls, d):
        base_attributes = super().from_markit_line(d)
        additional_attributes = {
            "identifier": d["internalinstrumentidentifier"],
            "pricelevel": d.get("pricelevelnormalized"),
        }
        d.update(base_attributes | additional_attributes)
        return cls.from_dict(**d)


@dataclass
class TRSQuote(MarkitQuote, table_name="markit_trs_quotes", deal_type=None):
    quoteid: int
    msg_id: str
    quotesource: str
    confidence: int
    maturity: datetime.date
    identifier: str = None
    bidlevel: float = None
    asklevel: float = None
    nav: float = None
    ref: float = None
    firmness: firmness = None
    funding_benchmark: str = None
    quotedate: datetime.datetime = None

    @classmethod
    def from_markit_line(cls, d):
        base_attributes = super().from_markit_line(d)
        additional_attributes = {
            "identifier": d["ticker"],
            "ref": d.get("reference"),
            "nav": d.get("inavparsed"),
            "funding_benchmark": d.get("parsedbenchmark"),
            "maturity": maturity_dt(d),
        }
        d.update(base_attributes | additional_attributes)
        return cls.from_dict(**d)