aboutsummaryrefslogtreecommitdiffstats
path: root/python/api_quotes/quotes.py
blob: 04b6da09aa4f6fb4fe2c335c90335021fac849ac (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
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


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
            ).replace(tzinfo=datetime.timezone.utc),
            "quotesource": d["sourceshortname"],
        }
        return base_attributes

    # 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)

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