Skip to content

Commit

Permalink
optional historical data/stats in watch
Browse files Browse the repository at this point in the history
  • Loading branch information
mahs4d committed Nov 20, 2020
1 parent 0c47a5b commit b678389
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 99 deletions.
180 changes: 94 additions & 86 deletions lib/tsetmc_api/core/watch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from traceback import print_exc

import requests


Expand All @@ -6,62 +8,65 @@ def _extract_prices(raw_section):

rows = raw_section.split(';')
for row in rows:
if row == '':
continue

cols = row.split(',')
if len(cols) in [0, 10]:
continue
try:
if row == '':
continue

cols = row.split(',')
if len(cols) in [0, 10]:
continue

symbol_id = cols[0]
isin = cols[1]
short_name = cols[2]
full_name = cols[3]
open_price = int(cols[5])
close_price = int(cols[6])
last_price = int(cols[7])
count = int(cols[8])
volume = int(cols[9])
value = int(cols[10])
low_price = int(cols[11])
high_price = int(cols[12])
yesterday = int(cols[13])
eps = int(cols[14]) if cols[14] != '' else None
base_volume = int(cols[15])
visit_count = int(cols[16])
flow = int(cols[17])
group = int(cols[18])
max_price = int(float(cols[19]))
min_price = int(float(cols[20]))
z = int(cols[21])
yval = int(cols[22])

symbol_id = cols[0]
isin = cols[1]
short_name = cols[2]
full_name = cols[3]
open_price = int(cols[5])
close_price = int(cols[6])
last_price = int(cols[7])
count = int(cols[8])
volume = int(cols[9])
value = int(cols[10])
low_price = int(cols[11])
high_price = int(cols[12])
yesterday = int(cols[13])
eps = int(cols[14]) if cols[14] != '' else None
base_volume = int(cols[15])
visit_count = int(cols[16])
flow = int(cols[17])
group = int(cols[18])
max_price = int(float(cols[19]))
min_price = int(float(cols[20]))
z = int(cols[21])
yval = int(cols[22])

ret[symbol_id] = {
'symbol_id': symbol_id,
'symbol_short_name': short_name,
'symbol_long_name': full_name,
'isin': isin,
'open': open_price,
'close': close_price,
'last': last_price,
'high': high_price,
'low': low_price,
'count': count,
'volume': volume,
'value': value,
'yesterday': yesterday,
'eps': eps,
'base_volume': base_volume,
'visits_count': visit_count,
'flow': flow,
'group_code': group,
'max': max_price,
'min': min_price,
'z': z,
'yval': yval,
'buy_orders': [],
'sell_orders': [],
}
ret[symbol_id] = {
'symbol_id': symbol_id,
'symbol_short_name': short_name,
'symbol_long_name': full_name,
'isin': isin,
'open': open_price,
'close': close_price,
'last': last_price,
'high': high_price,
'low': low_price,
'count': count,
'volume': volume,
'value': value,
'yesterday': yesterday,
'eps': eps,
'base_volume': base_volume,
'visits_count': visit_count,
'flow': flow,
'group_code': group,
'max': max_price,
'min': min_price,
'z': z,
'yval': yval,
'buy_orders': [],
'sell_orders': [],
}
except:
print_exc()

return ret

Expand All @@ -70,37 +75,40 @@ def _extract_orders(raw_section, watch):
ret = {}
rows = raw_section.split(';')
for row in rows:
if row == '':
continue

cols = row.split(',')

symbol_id = cols[0]
rank = cols[1]
sell_count = cols[2]
buy_count = cols[3]
buy_price = cols[4]
sell_price = cols[5]
buy_volume = cols[6]
sell_volume = cols[7]

ainfo = watch.get(symbol_id, None)
if ainfo is None:
continue
ainfo['buy_orders'].append({
'rank': int(rank),
'count': int(buy_count),
'price': int(buy_price),
'volume': int(buy_volume),
})
ainfo['sell_orders'].append({
'rank': int(rank),
'count': int(sell_count),
'price': int(sell_price),
'volume': int(sell_volume),
})

