Merge pull request #482 from tianqin18/dev

天勤接口修正
This commit is contained in:
vn.py 2017-09-04 10:24:49 +08:00 committed by GitHub
commit 3c7b3f44b8

View File

@ -26,6 +26,7 @@ class TqApi(object):
self.requests = [] # 请求缓存 self.requests = [] # 请求缓存
self.quote_callback_func = None # tick回调函数 self.quote_callback_func = None # tick回调函数
self.quote_ins_list = []
self.chart_subscribes = {} # k线回调函数 self.chart_subscribes = {} # k线回调函数
#---------------------------------------------------------------------- #----------------------------------------------------------------------
@ -36,7 +37,7 @@ class TqApi(object):
self.start() self.start()
# 启动tornado的IO线程 # 启动tornado的IO线程
loop_thread = threading.Thread(target=lambda : tornado.ioloop.IOLoop.current().start()) loop_thread = threading.Thread(target=lambda: tornado.ioloop.IOLoop.current().start())
loop_thread.setDaemon(True) loop_thread.setDaemon(True)
loop_thread.start() loop_thread.start()
@ -53,6 +54,7 @@ class TqApi(object):
""" """
if callback_func: if callback_func:
self.quote_callback_func = callback_func self.quote_callback_func = callback_func
self.quote_ins_list = ins_list
req = { req = {
"aid": "subscribe_quote", "aid": "subscribe_quote",
@ -72,8 +74,8 @@ class TqApi(object):
:param data_length: 需要获取的序列长度每个序列最大支持请求 8964 个数据 :param data_length: 需要获取的序列长度每个序列最大支持请求 8964 个数据
:param callback_func (可选): callback_func 是一个回调函数每当序列数据变更时会触发此函数应该接受2个参数 ins_id, duration_seconds :param callback_func (可选): callback_func 是一个回调函数每当序列数据变更时会触发此函数应该接受2个参数 ins_id, duration_seconds
:example: :example:
订阅 cu1803 的1分钟线 subscribe_chart("s1", "cu1803", 60) 订阅 cu1803 的1分钟线 subscribe_chart("cu1803", 60)
订阅 IF1709 的tick线 subscribe_chart("s2", "IF1709", 0) 订阅 IF1709 的tick线 subscribe_chart("IF1709", 0)
""" """
chart_id = self._generate_chart_id(ins_id, duration_seconds) chart_id = self._generate_chart_id(ins_id, duration_seconds)
@ -219,31 +221,34 @@ class TqApi(object):
if selector == "quotes": if selector == "quotes":
if self.quote_callback_func: if self.quote_callback_func:
for ins_id in section.keys(): for ins_id in section.keys():
self.quote_callback_func(ins_id) if ins_id in self.quote_ins_list:
self.quote_callback_func(ins_id)
elif selector == "ticks": elif selector == "ticks":
for ins_id in section.keys(): for ins_id in section.keys():
chart_id = self._generate_chart_id(ins_id, 0) chart_id = self._generate_chart_id(ins_id, 0)
sub_info = self.chart_subscribes.get(chart_id) sub_info = self.chart_subscribes.get(chart_id, None)
tick_serial = self.get_tick_serial(ins_id) tick_serial = self.get_tick_serial(ins_id)
while len(tick_serial) > sub_info["view_width"]: if tick_serial and sub_info:
tick_serial.popitem(last=False) while len(tick_serial) > sub_info["view_width"]:
callback_func = sub_info["callback"] tick_serial.popitem(last=False)
if callback_func: callback_func = sub_info["callback"]
callback_func(ins_id, 0) if callback_func:
callback_func(ins_id, 0)
elif selector == "klines": elif selector == "klines":
for ins_id, sub_section in section.items(): for ins_id, sub_section in section.items():
for dur_nanoseconds in sub_section.keys(): for dur_nanoseconds in sub_section.keys():
dur_seconds = int(dur_nanoseconds) / 1000000000 dur_seconds = int(dur_nanoseconds) / 1000000000
chart_id = self._generate_chart_id(ins_id, dur_seconds) chart_id = self._generate_chart_id(ins_id, dur_seconds)
sub_info = self.chart_subscribes.get(chart_id) sub_info = self.chart_subscribes.get(chart_id, None)
kline_serial = self.get_kline_serial(ins_id, dur_seconds) kline_serial = self.get_kline_serial(ins_id, dur_seconds)
while len(kline_serial) > sub_info["view_width"]: if kline_serial and sub_info:
kline_serial.popitem(last=False) while len(kline_serial) > sub_info["view_width"]:
callback_func = sub_info["callback"] kline_serial.popitem(last=False)
if callback_func: callback_func = sub_info["callback"]
callback_func(ins_id, dur_seconds) if callback_func:
callback_func(ins_id, dur_seconds)
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def _merge_obj(self, result, obj): def _merge_obj(self, result, obj):