Skip to content

Commit

Permalink
Allow returning multiple thresholds in day details (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mahs4d committed Jun 14, 2023
1 parent 0764b79 commit 72ab172
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
1 change: 0 additions & 1 deletion lib/tsetmc_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
VERSION = '5.1.0'
11 changes: 6 additions & 5 deletions lib/tsetmc_api/day_details/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def get_day_details_traders_type_data(symbol_id: str, date: jdate) -> dict:
}


def get_day_details_thresholds_data(symbol_id: str, date: jdate) -> dict:
def get_day_details_thresholds_data(symbol_id: str, date: jdate) -> list[dict]:
t = date.togregorian().strftime('%Y%m%d')
response = requests.get(
url=f'http://cdn.tsetmc.com/api/MarketData/GetStaticThreshold/{symbol_id}/{t}',
Expand All @@ -195,10 +195,11 @@ def get_day_details_thresholds_data(symbol_id: str, date: jdate) -> dict:
response.raise_for_status()
response = response.json()['staticThreshold']

return {
'max': response[1]['psGelStaMax'],
'min': response[1]['psGelStaMin'],
}
return [{
'time': convert_heven_to_jtime(rsp['hEven']),
'max': rsp['psGelStaMax'],
'min': rsp['psGelStaMin'],
} for rsp in response]


def get_day_details_shareholders_data(symbol_id: str, date: jdate) -> tuple[list[dict], list[dict]]:
Expand Down
12 changes: 7 additions & 5 deletions lib/tsetmc_api/day_details/day_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ def get_trades_data(self, summarize: bool = False) -> list[DayDetailsTradeDataRo
volume=row['volume'],
) for row in raw_data]

def get_thresholds_data(self) -> DayDetailsThresholdsData:
def get_thresholds_data(self) -> list[DayDetailsThresholdsData]:
raw_data = _core.get_day_details_thresholds_data(symbol_id=self.symbol_id, date=self.date)
return DayDetailsThresholdsData(
range_max=raw_data['max'],
range_min=raw_data['min'],
)

return [DayDetailsThresholdsData(
time=row['time'],
range_max=row['max'],
range_min=row['min'],
) for row in raw_data]

def get_shareholders_data(self) -> tuple[list[DayDetailsShareHolderDataRow], list[DayDetailsShareHolderDataRow]]:
"""
Expand Down
5 changes: 5 additions & 0 deletions lib/tsetmc_api/day_details/threshold.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from jdatetime import time as jtime
from pydantic import BaseModel


class DayDetailsThresholdsData(BaseModel):
time: jtime | None
range_max: int
range_min: int

class Config:
arbitrary_types_allowed = True
6 changes: 4 additions & 2 deletions lib/tsetmc_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def deep_update(d1: dict, d2: dict) -> dict:
return ret


def convert_heven_to_jtime(heven: int) -> jtime:
def convert_heven_to_jtime(heven: int | str) -> jtime | None:
heven = str(heven)
if len(heven) == 6:
return jtime(hour=int(heven[:2]), minute=int(heven[2:4]), second=int(heven[4:]))
else:
elif len(heven) == 5:
return jtime(hour=int(heven[:1]), minute=int(heven[1:3]), second=int(heven[3:]))
else:
return None


def convert_deven_to_jdate(deven: int) -> jdate:
Expand Down
1 change: 1 addition & 0 deletions lib/tsetmc_api/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION = '5.2.0'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tsetmc-api"
version = "5.1.0"
version = "5.2.0"
description = "simple package to communicate and crawl data from tsetmc.com (Tehran Stock Exchange Website)"
license = "MIT"
repository = "https://github.com/mahs4d/tsetmc-api"
Expand Down

0 comments on commit 72ab172

Please sign in to comment.