Skip to content

Commit 3ba7f68

Browse files
committed
quote_client.get_trade_ticks compatible with versiion 1.0
1 parent 22a07b1 commit 3ba7f68

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

tigeropen/quote/quote_client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ def get_bars_by_page(self, symbol, period=BarPeriod.DAY, begin_time=-1, end_time
480480
time.sleep(time_interval)
481481
return pd.concat(result).sort_values('time').reset_index(drop=True)
482482

483-
def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_index=None, limit=None, lang=None):
483+
def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_index=None, limit=None, lang=None,
484+
**kwargs):
484485
"""
485486
获取逐笔成交
486487
:param symbols: 股票代号列表
@@ -497,12 +498,16 @@ def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_ind
497498
direction: 价格变动方向,"-"表示向下变动, "+" 表示向上变动
498499
"""
499500
params = MultipleQuoteParams()
500-
params.symbols = symbols
501+
params.symbols = [symbols] if isinstance(symbols, str) else symbols
502+
# compatible with version 1.0
503+
params.symbol = symbols if isinstance(symbols, str) else symbols[0]
501504
params.trade_session = get_enum_value(trade_session)
502505
params.begin_index = begin_index
503506
params.end_index = end_index
504507
params.limit = limit
505508
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
509+
if 'version' in kwargs:
510+
params.version = kwargs.get('version')
506511

507512
request = OpenApiRequest(TRADE_TICK, biz_model=params)
508513
response_content = self.__fetch_data(request)

tigeropen/quote/request/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class MultipleQuoteParams(MarketParams):
210210
def __init__(self):
211211
super(MultipleQuoteParams, self).__init__()
212212
self._symbols = None
213+
self._symbol = None
213214
self._include_hour_trading = None
214215
self._include_ask_bid = None
215216
self._right = None
@@ -231,6 +232,14 @@ def symbols(self):
231232
def symbols(self, value):
232233
self._symbols = value
233234

235+
@property
236+
def symbol(self):
237+
return self._symbol
238+
239+
@symbol.setter
240+
def symbol(self, value):
241+
self._symbol = value
242+
234243
@property
235244
def include_hour_trading(self):
236245
return self._include_hour_trading
@@ -333,6 +342,9 @@ def to_openapi_dict(self):
333342
if self.symbols:
334343
params['symbols'] = self.symbols
335344

345+
if self.symbol:
346+
params['symbol'] = self.symbol
347+
336348
if self.include_hour_trading is not None:
337349
params['include_hour_trading'] = self.include_hour_trading
338350

tigeropen/quote/response/quote_ticks_response.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
@author: gaoan
66
"""
7+
import json
8+
79
import pandas as pd
810

911
from tigeropen.common.response import TigerResponse
@@ -22,15 +24,33 @@ def parse_response_content(self, response_content):
2224
if 'is_success' in response:
2325
self._is_success = response['is_success']
2426

25-
if self.data and isinstance(self.data, list):
27+
# v2: {'data': [{'symbol': 'AAPL', 'beginIndex': 724203, 'endIndex': 724403,
28+
# 'items': [{'time': 1663963199040, 'volume': 200, 'price': 150.66, 'type': '+'},
29+
# {'time': 1663963199051, 'volume': 100, 'price': 150.65, 'type': '-'},
30+
# {'time': 1663963199051, 'volume': 300, 'price': 150.65, 'type': '-'}]
31+
32+
# v1: {'data': '{"beginIndex":722500,"endIndex":724403,
33+
# "items":[{"time":1663963192126,"volume":400,"price":150.5901,"type":"-"},
34+
# {"time":1663963192142,"volume":100,"price":150.61,"type":"+"}
35+
36+
if self.data:
37+
# v2
38+
if isinstance(self.data, list):
39+
symbol_items = self.data
40+
else:
41+
data = json.loads(self.data)
42+
symbol_items = [data]
43+
2644
tick_items = []
27-
for symbol_item in self.data:
45+
for symbol_item in symbol_items:
2846
symbol = symbol_item.get('symbol')
2947
if 'items' in symbol_item:
3048
index = symbol_item.get('beginIndex')
3149

3250
for item in symbol_item['items']:
33-
item_values = {'symbol': symbol}
51+
item_values = dict()
52+
if symbol is not None:
53+
item_values['symbol'] = symbol
3454

3555
for key, value in item.items():
3656
if value is None:
@@ -44,6 +64,6 @@ def parse_response_content(self, response_content):
4464
if index is not None:
4565
item_values['index'] = index
4666
index += 1
47-
tick_items.append([item_values.get(tag) for tag in COLUMNS])
67+
tick_items.append(item_values)
4868

49-
self.trade_ticks = pd.DataFrame(tick_items, columns=COLUMNS)
69+
self.trade_ticks = pd.DataFrame(tick_items)

0 commit comments

Comments
 (0)