Skip to content

Commit 7441f46

Browse files
committed
modify query subscribed callback
1 parent 377f9c7 commit 7441f46

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

tigeropen/common/consts/push_destinations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
QUOTE_DEPTH = 'quotedepth'
55
QUOTE_FUTURE = 'future'
66
QUOTE_OPTION = 'option'
7-
QUOTE_TICK = 'tradetick'
7+
TRADE_TICK = 'tradetick'
88

99
TRADE_ASSET = 'trade/asset'
1010
TRADE_POSITION = 'trade/position'

tigeropen/common/consts/push_subscriptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
SUBSCRIPTION_QUOTE_DEPTH = 'QuoteDepth'
55
SUBSCRIPTION_QUOTE_FUTURE = 'Future'
66
SUBSCRIPTION_QUOTE_OPTION = 'Option'
7-
SUBSCRIPTION_QUOTE_TICK = 'TradeTick'
7+
SUBSCRIPTION_TRADE_TICK = 'TradeTick'
88

99
SUBSCRIPTION_TRADE_ASSET = 'Asset'
1010
SUBSCRIPTION_TRADE_POSITION = 'Position'

tigeropen/examples/push_client_demo.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"""
77
import time
88
# from tigeropen.common.consts import QuoteKeyType
9+
import pandas as pd
10+
911
from tigeropen.push.push_client import PushClient
1012
from tigeropen.examples.client_config import get_client_config
1113

@@ -58,6 +60,28 @@ def on_quote_changed(symbol, items, hour_trading):
5860
print(symbol, items, hour_trading)
5961

6062

63+
def on_tick_changed(symbol, items):
64+
"""
65+
66+
:param symbol:
67+
:param items:
68+
items example:
69+
[{'tick_type': '*', 'price': 293.87, 'volume': 102, 'part_code': 'NSDQ',
70+
'part_code_name': 'NASDAQ Stock Market, LLC (NASDAQ)', 'cond': 'US_FORM_T', 'time': 1656405615779,
71+
'server_timestamp': 1656405573461, 'type': 'TradeTick', 'quote_level': 'usStockQuote', 'sn': 342,
72+
'timestamp': 1656405617385},
73+
{'tick_type': '*', 'price': 293.87, 'volume': 102, 'part_code': 'NSDQ',
74+
'part_code_name': 'NASDAQ Stock Market, LLC (NASDAQ)', 'cond': 'US_FORM_T', 'time': 1656405616573,
75+
'server_timestamp': 1656405573461,
76+
'type': 'TradeTick', 'quote_level': 'usStockQuote', 'sn': 342, 'timestamp': 1656405617385}]
77+
:return:
78+
"""
79+
print(symbol, items)
80+
# convert to DataFrame
81+
# frame = pd.DataFrame(items)
82+
# print(frame)
83+
84+
6185
def on_order_changed(account, items):
6286
"""
6387
@@ -152,6 +176,8 @@ def disconnect_callback():
152176

153177
# 行情变动回调
154178
push_client.quote_changed = on_quote_changed
179+
# 逐笔数据回调
180+
push_client.tick_changed = on_tick_changed
155181
# 已订阅 symbol 查询回调
156182
push_client.subscribed_symbols = on_query_subscribed_quote
157183
# 订单变动回调
@@ -182,6 +208,9 @@ def disconnect_callback():
182208
# 订阅深度行情
183209
push_client.subscribe_depth_quote(['AMD', 'BABA'])
184210

211+
# 订阅逐笔数据
212+
push_client.subscribe_tick(['AMD', 'QQQ'])
213+
185214
# 订阅资产变动
186215
push_client.subscribe_asset()
187216
# 订阅订单变动

