Skip to content

Commit

Permalink
Merge pull request #223 from Glyphack/add-instrument-info-api-and-upd…
Browse files Browse the repository at this point in the history
…ate-title

get title from instrument info api
  • Loading branch information
sfmqrb authored Sep 24, 2023
2 parents 943a292 + ebb770d commit 511b1cc
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 9 deletions.
51 changes: 51 additions & 0 deletions pytse_client/ticker/api_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from dataclasses import dataclass
from typing import List, Optional, Tuple

from pytse_client import tse_settings, utils

from pytse_client.ticker.dto import InstrumentInfo


@dataclass
class Order:
Expand Down Expand Up @@ -76,3 +80,50 @@ def get_corporate_trade_summary(corporate_trade_summary_section):
sell_vol=corporate_sell_vol,
sell_count=corporate_sell_count,
)


def get_instrument_info(ticket_id: str) -> InstrumentInfo:
resp = utils.requests_retry_session().get(
tse_settings.TSE_INSTRUMENT_INFO.format(ticket_id),
timeout=10,
headers=tse_settings.HEADERS,
)
resp.raise_for_status()
data = resp.json()["instrumentInfo"]
return InstrumentInfo(
eps=data["eps"],
sector=data["sector"],
static_threshold=data["staticThreshold"],
min_week=data["minWeek"],
max_week=data["maxWeek"],
min_year=data["minYear"],
max_year=data["maxYear"],
q_tot_tran5_j_avg=data["qTotTran5JAvg"],
k_aj_cap_val_cps_idx=data["kAjCapValCpsIdx"],
d_even=data["dEven"],
top_inst=data["topInst"],
fara_desc=data["faraDesc"],
contract_size=data["contractSize"],
nav=data["nav"],
under_supervision=data["underSupervision"],
c_val_mne=data["cValMne"],
l_val18=data["lVal18"],
c_soc_csac=data["cSocCSAC"],
l_soc30=data["lSoc30"],
y_mar_nsc=data["yMarNSC"],
y_val=data["yVal"],
ins_code=data["insCode"],
l_val30=data["lVal30"],
l_val18_afc=data["lVal18AFC"],
flow=data["flow"],
c_isin=data["cIsin"],
z_titad=data["zTitad"],
base_vol=data["baseVol"],
instrument_id=data["instrumentID"],
cgr_val_cot=data["cgrValCot"],
c_com_val=data["cComVal"],
last_date=data["lastDate"],
source_id=data["sourceID"],
flow_title=data["flowTitle"],
cgr_val_cot_title=data["cgrValCotTitle"],
)
40 changes: 40 additions & 0 deletions pytse_client/ticker/dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from dataclasses import dataclass


@dataclass
class InstrumentInfo:
eps: dict
sector: dict
static_threshold: dict
min_week: float
max_week: float
min_year: float
max_year: float
q_tot_tran5_j_avg: float
k_aj_cap_val_cps_idx: str
d_even: float
top_inst: float
fara_desc: str
contract_size: float
nav: float
under_supervision: float
c_val_mne: str
l_val18: str
c_soc_csac: str
l_soc30: str
y_mar_nsc: str
y_val: str
ins_code: str
l_val30: str
l_val18_afc: str
flow: float
c_isin: str
z_titad: float
base_vol: float
instrument_id: str
cgr_val_cot: str
c_com_val: str
last_date: float
source_id: float
flow_title: str
cgr_val_cot_title: str
11 changes: 5 additions & 6 deletions pytse_client/ticker/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
TradeSummary,
get_corporate_trade_summary,
get_individual_trade_summary,
get_instrument_info,
get_orders,
)
from pytse_client.tse_settings import (
Expand Down Expand Up @@ -101,6 +102,8 @@ def __init__(
f"{config.DATA_BASE_PATH}/{self.symbol}-ت.csv"
)
self._url = tse_settings.TSE_TICKER_ADDRESS.format(self._index)
# this URL sholud be used from now on for features because old one is deprecated
self._url_new = tse_settings.TSE_INSTRUMENT_INFO.format(self._index)
self._info_url = tse_settings.TSE_ISNT_INFO_URL.format(self._index)
self._client_types_url = TSE_CLIENT_TYPE_DATA_URL.format(self._index)
# TODO: if symbol equals instrument id, it cannot fetch introduction
Expand Down Expand Up @@ -183,19 +186,15 @@ def title(self) -> str:
Symbol title in persian
"""
return replace_arabic(
re.findall(r"Title='(.*?)',", self._ticker_page_response.text)[
0
].split("-")[0]
get_instrument_info(self._index).l_val30.split("-")[0]
)

@property
def fulltitle(self) -> str:
"""
Symbol title with it's market in persian
"""
return replace_arabic(
re.findall(r"Title='(.*?)',", self._ticker_page_response.text)[0]
)
return replace_arabic(get_instrument_info(self._index).l_val30)

@property
def group_name(self) -> str:
Expand Down
15 changes: 15 additions & 0 deletions pytse_client/tse_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@


TSE_TICKER_ADDRESS = "http://old.tsetmc.com/Loader.aspx?ParTree=151311&i={}"
TSE_INSTRUMENT_INFO = (
"http://cdn.tsetmc.com/api/Instrument/GetInstrumentInfo/{}"
)

# FIXME: c should be cSecVal (group code of instrument)
# remove e parameter when instrument has not NAV value
Expand Down Expand Up @@ -75,3 +78,15 @@

DATE_FORMAT = "%Y%m%d"
MIN_DATE = 20010321

HEADERS = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
"Accept-Language": "en-GB,en;q=0.9",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Cookie": "ASP.NET_SessionId=tieamwajlx5h0zdnwzbodf0a",
"DNT": "1",
"Sec-GPC": "1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/169.0.0.0 Safari/552.36",
}
10 changes: 7 additions & 3 deletions tests/test_ticker/test_ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@

class TestTicker(unittest.TestCase):
def setUp(self) -> None:
# This can break the test if symbol changes state
self.deactivated_symbol = "رتکو"
self.activate_symbol = "ذوب"
return super().setUp()

def tearDown(self) -> None:
return super().tearDown()

def test_option_trading(self):
ticker = Ticker("", "816070469779688")
ticker.title
ticker.fulltitle

def test_real_time_info_will_not_fail_with_deactivated_symbol(self):
ticker = Ticker("", index="3823243780502959")
self.assertRaises(
RuntimeError,
Ticker(self.deactivated_symbol).get_ticker_real_time_info_response,
ticker.get_ticker_real_time_info_response,
)

def test_get_total_shares_history_on_activate_symbol(self):
Expand Down

0 comments on commit 511b1cc

Please sign in to comment.