ret[symbol_id] = ainfo
try:
if row == '':
continue

cols = row.split(',')

symbol_id = cols[0]
rank = cols[1]
sell_count = cols[2]
buy_count = cols[3]
buy_price = cols[4]
sell_price = cols[5]
buy_volume = cols[6]
sell_volume = cols[7]

ainfo = watch.get(symbol_id, None)
if ainfo is None:
continue
ainfo['buy_orders'].append({
'rank': int(rank),
'count': int(buy_count),
'price': int(buy_price),
'volume': int(buy_volume),
})
ainfo['sell_orders'].append({
'rank': int(rank),
'count': int(sell_count),
'price': int(sell_price),
'volume': int(sell_volume),
})

ret[symbol_id] = ainfo
except:
print_exc()

return ret

Expand Down
43 changes: 30 additions & 13 deletions lib/tsetmc_api/watch.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,52 @@
import time
from abc import ABC, abstractmethod
from traceback import print_exc
from typing import List, Optional

import schedule

from .core import watch as watch_core


class WatchTick:
def __init__(self, price_data, client_type_data, historical_data, stats_data):
def __init__(self,
price_data: dict,
client_type_data: dict,
historical_data: Optional[List[dict]],
stats_data: Optional[dict]):
self.price_data = price_data
self.client_type_data = client_type_data
self.historical_data = historical_data
self.stats_data = stats_data

def get_symbol_ids(self):
def get_symbol_ids(self) -> List[str]:
"""
لیست آی دی همه‌ی نمادهای داخل دیده‌بان
"""
return self.price_data.keys()
return list(self.price_data.keys())

def get_price_data(self, symbol_id):
def get_price_data(self, symbol_id) -> dict:
"""
اطلاعات قیمتی و سفارشات دیده‌بان
"""
return self.price_data.get(symbol_id, {})

def get_client_type_data(self, symbol_id):
def get_client_type_data(self, symbol_id) -> dict:
"""
اطلاعات حقیقی حقوقی دیده‌بان
"""
return self.client_type_data.get(symbol_id, {})

def get_historical_data(self, symbol_id):
def get_historical_data(self, symbol_id) -> Optional[dict]:
"""
اطلاعات سابقه‌ی دیده‌بان
"""
if self.historical_data is None:
return None

return self.historical_data.get(symbol_id, {})

def get_stats_data(self, symbol_id):
def get_stats_data(self, symbol_id) -> Optional[dict]:
"""
آمارهای کلیدی دیده‌بان
"""
Expand Down Expand Up @@ -82,9 +90,13 @@ class Watch:
def __init__(self,
simple_refresh_time: int = 1,
client_type_refresh_time: int = 60,
historical_data: bool = True,
stats_data: bool = True,
):
self._price_data_rtime = simple_refresh_time
self._client_type_data_rtime = client_type_refresh_time
self._historical_data = historical_data
self._stats_data = stats_data

self._last_price_data = None
self._last_client_type_data = None
Expand All @@ -105,11 +117,13 @@ def prepare_watch(self):
"""
کنترل دستی
"""
while self._last_historical_data is None:
self._update_historical_data()
if self._historical_data:
while self._last_historical_data is None:
self._update_historical_data()

while self._last_stats_data is None:
self._update_stats_data()
if self._stats_data:
while self._last_stats_data is None:
self._update_stats_data()

while self._last_price_data is None:
self._update_price_data()
Expand All @@ -124,8 +138,11 @@ def step_watch(self):
"""
کنترل دستی
"""
schedule.run_pending()
time.sleep(1)
try:
schedule.run_pending()
time.sleep(1)
except:
print_exc()

def start_watch(self):
"""
Expand Down

0 comments on commit b678389

Please sign in to comment.