Skip to content

Commit

Permalink
Add date support to traders_type (#13)
Browse files Browse the repository at this point in the history
* add date to traders_type.
* add datetime to price overview.
  • Loading branch information
mahs4d authored Jun 13, 2023
1 parent d9bc281 commit 450961f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 72 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Tsetmc sometimes returns 403 and you should retry.
- [x] Migrate `day_details` component to use new tsetmc.
- [x] Migrate `market_map` component to use new tsetmc.
- [x] Migrate `group` component to use new tsetmc.
- [ ] Support asyncio.

## Support and Donation

Expand Down
1 change: 1 addition & 0 deletions lib/tsetmc_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION = '5.1.0'
50 changes: 24 additions & 26 deletions lib/tsetmc_api/symbol/_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ast
import locale
from datetime import datetime

import requests
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -53,6 +54,8 @@ def get_symbol_price_overview(symbol_id: str) -> dict:

# price section
data = all_sections[0].split(',')
gregorian_datetime = datetime.strptime(f"{data[12]}{data[0]}", '%Y%m%d%H:%M:%S')
jalali_datetime = jdatetime.fromgregorian(datetime=gregorian_datetime)
price_data = {
'last': int(data[2]),
'close': int(data[3]),
Expand Down Expand Up @@ -137,6 +140,7 @@ def get_symbol_price_overview(symbol_id: str) -> dict:
})

return {
'datetime': jalali_datetime,
'price_data': price_data,
'orderbook_data': orderbook,
'traders_type_data': traders_type,
Expand Down Expand Up @@ -316,53 +320,47 @@ def get_symbol_id_details(symbol_id: str) -> dict:

def get_symbol_traders_type_history(symbol_id: str) -> list[dict]:
response = requests.get(
url='http://old.tsetmc.com/tsev2/data/clienttype.aspx',
url=f'http://cdn.tsetmc.com/api/ClientType/GetClientTypeHistory/{symbol_id}',
params={
'i': symbol_id,
},
headers={
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
},
verify=False,
timeout=20,
)
response.raise_for_status()
response = response.text
response = response.json()

traders_type_history = []
raw_data = response.split(';')
raw_data = response['clientType']
for row in raw_data:
(
dt,
r_buy_c, l_buy_c, r_sell_c, l_sell_c,
r_buy_v, l_buy_v, r_sell_v, l_sell_v,
r_buy_vl, l_buy_vl, r_sell_vl, l_sell_vl
) = row.split(',')
gregorian_date = datetime.strptime(str(row['recDate']), '%Y%m%d').date()
traders_type_history.append({
'date': jdate.fromgregorian(
year=int(dt[:4]),
month=int(dt[4:6]),
day=int(dt[6:]),
),
'date': jdate.fromgregorian(date=gregorian_date),
'legal': {
'buy': {
'value': l_buy_vl,
'volume': l_buy_v,
'count': l_buy_c,
'value': row['buy_N_Value'],
'volume': row['buy_N_Volume'],
'count': row['buy_N_Count'],
},
'sell': {
'value': l_sell_vl,
'volume': l_sell_v,
'count': l_sell_c,
'value': row['sell_N_Value'],
'volume': row['sell_N_Volume'],
'count': row['sell_N_Count'],
}
},
'real': {
'buy': {
'value': r_buy_vl,
'volume': r_buy_v,
'count': r_buy_c,
'value': row['buy_I_Value'],
'volume': row['buy_I_Volume'],
'count': row['buy_I_Count'],
},
'sell': {
'value': r_sell_vl,
'volume': r_sell_v,
'count': r_sell_c,
'value': row['sell_I_Value'],
'volume': row['sell_I_Volume'],
'count': row['sell_I_Count'],
}
},
})
Expand Down
7 changes: 5 additions & 2 deletions lib/tsetmc_api/symbol/price.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from jdatetime import date as jdate
from jdatetime import time as jtime
from jdatetime import date as jdate, datetime as jdatetime, time as jtime
from pydantic import BaseModel

from .group import SymbolGroupDataRow
Expand Down Expand Up @@ -27,11 +26,15 @@ class Config:


class SymbolPriceOverview(BaseModel):
datetime: jdatetime
price_data: SymbolPriceData
orderbook: SymbolOrderBookData
traders_type: SymbolTradersTypeDataRow
group_data: list[SymbolGroupDataRow]

class Config:
arbitrary_types_allowed = True


class SymbolIntraDayPriceChartDataRow(BaseModel):
time: jtime
Expand Down
3 changes: 3 additions & 0 deletions lib/tsetmc_api/symbol/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def get_price_overview(self) -> SymbolPriceOverview:
)

traders_type = SymbolTradersTypeDataRow(
date=raw_data['datetime'].date(),
legal=SymbolTradersTypeInfo(
buy=SymbolTradersTypeSubInfo(
count=raw_data['traders_type_data']['legal']['buy']['count'],
Expand Down Expand Up @@ -85,6 +86,7 @@ def get_price_overview(self) -> SymbolPriceOverview:
) for row in raw_data['group_data']]

return SymbolPriceOverview(
datetime=raw_data['datetime'],
price_data=tick,
orderbook=orderbook,
traders_type=traders_type,
Expand Down Expand Up @@ -212,6 +214,7 @@ def get_traders_type_history(self) -> list[SymbolTradersTypeDataRow]:
raw_data = _core.get_symbol_traders_type_history(symbol_id=self.symbol_id)

traders_type_history = [SymbolTradersTypeDataRow(
date=row['date'],
legal=SymbolTradersTypeInfo(
buy=SymbolTradersTypeSubInfo(
count=row['legal']['buy']['count'],
Expand Down
5 changes: 5 additions & 0 deletions lib/tsetmc_api/symbol/traders_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from jdatetime import date as jdate
from pydantic import BaseModel


Expand All @@ -13,5 +14,9 @@ class SymbolTradersTypeInfo(BaseModel):


class SymbolTradersTypeDataRow(BaseModel):
date: jdate
legal: SymbolTradersTypeInfo
real: SymbolTradersTypeInfo

class Config:
arbitrary_types_allowed = True
86 changes: 43 additions & 43 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.0.5"
version = "5.1.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 450961f

Please sign in to comment.