Skip to content

Commit 03aa8db

Browse files
committed
api retry;fix tick push
1 parent 76535d3 commit 03aa8db

File tree

8 files changed

+65
-27
lines changed

8 files changed

+65
-27
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 2.2.9 (2023-02-09)
2+
### Fix
3+
- 修复tick数据推送报错的问题
4+
### Modify
5+
- 除下单/改单/撤单外的接口,增加重试机制。默认重试5次,可通过 client_config.retry_max_tries 参数修改,设置为0则不进行重试
6+
7+
18
## 2.2.8 (2023-02-03)
29
### Fix
310
- 修复期权合约四要素解析工具无法处理部分港股期权的问题

requirements.txt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
simplejson==3.17.6
2-
delorean==1.0.0
1+
simplejson
2+
delorean
33
pandas
4-
python-dateutil==2.8.2
5-
pytz==2022.1
6-
pyasn1==0.4.8
7-
rsa==4.8
8-
stomp.py==8.0.1
9-
getmac==0.8.3
10-
cryptography==37.0.2
4+
python-dateutil
5+
pytz
6+
pyasn1
7+
rsa
8+
stomp.py
9+
getmac
10+
cryptography
11+
backoff

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
'Programming Language :: Python',
3232
'Operating System :: Microsoft :: Windows',
3333
'Operating System :: Unix',
34-
'Programming Language :: Python :: 3.4',
3534
'Programming Language :: Python :: 3.5',
3635
'Programming Language :: Python :: 3.6',
3736
'Programming Language :: Python :: 3.7',
3837
'Programming Language :: Python :: 3.8',
3938
'Programming Language :: Python :: 3.9',
4039
'Programming Language :: Python :: 3.10',
40+
'Programming Language :: Python :: 3.11',
4141
],
4242
)

tigeropen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
55
@author: gaoan
66
"""
7-
__VERSION__ = '2.2.8'
7+
__VERSION__ = '2.2.9'

tigeropen/push/push_client.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,16 @@ def _convert_tick(self, tick):
512512
symbol = data.pop('symbol')
513513
data = camel_to_underline_obj(data)
514514
price_offset = 10 ** data.pop('price_offset')
515-
price_base = data.pop('price_base')
515+
price_base = float(data.pop('price_base'))
516+
data['timestamp'] = int(data.get('timestamp'))
517+
prices = data.pop('prices') if 'prices' in data else data.pop('price')
516518
# The latter time is equal to the sum of all previous values
517-
price_items = [('price', (item + price_base) / price_offset) for item in data.pop('prices')]
518-
time_items = [('time', item) for item in accumulate(data.pop('times'))]
519-
volumes = [('volume', item) for item in data.pop('volumes')]
520-
tick_types = data.pop('tick_type') if 'tick_type' in data else None
519+
price_items = [('price', (float(item) + price_base) / price_offset) for item in prices]
520+
times = [int(i) for i in (data.pop('times') if 'times' in data else data.pop('time'))]
521+
time_items = [('time', item) for item in accumulate(times)]
522+
volumes = data.pop('volumes') if 'volumes' in data else data.pop('volume')
523+
volume_items = [('volume', int(item)) for item in volumes]
524+
tick_types = data.pop('tick_type') if 'tick_type' in data else data.pop('type')
521525
if tick_types:
522526
tick_type_items = [('tick_type', item) for item in tick_types]
523527
else:
@@ -535,14 +539,14 @@ def _convert_tick(self, tick):
535539
cond_items = [('cond', get_trade_condition(item, cond_map)) for item in conds]
536540
else:
537541
cond_items = [('cond', None) for _ in range(len(time_items))]
538-
sn = data.pop('sn')
542+
sn = int(data.pop('sn'))
539543
sn_list = [('sn', sn + i) for i in range(len(time_items))]
540544
merged_vols = data.pop('merged_vols') if 'merged_vols' in data else None
541545
if merged_vols:
542546
merged_vols_items = [('merged_vols', item) for item in merged_vols]
543547
else:
544548
merged_vols_items = [('merged_vols', None) for _ in range(len(time_items))]
545-
tick_data = zip_longest(tick_type_items, price_items, volumes, part_code_items,
549+
tick_data = zip_longest(tick_type_items, price_items, volume_items, part_code_items,
546550
part_code_name_items, cond_items, time_items, sn_list, merged_vols_items)
547551
items = []
548552
for item in tick_data:

tigeropen/quote/domain/filter.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,26 @@ def _min_max(self, k):
6060

6161

6262
class SortFilterData:
63-
def __init__(self, field, sort_dir):
63+
def __init__(self, field, sort_dir, period=None):
64+
"""
65+
66+
:param field: filter field, subclass of tigeropen.common.consts.filter_fields.FilterField
67+
:param sort_dir: tigeropen.common.consts.SortDirection
68+
:param period: tigeropen.common.consts.filter_fields.AccumulateField
69+
"""
6470
# 排序属性
6571
self.field = field
6672
# SortDir 排序方向,默认不排序
6773
self.sort_dir = sort_dir
74+
# 时间周期 AccumulatePeriod非必传项-只有排序为ACC相关字段,需要此字段
75+
self.period = period
6876

6977
def to_dict(self):
7078
return {
7179
'field_name': self.field.index,
7280
'field_type': self.field.field_type_request_name,
73-
'sort_dir': self.sort_dir.value
81+
'sort_dir': self.sort_dir.value,
82+
'period': self.period.value
7483
}
7584

7685

@@ -83,8 +92,8 @@ def __init__(self, field, filter_min=None, filter_max=None, is_no_filter=False,
8392
:param filter_min: Lower limit of the interval (closed interval), if not pass, means negative infinity
8493
:param filter_max: Upper limit of the interval (closed interval), if not pass, means positive infinity
8594
:param is_no_filter: is enable filter for this field
86-
:param accumulate_period:
87-
:param financial_period:
95+
:param accumulate_period: tigeropen.common.consts.filter_fields.AccumulateField
96+
:param financial_period: tigeropen.common.consts.filter_fields.FinancialPeriod
8897
:param tag_list:
8998
"""
9099
self.field = field

