From 374c228ebf2f2b8d75f0dad3a3af711d4f80d090 Mon Sep 17 00:00:00 2001 From: yangyang Date: Thu, 31 Aug 2017 15:02:16 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E4=B8=BA=E7=A9=BA=E6=97=B6=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/data/tq/vntq.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/vnpy/data/tq/vntq.py b/vnpy/data/tq/vntq.py index ba91c0f3..b630b809 100644 --- a/vnpy/data/tq/vntq.py +++ b/vnpy/data/tq/vntq.py @@ -226,11 +226,12 @@ class TqApi(object): chart_id = self._generate_chart_id(ins_id, 0) sub_info = self.chart_subscribes.get(chart_id) tick_serial = self.get_tick_serial(ins_id) - while len(tick_serial) > sub_info["view_width"]: - tick_serial.popitem(last=False) - callback_func = sub_info["callback"] - if callback_func: - callback_func(ins_id, 0) + if tick_serial: + while len(tick_serial) > sub_info["view_width"]: + tick_serial.popitem(last=False) + callback_func = sub_info["callback"] + if callback_func: + callback_func(ins_id, 0) elif selector == "klines": for ins_id, sub_section in section.items(): @@ -239,11 +240,12 @@ class TqApi(object): chart_id = self._generate_chart_id(ins_id, dur_seconds) sub_info = self.chart_subscribes.get(chart_id) kline_serial = self.get_kline_serial(ins_id, dur_seconds) - while len(kline_serial) > sub_info["view_width"]: - kline_serial.popitem(last=False) - callback_func = sub_info["callback"] - if callback_func: - callback_func(ins_id, dur_seconds) + if kline_serial: + while len(kline_serial) > sub_info["view_width"]: + kline_serial.popitem(last=False) + callback_func = sub_info["callback"] + if callback_func: + callback_func(ins_id, dur_seconds) #---------------------------------------------------------------------- def _merge_obj(self, result, obj): From 20ebd554997658441d18e30ffc2104e6c5e96691 Mon Sep 17 00:00:00 2001 From: yangyang Date: Mon, 4 Sep 2017 09:34:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E5=9B=9E=E8=B0=83=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=8A=9B=E5=87=BAexception=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/data/tq/vntq.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/vnpy/data/tq/vntq.py b/vnpy/data/tq/vntq.py index b630b809..4c08390d 100644 --- a/vnpy/data/tq/vntq.py +++ b/vnpy/data/tq/vntq.py @@ -26,6 +26,7 @@ class TqApi(object): self.requests = [] # 请求缓存 self.quote_callback_func = None # tick回调函数 + self.quote_ins_list = [] self.chart_subscribes = {} # k线回调函数 #---------------------------------------------------------------------- @@ -36,7 +37,7 @@ class TqApi(object): self.start() # 启动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.start() @@ -53,7 +54,8 @@ class TqApi(object): """ if callback_func: self.quote_callback_func = callback_func - + self.quote_ins_list = ins_list + req = { "aid": "subscribe_quote", "ins_list": ",".join(ins_list), @@ -72,8 +74,8 @@ class TqApi(object): :param data_length: 需要获取的序列长度。每个序列最大支持请求 8964 个数据 :param callback_func (可选): callback_func 是一个回调函数,每当序列数据变更时会触发。此函数应该接受2个参数 ins_id, duration_seconds :example: - 订阅 cu1803 的1分钟线: subscribe_chart("s1", "cu1803", 60) - 订阅 IF1709 的tick线: subscribe_chart("s2", "IF1709", 0) + 订阅 cu1803 的1分钟线: subscribe_chart("cu1803", 60) + 订阅 IF1709 的tick线: subscribe_chart("IF1709", 0) """ chart_id = self._generate_chart_id(ins_id, duration_seconds) @@ -219,14 +221,15 @@ class TqApi(object): if selector == "quotes": if self.quote_callback_func: 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": for ins_id in section.keys(): 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) - if tick_serial: + if tick_serial and sub_info: while len(tick_serial) > sub_info["view_width"]: tick_serial.popitem(last=False) callback_func = sub_info["callback"] @@ -238,9 +241,9 @@ class TqApi(object): for dur_nanoseconds in sub_section.keys(): dur_seconds = int(dur_nanoseconds) / 1000000000 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) - if kline_serial: + if kline_serial and sub_info: while len(kline_serial) > sub_info["view_width"]: kline_serial.popitem(last=False) callback_func = sub_info["callback"]