Skip to content

Commit

Permalink
Merge pull request #11 from mahs4d/bugfix/old-base-url
Browse files Browse the repository at this point in the history
Change `_core` base urls to `old.tsetmc.com`
  • Loading branch information
mahs4d committed May 27, 2023
2 parents 42b3595 + 5b1dd31 commit 7d8170d
Show file tree
Hide file tree
Showing 18 changed files with 496 additions and 241 deletions.
2 changes: 1 addition & 1 deletion LICENSE → LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(The MIT License)

Copyright 2022 Mahdi Sadeghi <[email protected]>
Copyright 2023 Mahdi Sadeghi <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,25 @@ You can install this library using the following command:

`pip install tsetmc-api`

## Examples

You can find examples of using each component in `examples` directory.

| Component | Example File |
|--------------|-------------------------------------------------------------|
| Symbol | [symbol_example.py](examples/symbol_example.py) |
| Market Watch | [market_watch_example.py](examples/market_watch_example.py) |
| Day Details | [day_details_example.py](examples/day_details_example.py) |
| Market Map | [market_map_example.py](examples/market_map_example.py) |
| Group | [group_example.py](examples/group_example.py) |

## Usage

- **symbol:** working with main symbol page and live data (e.g. [this page](http://www.tsetmc.com/loader.aspx?ParTree=151311&i=43362635835198978))
- **symbol:** working with main symbol page and live data (
e.g. [this page](http://www.tsetmc.com/loader.aspx?ParTree=151311&i=43362635835198978))
- **market_watch:** getting data visible from [market watch page](http://www.tsetmc.com/Loader.aspx?ParTree=15131F)
- **day_details:** working with details of a symbol in a single day of history (e.g. [this page](http://cdn.tsetmc.com/History/43362635835198978/20221029))
- **day_details:** working with details of a symbol in a single day of history (
e.g. [this page](http://cdn.tsetmc.com/History/43362635835198978/20221029))
- **market_map:** getting data visible in [market map page](http://main.tsetmc.com/marketmap)
- **group:** getting list of available symbol groups

Expand Down Expand Up @@ -40,6 +54,15 @@ Group component currently only has one function (`get_all_groups`) which returns

Tsetmc sometimes returns 403 and you should retry.

### TODO

[ ] Migrate `symbol` component to use new tsetmc.
[ ] Migrate `market_watch` component to use new tsetmc.
[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 and Donation

If this repository helped you, please support it by giving a star (:star:).
For donation please contact me at [[email protected]](mailto:[email protected]).
30 changes: 30 additions & 0 deletions examples/day_details_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from jdatetime import date as jdate

from tsetmc_api.day_details import DayDetails

# تغییرات نماد در یک روز
day_details = DayDetails(symbol_id='14079693677610396', date=jdate(1402, 3, 1))

# قیمت‌های نهایی
price_overview = day_details.get_price_overview()

# تغییرات قیمتی
price_data = day_details.get_price_data()

# تغییرات صف‌های خرید و فروش
orderbook_data = day_details.get_orderbook_data()

# معاملات
trades_data = day_details.get_trades_data()

# بیشترین و کمترین قیمت روز
thresholds_data = day_details.get_thresholds_data()

# تغییرات سهامداران عمده
old_shareholders, new_shareholders = day_details.get_shareholders_data()

# چارت یک سهامدار
old_shareholders[0].get_chart_data()

# سهام یک سهامدار عمده
old_shareholders[0].shareholder.get_portfolio_data()
4 changes: 4 additions & 0 deletions examples/group_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from tsetmc_api.group import Group

# اطلاعات گروه‌ها
all_groups = Group.get_all_groups()
10 changes: 10 additions & 0 deletions examples/market_map_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from tsetmc_api.market_map import MarketMap, MapType

# نقشه‌ی بازار
market_map = MarketMap()

# گرفتن نقشه‌ی بازار بر اساس ارزش
map_by_value = market_map.get_market_map_data(map_type=MapType.MARKET_VALUE)

# گرفتن نقشه‌ی بازار بر اساس حجم
map_by_volume = market_map.get_market_map_data(map_type=MapType.MARKET_VOLUME)
21 changes: 21 additions & 0 deletions examples/market_watch_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tsetmc_api.market_watch import MarketWatch

# دیده‌بان بازار
# توجه کنید که این اطلاعات از صفحه‌ی دیده‌بان بازاره و مثلا سابقه‌ی اینجا با اون سابقه‌ای که توی صفحه
# نماد می‌بینیم فرق داره. اونا توی پکیج symbol قرار دارند.
market_watch = MarketWatch()

# اطلاعات قیمتی
price_data = market_watch.get_price_data()

# اطلاعات آماری به صورت خام
raw_stats_data = market_watch.get_raw_stats_data()

# اطلاعات آماری به صورت parse شده
stats_data = market_watch.get_stats_data()

# اطلاعات حقیقی حقوقی
traders_type_data = market_watch.get_traders_type_data()

# اطلاعات سابقه
daily_history_data = market_watch.get_daily_history_data()
40 changes: 40 additions & 0 deletions examples/symbol_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from tsetmc_api.symbol import Symbol

# دیدن مشخصات یک نماد (نماد آسیاتک)
# این عددی که به عنوان symbol_id بهش میدیم، از تیکه‌ی آخر url صفحه‌ی آسیاتک توی سایت برداشته شده
# مثلا آدرس آسیاتک توی tsetmc اینه: http://main.tsetmc.com/InstInfo/14079693677610396
# اون تیکه آخرش میشه symbol_id
symbol = Symbol(symbol_id='14079693677610396')

# اطلاعات قیمتی داخل در یک نگاه
price_overview = symbol.get_price_overview()

# چارت داخل در یک نگاه
intraday_price_chart_data = symbol.get_intraday_price_chart_data()

# پیام ناظر
supervisor_message_data = symbol.get_supervisor_messages_data()

# اطلاعیه
notification_data = symbol.get_notifications_data()

# تغییر وضعیت
state_changes_data = symbol.get_state_changes_data()

# سابقه
daily_history = symbol.get_daily_history()

# شناسه
id_details = symbol.get_id_details()

# حقیقی حقوقی
traders_type_history = symbol.get_traders_type_history()

# سهامداران
shareholders = symbol.get_shareholders_data()

# چارتی که وقتی روی یک سهامدار کلیک میکنیم میده
shareholder_chart_data = shareholders[0].get_chart_data()

# سایر سهام سهامدار عمده
shareholder_portfolio_data = shareholders[0].shareholder.get_portfolio_data()
45 changes: 37 additions & 8 deletions lib/tsetmc_api/day_details/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def get_day_details_price_overview(symbol_id: str, date: jdate) -> dict:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/ClosingPrice/GetClosingPriceDaily/{symbol_id}/{t}',
params={},
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,
)
Expand All @@ -37,6 +40,9 @@ def get_day_details_price_data(symbol_id: str, date: jdate) -> list[dict]:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/ClosingPrice/GetClosingPriceHistory/{symbol_id}/{t}',
params={},
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,
)
Expand All @@ -60,6 +66,9 @@ def get_day_details_orderbook_data(symbol_id: str, date: jdate) -> list[dict]:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/BestLimits/{symbol_id}/{t}',
params={},
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,
)
Expand Down Expand Up @@ -114,6 +123,9 @@ def get_day_details_trade_data(symbol_id: str, date: jdate, summarize: bool) ->
response = requests.get(
url=f'http://cdn.tsetmc.com/api/Trade/GetTradeHistory/{symbol_id}/{t}/{summarize_url_ph}',
params={},
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,
)
Expand All @@ -132,6 +144,9 @@ def get_day_details_traders_type_data(symbol_id: str, date: jdate) -> dict:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/ClientType/GetClientTypeHistory/{symbol_id}/{t}',
params={},
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,
)
Expand Down Expand Up @@ -171,6 +186,9 @@ def get_day_details_thresholds_data(symbol_id: str, date: jdate) -> dict:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/MarketData/GetStaticThreshold/{symbol_id}/{t}',
params={},
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,
)
Expand All @@ -188,6 +206,9 @@ def get_day_details_shareholders_data(symbol_id: str, date: jdate) -> tuple[list
response = requests.get(
url=f'http://cdn.tsetmc.com/api/Shareholder/{symbol_id}/{t}',
params={},
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,
)
Expand All @@ -199,10 +220,10 @@ def get_day_details_shareholders_data(symbol_id: str, date: jdate) -> tuple[list
it = int(t)
for row in response:
sh_data = {
'id': str(row['shareHolderId']),
'id': str(row['shareHolderID']),
'name': row['shareHolderName'],
'count': row['numberOfShares'],
'percentage': row['perOfShares'],
'shares_count': row['numberOfShares'],
'shares_percentage': row['perOfShares'],
}

if row['dEven'] < it:
Expand All @@ -217,6 +238,9 @@ def get_shareholder_chart_data(symbol_id: str, shareholder_id: str, days: int) -
response = requests.get(
url=f'http://cdn.tsetmc.com/api/Shareholder/GetShareHolderHistory/{symbol_id}/{shareholder_id}/{days}',
params={},
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,
)
Expand All @@ -225,23 +249,28 @@ def get_shareholder_chart_data(symbol_id: str, shareholder_id: str, days: int) -

return [{
'date': convert_deven_to_jdate(deven=row['dEven']),
'count': row['numberOfShares'],
'percentage': row['perOfShares'],
'shares_count': row['numberOfShares'],
'shares_percentage': row['perOfShares'],
} for row in response]


def get_shareholder_portfolio(shareholder_id: str) -> list[dict]:
response = requests.get(
url=f'http://cdn.tsetmc.com/api/Shareholder/GetShareHolderCompanyList/{shareholder_id}',
params={},
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.json()['shareHolderShare']

return [{
'symbol_id': row['insCodes'],
'short_name': row['lVal18AFC'],
'long_name': row['lVal30'],
'symbol_id': row['instrument']['insCode'],
'short_name': row['instrument']['lVal18AFC'],
'long_name': row['instrument']['lVal30'],
'shares_count': row['numberOfShares'],
'shares_percent': row['perOfShares'],
} for row in response]
8 changes: 6 additions & 2 deletions lib/tsetmc_api/day_details/day_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,19 @@ def get_shareholders_data(self) -> tuple[list[DayDetailsShareHolderDataRow], lis
)

old_shareholders = [DayDetailsShareHolderDataRow(
symbol_id=self.symbol_id,
date=self.date,
shareholder=DayDetailsShareHolder(id=row['id'], name=row['name']),
count=row['count'],
percentage=row['percentage'],
) for row in raw_old_shareholders]

new_shareholders = [DayDetailsShareHolderDataRow(
symbol_id=self.symbol_id,
date=self.date,
shareholder=DayDetailsShareHolder(id=row['id'], name=row['name']),
count=row['count'],
percentage=row['percentage'],
shares_count=row['shares_count'],
shares_percentage=row['shares_percentage'],
) for row in raw_new_shareholders]

return old_shareholders, new_shareholders
16 changes: 10 additions & 6 deletions lib/tsetmc_api/day_details/shareholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class DayDetailsShareHolderPortfolioRow(BaseModel):
symbol_id: str
short_name: str
long_name: str
shares_count: int
shares_percent: float


class DayDetailsShareHolder(BaseModel):
Expand All @@ -25,13 +27,15 @@ def get_portfolio_data(self) -> list[DayDetailsShareHolderPortfolioRow]:
symbol_id=row['symbol_id'],
short_name=row['short_name'],
long_name=row['long_name'],
shares_count=row['shares_count'],
shares_percent=row['shares_percent'],
) for row in raw_data]


class DayDetailsShareHolderChartRow(BaseModel):
date: jdate
count: int
percentage: float
shares_count: int
shares_percentage: float

class Config:
arbitrary_types_allowed = True
Expand All @@ -41,8 +45,8 @@ class DayDetailsShareHolderDataRow(BaseModel):
symbol_id: str
date: jdate
shareholder: DayDetailsShareHolder
count: int
percentage: float
shares_count: int
shares_percentage: float

def get_chart_data(self, days: int = 90) -> list[DayDetailsShareHolderChartRow]:
"""
Expand All @@ -57,8 +61,8 @@ def get_chart_data(self, days: int = 90) -> list[DayDetailsShareHolderChartRow]:

return [DayDetailsShareHolderChartRow(
date=row['date'],
count=row['count'],
percentage=row['percentage'],
shares_count=row['shares_count'],
shares_percentage=row['shares_percentage'],
) for row in raw_data]

class Config:
Expand Down
5 changes: 4 additions & 1 deletion lib/tsetmc_api/group/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ def get_group_static_data() -> list[dict]:
response = requests.get(
url='http://cdn.tsetmc.com/api/StaticData/GetStaticData',
params={},
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,
timeout=20,
)
response.raise_for_status()
response = response.json()['staticData']
Expand Down
3 changes: 3 additions & 0 deletions lib/tsetmc_api/market_map/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def get_market_map_data(map_type: int, heven: int = 0) -> tuple[dict[dict], int]
'typeSelected': map_type,
'hEven': heven,
},
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,
)
Expand Down
Loading

0 comments on commit 7d8170d

Please sign in to comment.