tigeropen/tiger_open_client.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
import logging
1010
import uuid
1111

12+
import backoff
13+
1214
from tigeropen import __VERSION__
1315
from tigeropen.common.consts import OPEN_API_SERVICE_VERSION, THREAD_LOCAL
1416
from tigeropen.common.consts.params import P_TIMESTAMP, P_TIGER_ID, P_METHOD, P_CHARSET, P_VERSION, P_SIGN_TYPE, \
1517
P_DEVICE_ID, P_NOTIFY_URL, COMMON_PARAM_KEYS, P_SIGN
16-
from tigeropen.common.consts.service_types import USER_LICENSE
18+
from tigeropen.common.consts.service_types import USER_LICENSE, PLACE_ORDER, CANCEL_ORDER, MODIFY_ORDER
1719
from tigeropen.common.exceptions import ResponseException, RequestException
1820
from tigeropen.common.request import OpenApiRequest
1921
from tigeropen.common.response import TigerResponse
@@ -28,6 +30,7 @@ def get_mac_address():
2830
return ':'.join(("%012x" % uuid.getnode())[i:i + 2] for i in range(0, 12, 2))
2931

3032
LOG_FORMATTER = logging.Formatter('%(asctime)s %(name)s %(levelname)s: %(message)s')
33+
SKIP_RETRY_SERVICES = {PLACE_ORDER, CANCEL_ORDER, MODIFY_ORDER}
3134

3235

3336
class TigerOpenClient:
@@ -153,20 +156,32 @@ def __parse_response(self, response_str, timestamp=None):
153156

154157
return response_content
155158

159+
def _get_retry_deco(self, service):
160+
if service not in SKIP_RETRY_SERVICES and self.__config.retry_max_tries > 0:
161+
return backoff.on_exception(backoff.fibo,
162+
(RequestException, ResponseException),
163+
max_time=self.__config.retry_max_time,
164+
max_tries=self.__config.retry_max_tries,
165+
jitter=None)
166+
return None
167+
156168
"""
157169
执行接口请求
158170
"""
159-
160171
def execute(self, request, url=None):
161172
if url is None:
162173
url = self.__config.server_url
163174
THREAD_LOCAL.uuid = str(uuid.uuid1())
164175
query_string = None
165176
params = self.__prepare_request(request, url)
166177

167-
response = do_post(url, query_string, self.__headers, params, self.__config.timeout,
168-
self.__config.charset)
169-
178+
retry_deco = self._get_retry_deco(request._method)
179+
if retry_deco is not None:
180+
response = retry_deco(do_post)(url, query_string, self.__headers, params, self.__config.timeout,
181+
self.__config.charset)
182+
else:
183+
response = do_post(url, query_string, self.__headers, params, self.__config.timeout,
184+
self.__config.charset)
170185
return self.__parse_response(response, params.get('timestamp'))
171186

172187
def query_license(self):

tigeropen/tiger_open_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def __init__(self, sandbox_debug=False, enable_dynamic_domain=True):
9494

9595
self.log_level = None
9696
self.log_path = None
97+
self.retry_max_time = 60
98+
self.retry_max_tries = 5
9799

98100
@property
99101
def tiger_id(self):

0 commit comments

Comments
 (0)