Merge commit 'bfbc270b655b1db87dfd9e3afaf79d9968042da5' as 'utils_v2'

This commit is contained in:
yatmesh
2025-06-12 13:46:57 +05:30
217 changed files with 149785 additions and 0 deletions
View File
+132
View File
@@ -0,0 +1,132 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Wednesday, 30th Oct., 2024.
OBJECTIVE:
To provide a data model for describing the API response from Google's APIs.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, model_validator
from typing import Optional, Literal, Union, Dict, List, Any
# My utils:
from utils_v2.string import json
from utils_v2.string import regex
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class NSEApiResponse(BaseModel):
action: str = Field(frozen = False, default = None)
url: str | None = Field(frozen = True)
method: str | None = Field(frozen = True)
inputs: Any | None = Field(frozen = True)
response: Any = None
httpCode: int = None
success: bool = False
message: str = None
data: Any = None
exception: Any = None
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "forbid"
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_markdown(self):
if self.exception: message = "❌ *NSE API EXCEPTION:* ❌\n\n"
else: message = "*NSE API RESPONSE:*\n\n"
message += f"*ACTION:*\n`{self.action}`\n\n"
message += f"*URL:*\n`{self.url}`\n\n"
message += f"*METHOD:*\n`{self.method}`\n\n"
message += f"*RESPONSE:*\n`{self.response}`\n\n"
message += f"*SUCCESS:*\n`{self.success}`\n\n"
message += f"*MESSAGE:*\n`{self.message}`\n\n"
message += f"*EXCEPTION:*\n`{self.exception.__class__.__name__}: {str(self.exception)}`\n\n"
return message
async def get_json(self):
try: return self.response.json()
except: return {}
async def get_content(self):
try: return self.response.content
except: return b""
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+396
View File
@@ -0,0 +1,396 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Thursday, 9th Jan., 2025.
OBJECTIVE:
To provide data models for NSE's calendar events.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, AwareDatetime, computed_field
from typing import Optional, Literal, List
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class NSEHoliday(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
ts: AwareDatetime = Field(
description = "The date which is to be recorded as a holiday.",
frozen = True
)
tz: str = Field(
description = "The timezone in which the date has to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
event: str = Field(
description = "The name of the event/occasion.",
frozen = True
)
type: List[str] = Field(
description = "One or more types of holiday for this event.",
frozen = True,
examples = ["CBM", "CD", "CM", "CMOT", "COM", "FO", "IRD", "MF", "NDM", "NTRP", "SLBS"]
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
@computed_field
def year(self) -> int:
return self.ts.year
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("ts", "scrapeTs", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
return {
"event": self.event,
"ts": self.ts.timestamp(),
"tz": self.tz,
"type": self.type
}
# ---------------------------------------------------------------------------------------------------------------------
class NSECorporateEvent(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
kind: str | None = Field(
description = "The type of 'index'.",
frozen = True,
default = None
)
symbol: str = Field(
description = "The trading symbol of the listed entity.",
frozen = True
)
name: str = Field(
description = "The full name of the listed entity.",
frozen = True
)
ts: AwareDatetime = Field(
description = "The date (UTC) of the corporate event.",
frozen = True
)
tz: str = Field(
description = "The timezone in which the date has to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
purpose: str = Field(
description = "The type of the event.",
frozen = True
)
description: str = Field(
description = "A brief description of the event.",
frozen = True
)
attachment: str | None = Field(
description = "Any file attachment with the event announcement.",
frozen = True,
default = None
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
pass
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("ts", "scrapeTs", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
return {
"symbol": self.symbol,
"name": self.name,
"kind": self.kind,
"purpose": self.purpose,
"description": self.description,
"attachment": self.attachment,
"ts": self.ts.timestamp(),
"tz": self.tz
}
# ---------------------------------------------------------------------------------------------------------------------
class NSECorporateAction(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
kind: str | None = Field(
description = "The type of 'index'.",
frozen = True,
default = None
)
symbol: str = Field(
description = "The trading symbol of the listed entity.",
frozen = True
)
name: str = Field(
description = "The full name of the listed entity.",
frozen = True
)
isin: str = Field(
description = "The ISIN code of the listed entity.",
frozen = True
)
series: str = Field(
description = "The series code of the listed entity.",
frozen = True
)
exTs: AwareDatetime | None = Field(
description = "The ex-date/ex-dividend-date (UTC) ",
frozen = True
)
recTs: AwareDatetime | None = Field(
description = "The record date (UTC).",
frozen = True
)
bcStartTs: AwareDatetime | None = Field(
description = "The book-closure start date (UTC).",
frozen = True
)
bcEndTs: AwareDatetime | None = Field(
description = "The book-closure end date (UTC).",
frozen = True
)
ndStartTs: AwareDatetime | None = Field(
description = "The notice-date (start) date (UTC).",
frozen = True
)
ndEndTs: AwareDatetime | None = Field(
description = "The notice-date (end) date (UTC).",
frozen = True
)
caBroadcastTs: AwareDatetime | None = Field(
description = "The corporate-action broadcast date (UTC).",
frozen = True
)
tz: str = Field(
description = "The timezone in which all dates have to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
action: str = Field(
description = "The subject of the corporate action.",
frozen = True
)
faceVal: float | int = Field(
description = "The face-value of the share of the entity.",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
pass
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator(
"scrapeTs",
"exTs", "recTs",
"bcStartTs", "bcEndTs",
"ndStartTs", "ndEndTs",
"caBroadcastTs",
mode = "before"
)
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
return {
"symbol": self.symbol,
"name": self.name,
"isin": self.isin,
"series": self.series,
"kind": self.kind,
"action": self.action,
"faceVal": self.faceVal,
"exTs": self.exTs.timestamp() if isinstance(self.exTs, datetime.datetime) else self.exTs,
"recTs": self.recTs.timestamp()if isinstance(self.recTs, datetime.datetime) else self.recTs,
"bcStartTs": self.bcStartTs.timestamp()if isinstance(self.bcStartTs, datetime.datetime) else self.bcStartTs,
"bcEndTs": self.bcEndTs.timestamp()if isinstance(self.bcEndTs, datetime.datetime) else self.bcEndTs,
"ndStartTs": self.ndStartTs.timestamp()if isinstance(self.ndStartTs, datetime.datetime) else self.ndStartTs,
"ndEndTs": self.ndEndTs.timestamp()if isinstance(self.ndEndTs, datetime.datetime) else self.ndEndTs,
"caBroadcastTs": self.caBroadcastTs.timestamp()if isinstance(self.caBroadcastTs, datetime.datetime) else self.caBroadcastTs,
"tz": self.tz
}
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+319
View File
@@ -0,0 +1,319 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Tuesday, 14th Jan., 2025.
OBJECTIVE:
To provide data model(s) for NSE's index information.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, AwareDatetime, computed_field
from typing import Optional, Literal, List
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class NSEIndexInfo(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
tz: str = Field(
description = "The timezone in which the dates have to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
symbol: str = Field(
description = "The symbol of this index.",
frozen = True
)
name: str = Field(
description = "The display name of this index.",
frozen = True
)
kind: str = Field(
description = "The category/type of this index.",
frozen = True
)
open: float | None = Field(
description = "The opening price of this index.",
frozen = True
)
high: float | None = Field(
description = "The highest price of this index.",
frozen = True
)
low: float | None = Field(
description = "The lowest price of this index.",
frozen = True
)
close: float | None = Field(
description = "The LTP of this index.",
frozen = True
)
prevClose: float | None = Field(
description = "The previous session's closing price of this index.",
frozen = True
)
yearHigh: float | None = Field(
description = "The year-high value of this symbol.",
frozen = True
)
yearLow: float | None = Field(
description = "The year-low value of this symbol.",
frozen = True
)
pChg: float | None = Field(
description = "The percent change since the last trading session.",
frozen = True
)
advances: int | None = Field(
description = "The no. of constituent symbols that are running positive.",
frozen = True
)
declines: int | None = Field(
description = "The no. of constituent symbols that are running negative.",
frozen = True
)
unchanged: int | None = Field(
description = "The no. of constituent symbols that are running flat.",
frozen = True
)
pChg30d: float | None = Field(
description = "The percent change in the last month.",
frozen = True
)
pChg365d: float | None = Field(
description = "The percent change in the last year.",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
@computed_field()
def chg(self) -> float:
return self.close - self.prevClose
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("scrapeTs", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
model_json = self.model_dump()
model_json["scrapeTs"] = model_json["scrapeTs"].timestamp()
return model_json
# # ---------------------------------------------------------------------------------------------------------------------
#
#
# class NSEPreMarketData(BaseModel):
#
# scrapeTs: AwareDatetime = Field(
# description = "The time (UTC) at which this this data was scraped.",
# frozen = True,
# default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
# )
#
# ts: AwareDatetime = Field(
# description = "The time (UTC) at which this data was made available by NSE.",
# frozen = True
# )
#
# tz: str = Field(
# description = "The timezone in which the dates have to be interpreted.",
# frozen = True,
# default = "Asia/Kolkata"
# )
#
# key: str | None = Field(
# description = "The kind of list that was fetched.",
# frozen = True,
# default = None,
# examples = ["NIFTY", "BANKNIFTY", "SME", "FO", "OTHERS", "ALL"]
# )
#
# advances: int = Field(
# description = "The no. of symbols that will be opening positive.",
# frozen = True
# )
#
# declines: int = Field(
# description = "The no. of symbols that will be opening negative.",
# frozen = True
# )
#
# unchanged: int = Field(
# description = "The no. of symbols that will be opening flat.",
# frozen = True
# )
#
# totalMarketCap: float | int | None = Field(
# description = "The total market cap. of all of the listed symbols.",
# frozen = True
# )
#
# totalTradedValue: float | int | None = Field(
# description = "The total value in Rupees that was traded in this pre-market session.",
# frozen = True
# )
#
# totalTradedVolume: int | None = Field(
# description = "The total volume that was traded in the pre-market session.",
# frozen = True
# )
#
# symbols: List[NSEPreMarketSymbol] = Field(
# description = "The actual symbol-wise data.",
# frozen = True
# )
#
# # ┏┓ ┏•
# # ┃ ┏┓┏┓╋┓┏┓
# # ┗┛┗┛┛┗┛┗┗┫
# # ┛
#
# class Config:
# extra = "ignore"
#
# # ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# # ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# # ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# # ┛
#
# @computed_field()
# def dateIst(self) -> str:
# return date_time.to_timezone(self.ts, timezone = date_time.TIMEZONE_IST).strftime("%Y%m%d")
#
# # ┓┏ ┓• ┓ •
# # ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# # ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
#
# @field_validator("scrapeTs", "ts", mode = "before")
# def parse_date_time(cls, value):
# if isinstance(value, datetime.datetime):
# value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
# return value
#
# # ┏┓ ┏┓
# # ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# # ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
#
# def to_json(self) -> dict:
# model_json = self.model_dump()
# model_json["scrapeTs"] = model_json["scrapeTs"].timestamp()
# for s in model_json["symbols"]: s["scrapeTs"].timestamp()
# return model_json
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+261
View File
@@ -0,0 +1,261 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Thursday, 9th Jan., 2025.
OBJECTIVE:
To provide data models for NSE's calendar events.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, AwareDatetime, computed_field
from typing import Optional, Literal, List
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class NSEIPOIssueData(BaseModel):
title: str | None = Field(
description = "A heading for this detail.",
frozen = True
)
value: str | None = Field(
description = "The content under the heading of this detail.",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("title", "value", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, str):
value = value.strip()
if len(value) == 0: value = None
else: value = None
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
return self.model_dump()
# ---------------------------------------------------------------------------------------------------------------------
class NSEIPO(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = False,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
issueStartTs: AwareDatetime = Field(
description = "The start date (UTC) of the issue.",
frozen = True
)
issueEndTs: AwareDatetime = Field(
description = "The end date (UTC) of the issue.",
frozen = True
)
listingTs: AwareDatetime | None = Field(
description = "The date when the company was listed on the exchange.",
frozen = True,
default = None
)
tz: str = Field(
description = "The timezone in which the dates have to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
symbol: str = Field(
description = "The trading symbol of this listing.",
frozen = True
)
name: str = Field(
description = "The name of the parent company of this listing.",
frozen = True
)
status: Literal["Past", "Active", "Forthcoming"] | str = Field(
description = "Whether this IPO is active or has closed.",
frozen = True,
default = "Past"
)
kind: str | None = Field(
description = "The category under which this IPO falls.",
frozen = True,
default = None,
examples = ["SME", "EQ", "DEBT"]
)
upperBand: float | None = Field(
description = "The upper price of the price band.",
frozen = True,
default = None
)
lowerBand: float | None = Field(
description = "The lower price of the price band.",
frozen = True,
default = None
)
issuePrice: float | None = Field(
description = "The final price at which the shares were sold.",
frozen = True,
default = None
)
offerQty: int | None = Field(
description = "How many shares are being offered for sale by the parent.",
frozen = True,
default = None
)
bidQty: int | None = Field(
description = "How many shares investors are willing to buy.",
frozen = True,
default = None
)
bidFactor: float | None = Field(
description = "How many times over has the IPO been subscribed.",
frozen = True,
default = None
)
# ┏┓ ┓ ┓• • ┓ ┳ ┳ ┏
# ┣┫┏┫┏┫┓╋┓┏┓┏┓┏┓┃ ┃┏┏┓┏┏┓ ┃┏┓╋┏┓
# ┛┗┗┻┗┻┗┗┗┗┛┛┗┗┻┗ ┻┛┛┗┻┗ ┻┛┗┛┗┛
issueInfo: List[NSEIPOIssueData] | None = Field(
description = "Additional information related to the issue.",
frozen = False,
default = None
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
pass
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("scrapeTs", "issueStartTs", "issueEndTs", "listingTs", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
return self.model_dump()
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+300
View File
@@ -0,0 +1,300 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Monday, 13th Jan., 2025.
OBJECTIVE:
To provide data model(s) for NSE's pre-market data. This will be useful for catching gap-ups and gap-downs.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For making data behaviour_models:
from pydantic import BaseModel, Field, field_validator, AwareDatetime, computed_field
from typing import Optional, Literal, List
# My utils:
from utils_v2.string import regex
from utils_v2.date_time import date_time
# To work with date and time:
import datetime
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
class NSEPreMarketSymbol(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
tz: str = Field(
description = "The timezone in which the dates have to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
symbol: str = Field(
description = "The trading symbol of this listing.",
frozen = True
)
ffmc: float | None = Field(
description = "The free-floating market cap. of this symbol.",
frozen = True
)
trigger: str | None = Field(
description = "Any known trigger.",
frozen = True,
default = None
)
yearHigh: float | None = Field(
description = "The year-high value of this symbol.",
frozen = True
)
yearLow: float | None = Field(
description = "The year-low value of this symbol.",
frozen = True
)
prevClose: float | None = Field(
description = "The previous trading session's closing price of this symbol.",
frozen = True
)
preMarketPrice: float | None = Field(
description = "The pre-market decided price of this symbol.",
frozen = True
)
chg: float | None = Field(
description = "The absolute change between last closing price and the pre-market price.",
frozen = True
)
pChg: float | None = Field(
description = "The percent change between last closing price and the pre-market price.",
frozen = True
)
totalTradedVolume: int | None = Field(
description = "The total volume that was traded in the pre-market session.",
frozen = True
)
totalBuyVolume: int | None = Field(
description = "The total buying volume that was punched-in in the pre-market session.",
frozen = True
)
totalSellVolume: int | None = Field(
description = "The total selling volume that was punched-in in the pre-market session.",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
@computed_field()
def dateIst(self) -> str:
return date_time.to_timezone(self.scrapeTs, timezone = date_time.TIMEZONE_IST).strftime("%Y%m%d")
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("scrapeTs", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
model_json = self.model_dump()
model_json["scrapeTs"] = model_json["scrapeTs"].timestamp()
return model_json
# ---------------------------------------------------------------------------------------------------------------------
class NSEPreMarketData(BaseModel):
scrapeTs: AwareDatetime = Field(
description = "The time (UTC) at which this this data was scraped.",
frozen = True,
default_factory = lambda: date_time.get_current_utc_date_time(as_string = False)
)
ts: AwareDatetime = Field(
description = "The time (UTC) at which this data was made available by NSE.",
frozen = True
)
tz: str = Field(
description = "The timezone in which the dates have to be interpreted.",
frozen = True,
default = "Asia/Kolkata"
)
key: str | None = Field(
description = "The kind of list that was fetched.",
frozen = True,
default = None,
examples = ["NIFTY", "BANKNIFTY", "SME", "FO", "OTHERS", "ALL"]
)
advances: int = Field(
description = "The no. of symbols that will be opening positive.",
frozen = True
)
declines: int = Field(
description = "The no. of symbols that will be opening negative.",
frozen = True
)
unchanged: int = Field(
description = "The no. of symbols that will be opening flat.",
frozen = True
)
totalMarketCap: float | int | None = Field(
description = "The total market cap. of all of the listed symbols.",
frozen = True
)
totalTradedValue: float | int | None = Field(
description = "The total value in Rupees that was traded in this pre-market session.",
frozen = True
)
totalTradedVolume: int | None = Field(
description = "The total volume that was traded in the pre-market session.",
frozen = True
)
symbols: List[NSEPreMarketSymbol] = Field(
description = "The actual symbol-wise data.",
frozen = True
)
# ┏┓ ┏•
# ┃ ┏┓┏┓╋┓┏┓
# ┗┛┗┛┛┗┛┗┗┫
# ┛
class Config:
extra = "ignore"
# ┏┓ ┏┓ ┓ ┏┓• ┓ ┓
# ┣┫┓┏╋┏┓━━┃ ┏┓┏┳┓┏┓┓┏╋┏┓┏┫ ┣ ┓┏┓┃┏┫┏
# ┛┗┗┻┗┗┛ ┗┛┗┛┛┗┗┣┛┗┻┗┗ ┗┻ ┻ ┗┗ ┗┗┻┛
# ┛
@computed_field()
def dateIst(self) -> str:
return date_time.to_timezone(self.ts, timezone = date_time.TIMEZONE_IST).strftime("%Y%m%d")
# ┓┏ ┓• ┓ •
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓┏
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗┛
@field_validator("scrapeTs", "ts", mode = "before")
def parse_date_time(cls, value):
if isinstance(value, datetime.datetime):
value = date_time.to_timezone(value, date_time.TIMEZONE_UTC)
return value
# ┏┓ ┏┓
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
def to_json(self) -> dict:
model_json = self.model_dump()
model_json["scrapeTs"] = model_json["scrapeTs"].timestamp()
for s in model_json["symbols"]: s["scrapeTs"].timestamp()
return model_json
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass