(20241224) MCX data test.
This commit is contained in:
@@ -40,6 +40,7 @@ from pydantic import BaseModel, Field, field_validator, PastDatetime, model_vali
|
||||
from typing import Optional, Literal, Union, List
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.date_time import date_time
|
||||
|
||||
@@ -78,20 +79,24 @@ class OneMarketDepth(BaseModel):
|
||||
|
||||
price: float = Field(
|
||||
description = "a price at which trader(s) are willing to trade this instrument",
|
||||
frozen = True
|
||||
frozen = True,
|
||||
alias = "price"
|
||||
)
|
||||
|
||||
qty: int = Field(
|
||||
description = "the no. of shares available at the above price",
|
||||
frozen = True
|
||||
frozen = True,
|
||||
alias = "quantity"
|
||||
)
|
||||
|
||||
orders: int = Field(
|
||||
description = "how many orders have contributed to the above quantity"
|
||||
description = "how many orders have contributed to the above quantity",
|
||||
frozen = True,
|
||||
alias = "orders"
|
||||
)
|
||||
|
||||
@computed_field
|
||||
def liquidity(self) -> float:
|
||||
def lqdty(self) -> float:
|
||||
return self.price * self.qty
|
||||
|
||||
# ┏┓ ┏•
|
||||
@@ -194,6 +199,10 @@ class TradingTick(BaseModel):
|
||||
frozen = True
|
||||
)
|
||||
|
||||
qty: int = Field(
|
||||
description = "how many units were traded in this tick"
|
||||
)
|
||||
|
||||
chg: float = Field(
|
||||
description = "the absolute change since the previous close",
|
||||
frozen=True
|
||||
@@ -255,13 +264,12 @@ class TradingTick(BaseModel):
|
||||
)
|
||||
|
||||
oiDayLow: int | None = Field(
|
||||
description="this session's lowest open interest of this instrument (if derivative)",
|
||||
frozen=True
|
||||
description = "this session's lowest open interest of this instrument (if derivative)",
|
||||
frozen = True
|
||||
)
|
||||
|
||||
tradeTs: AwareDatetime | None = Field(
|
||||
tradeTs: AwareDatetime = Field(
|
||||
description = "the last trade time (utc) of this instrument",
|
||||
default = None,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
@@ -271,9 +279,8 @@ class TradingTick(BaseModel):
|
||||
examples = ["UTC", "Asia/Kolkata"]
|
||||
)
|
||||
|
||||
exchgTs: AwareDatetime | None = Field(
|
||||
exchgTs: AwareDatetime = Field(
|
||||
description = "the time (utc) at which this update was received from the exchange",
|
||||
default = None,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
@@ -287,6 +294,19 @@ class TradingTick(BaseModel):
|
||||
description = "the market depth data for this instrument at the time of this update"
|
||||
)
|
||||
|
||||
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
|
||||
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
|
||||
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
|
||||
# ┛
|
||||
|
||||
@computed_field
|
||||
def tickCashflow(self) -> float:
|
||||
return self.qty * self.ltp
|
||||
|
||||
@computed_field
|
||||
def totCashflow(self) -> float:
|
||||
return self.totVol * self.vwap
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
@@ -302,7 +322,7 @@ class TradingTick(BaseModel):
|
||||
@staticmethod
|
||||
def from_zerodha_kite(
|
||||
ticks: dict | List[dict],
|
||||
lookup: dict
|
||||
instrument_lookup: dict
|
||||
) -> list:
|
||||
|
||||
# Ensure that we are working with a list:
|
||||
@@ -311,9 +331,14 @@ class TradingTick(BaseModel):
|
||||
# Iterate through the ticks and fit them into the model:
|
||||
modelled_ticks = []
|
||||
for tick in ticks:
|
||||
|
||||
# Stash frequently needed vars:
|
||||
broker_token = tick["instrument_token"]
|
||||
tick_lookup = lookup[broker_token]
|
||||
tick_lookup = instrument_lookup[broker_token]
|
||||
change = tick["change"]
|
||||
last_price = tick["last_price"]
|
||||
|
||||
# Model the currently picked tick:
|
||||
modelled_ticks.append(
|
||||
TradingTick(
|
||||
symbol = tick_lookup["symbol"],
|
||||
@@ -321,14 +346,32 @@ class TradingTick(BaseModel):
|
||||
exchangeToken = tick_lookup["exchangeToken"],
|
||||
broker = "zerodhaKite",
|
||||
brokerToken = broker_token,
|
||||
tradeable = tick["tradeable"],
|
||||
tradeable = tick["tradable"],
|
||||
segment = tick_lookup["segment"],
|
||||
type = tick_lookup["type"],
|
||||
strike = tick_lookup.get("strike"),
|
||||
expiryTs = tick_lookup["expiryTs"],
|
||||
expiryTz = tick_lookup["expiryTz"],
|
||||
ltp = ,
|
||||
ltp = last_price,
|
||||
qty = tick["last_traded_quantity"],
|
||||
chg = change,
|
||||
pChg =
|
||||
pChg = change / (last_price - change),
|
||||
o = tick["ohlc"]["open"],
|
||||
h = tick["ohlc"]["high"],
|
||||
l = tick["ohlc"]["low"],
|
||||
c = tick["ohlc"]["close"],
|
||||
totVol = tick["volume_traded"],
|
||||
vwap = tick["average_traded_price"],
|
||||
totBuyQty = tick["total_buy_quantity"],
|
||||
totSellQty = tick["total_sell_quantity"],
|
||||
oi = tick["oi"],
|
||||
oiDayHigh = tick["oi_day_high"],
|
||||
oiDayLow = tick["oi_day_low"],
|
||||
tradeTs = tick["last_trade_time"],
|
||||
tradeTz = "Asia/Kolkata",
|
||||
exchgTs = tick["exchange_timestamp"],
|
||||
exchgTz = "Asia/Kolkata",
|
||||
depth = tick["depth"]
|
||||
)
|
||||
)
|
||||
|
||||
@@ -355,7 +398,11 @@ class TradingTick(BaseModel):
|
||||
value = value.strip()
|
||||
value = date_time.parse_date_time(
|
||||
input_value = value,
|
||||
timezone = date_time.TIMEZONE_UTC
|
||||
timezone = date_time.TIMEZONE_UTC,
|
||||
date_formats = [
|
||||
"%Y-%m-%d",
|
||||
"%Y-%m-%d %H:%M:%S",
|
||||
]
|
||||
)
|
||||
|
||||
# When the input is a datetime obj.,
|
||||
@@ -464,13 +511,15 @@ if __name__ == "__main__":
|
||||
"exchange": "NSE",
|
||||
"exchangeToken": 12345678,
|
||||
"segment": "NFO-OPT",
|
||||
"type": "CE"
|
||||
"type": "CE",
|
||||
"expiryTs": "2024-12-20",
|
||||
"expiryTz": "Asia/Kolkata"
|
||||
}
|
||||
}
|
||||
|
||||
my_ticks = TradingTick.from_zerodha_kite(
|
||||
ticks = zerodha_tick,
|
||||
ticks = [zerodha_tick] * 10_000,
|
||||
instrument_lookup = zerodha_lookup
|
||||
)
|
||||
|
||||
print(my_ticks[0])
|
||||
print(json.to_string(my_ticks[0].model_dump(), default = str))
|
||||
|
||||
Reference in New Issue
Block a user