tigeropen/push/push_client.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
from tigeropen.common.consts import OrderStatus
1818
from tigeropen.common.consts.params import P_SDK_VERSION, P_SDK_VERSION_PREFIX
1919
from tigeropen.common.consts.push_destinations import QUOTE, QUOTE_DEPTH, QUOTE_FUTURE, QUOTE_OPTION, TRADE_ASSET, \
20-
TRADE_ORDER, TRADE_POSITION, QUOTE_TICK
20+
TRADE_ORDER, TRADE_POSITION, TRADE_TICK
2121
from tigeropen.common.consts.push_subscriptions import SUBSCRIPTION_QUOTE, SUBSCRIPTION_QUOTE_DEPTH, \
2222
SUBSCRIPTION_QUOTE_OPTION, SUBSCRIPTION_QUOTE_FUTURE, SUBSCRIPTION_TRADE_ASSET, SUBSCRIPTION_TRADE_POSITION, \
23-
SUBSCRIPTION_TRADE_ORDER, SUBSCRIPTION_QUOTE_TICK
23+
SUBSCRIPTION_TRADE_ORDER, SUBSCRIPTION_TRADE_TICK
2424
from tigeropen.common.consts.push_types import RequestType, ResponseType
2525
from tigeropen.common.consts.quote_keys import QuoteChangeKey, QuoteKeyType
2626
from tigeropen.common.exceptions import ApiException
@@ -90,6 +90,7 @@ def __init__(self, host, port, use_ssl=True, connection_timeout=120, heartbeats=
9090
self._destination_counter_map = defaultdict(lambda: 0)
9191

9292
self.subscribed_symbols = None
93+
self.query_subscribed_callback = None
9394
self.quote_changed = None
9495
self.tick_changed = None
9596
self.asset_changed = None
@@ -102,6 +103,7 @@ def __init__(self, host, port, use_ssl=True, connection_timeout=120, heartbeats=
102103
self.error_callback = None
103104
self._connection_timeout = connection_timeout
104105
self._heartbeats = heartbeats
106+
self.logger = logging.getLogger('tiger_openapi')
105107
_patch_ssl()
106108

107109
def _connect(self):
@@ -156,17 +158,29 @@ def on_message(self, frame):
156158
try:
157159
response_type = headers.get('ret-type')
158160
if response_type == str(ResponseType.GET_SUB_SYMBOLS_END.value):
159-
if self.subscribed_symbols:
161+
if self.subscribed_symbols or self.query_subscribed_callback:
160162
data = json.loads(body)
161-
limit = data.get('limit')
162-
symbols = data.get('subscribedSymbols')
163-
used = data.get('used')
164-
symbol_focus_keys = data.get('symbolFocusKeys')
163+
formatted_data = camel_to_underline_obj(data)
164+
165+
limit = formatted_data.get('limit')
166+
subscribed_symbols = formatted_data.get('subscribed_symbols')
167+
used = formatted_data.get('used')
168+
symbol_focus_keys = formatted_data.get('symbol_focus_keys')
165169
focus_keys = dict()
166170
for sym, keys in symbol_focus_keys.items():
167171
keys = set(QUOTE_KEYS_MAPPINGS.get(key, camel_to_underline(key)) for key in keys)
168172
focus_keys[sym] = list(keys)
169-
self.subscribed_symbols(symbols, focus_keys, limit, used)
173+
formatted_data['symbol_focus_keys'] = focus_keys
174+
formatted_data['subscribed_quote_depth_symbols'] = formatted_data.pop('subscribed_ask_bid_symbols')
175+
formatted_data['quote_depth_limit'] = formatted_data.pop('ask_bid_limit')
176+
formatted_data['quote_depth_used'] = formatted_data.pop('ask_bid_used')
177+
if self.subscribed_symbols:
178+
self.logger.warning('PushClient.subscribed_symbols is deprecated, '
179+
'use PushClient.query_subscribed_callback instead.')
180+
self.subscribed_symbols(subscribed_symbols, focus_keys, limit, used)
181+
if self.query_subscribed_callback:
182+
self.query_subscribed_callback(formatted_data)
183+
170184
elif response_type == str(ResponseType.GET_QUOTE_CHANGE_END.value):
171185
if self.quote_changed:
172186
data = json.loads(body)
@@ -266,19 +280,19 @@ def on_message(self, frame):
266280
if self.error_callback:
267281
self.error_callback(body)
268282
except Exception as e:
269-
logging.error(e, exc_info=True)
283+
self.logger.error(e, exc_info=True)
270284

271285
def on_error(self, frame):
272286
body = json.loads(frame.body)
273287
if body.get('code') == 4001:
274-
logging.error(body)
288+
self.logger.error(body)
275289
self.disconnect_callback = None
276290
raise ApiException(4001, body.get('message'))
277291

278292
if self.error_callback:
279293
self.error_callback(frame)
280294
else:
281-
logging.error(frame.body)
295+
self.logger.error(frame.body)
282296

283297
def _update_subscribe_id(self, destination):
284298
self._destination_counter_map[destination] += 1
@@ -356,7 +370,7 @@ def subscribe_tick(self, symbols):
356370
:param symbols: symbol列表
357371
:return:
358372
"""
359-
return self._handle_quote_subscribe(destination=QUOTE_TICK, subscription=SUBSCRIPTION_QUOTE_TICK,
373+
return self._handle_quote_subscribe(destination=TRADE_TICK, subscription=SUBSCRIPTION_TRADE_TICK,
360374
symbols=symbols)
361375

362376
def subscribe_depth_quote(self, symbols):
@@ -405,7 +419,7 @@ def unsubscribe_tick(self, symbols=None, id=None):
405419
退订行情更新
406420
:return:
407421
"""
408-
self._handle_quote_unsubscribe(destination=QUOTE_TICK, subscription=SUBSCRIPTION_QUOTE_TICK, sub_id=id,
422+
self._handle_quote_unsubscribe(destination=TRADE_TICK, subscription=SUBSCRIPTION_TRADE_TICK, sub_id=id,
409423
symbols=symbols)
410424

411425
def unsubscribe_depth_quote(self, symbols=None, id=None):

0 commit comments

Comments
 (0)