(20241224) Live Feed From Kafka (to test).

This commit is contained in:
2024-12-24 14:06:05 +05:30
parent 7c7b10f27c
commit ae6a959a21
18 changed files with 1018 additions and 538 deletions
@@ -43,6 +43,9 @@ from typing import Optional, Literal, Union, List
from utils_v2.string import regex
from utils_v2.date_time import date_time
# Other models:
from models.finstitutions.trading.symbols import TradingSymbol
# To work with date and time:
import datetime
@@ -75,145 +78,6 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
# *****************************************************************************************************************
class TradingSymbol(BaseModel):
exchange: Literal["NSE", "NFO", "BSE", "BFO", "MCX", "CDS", "BCD"] = Field(
description = "the exchange on which this symbol is traded",
frozen = True
)
exchangeToken: str | int = Field(
description = "the code by which the exchange identifies this instrument",
frozen = True
)
broker: Literal["zerodhaKite", "iciciBreeze"] = Field(
description = "the broker that gave you the details of this instrument",
frozen = True
)
brokerToken: str | int = Field(
description = "the code by which the broker identifies this instrument",
frozen = True
)
name: str = Field(
description = "the name of the co./asset",
frozen = True
)
symbol: str = Field(
description = "tha trading symbol pf the co./asset",
frozen = True
)
tickSize: float = Field(
description = "the minimum step size in the change of price of the instrument",
frozen = True
)
ltp: float = Field(
description = "the last price of this instrument at the time of requesting the symbol list",
frozen = True
)
segment: str = Field(
description = "the segment which this asset represents",
frozen = True
)
type: str = Field(
description = "the type of the instrument in the segment",
frozen = True
)
lotSize: int = Field(
description = "the minimum tradeable qty of this instrument",
frozen = True
)
strike: int | float | None = Field(
description = "the strike price of the instrument if it is a derivative",
default = None,
frozen = True
)
expiryTs: AwareDatetime | None = Field(
description = "the expiry (utc) of this instrument if it is a derivative",
default = None,
frozen = True
)
expiryTz: str = Field(
description = "the timezone in which the expiry timestamp my be interpreted; should be compatible with pytz",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
@staticmethod
def from_zerodha_kite(instrument: dict):
return TradingSymbol(
exchange = instrument["exchange"],
exchangeToken = instrument["exchange_token"],
broker = "zerodhaKite",
brokerToken = instrument["instrument_token"],
name = instrument["name"],
symbol = instrument["tradingsymbol"],
tickSize = instrument["tick_size"],
ltp = instrument["last_price"],
segment = instrument["segment"],
type = instrument["instrument_type"],
lotSize = instrument["lot_size"],
strike = instrument["strike"],
expiryTs = date_time.to_timezone(
datetime_object = datetime.datetime.combine(
instrument["expiry"],
datetime.time(hour = 0, minute = 0, second = 0)
),
timezone = date_time.TIMEZONE_UTC
) if instrument["expiry"] else None,
expiryTz = "Asia/Kolkata"
)
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
@field_validator("expiryTs", mode = "before")
def parse_date_time(cls, value):
if not value: value = None
if isinstance(value, str):
value = value.strip()
value = date_time.parse_date_time(
input_value = value,
timezone = date_time.TIMEZONE_UTC
)
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(
value,
timezone = date_time.TIMEZONE_UTC
)
return value
# ---------------------------------------------------------------------------------------------------------------------
class TradingSymbolListBrokerResponse(BaseModel):
success: bool = Field(