| | |
| | | set_codes_data_queue = queue.Queue() |
| | | market_code_dict = {} |
| | | |
| | | ENABLE_NGST = True |
| | | |
| | | |
| | | class Lev2MdSpi(lev2mdapi.CTORATstpLev2MdSpi): |
| | | latest_codes_set = set() |
| | |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅上证:{sh}") |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅深证:{sz}") |
| | | if sh: |
| | | # 取消订阅逐笔委托 |
| | | self.__api.UnSubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | # 取消订阅逐笔成交 |
| | | self.__api.UnSubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | if ENABLE_NGST: |
| | | result = self.__api.UnSubscribeNGTSTick(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔NGTS订阅结果sh:{result}") |
| | | else: |
| | | # 取消订阅逐笔委托 |
| | | self.__api.UnSubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | # 取消订阅逐笔成交 |
| | | self.__api.UnSubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | if sz: |
| | | self.__api.UnSubscribeOrderDetail(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | |
| | | logger_local_huaxin_l2_subscript.info(f"订阅上证:{sh}") |
| | | logger_local_huaxin_l2_subscript.info(f"订阅深证:{sz}") |
| | | if sh: |
| | | # 订阅逐笔委托 |
| | | result = self.__api.SubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sh:{result}") |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | if ENABLE_NGST: |
| | | result = self.__api.SubscribeNGTSTick(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔NGTS订阅结果sh:{result}") |
| | | else: |
| | | # 订阅逐笔委托 |
| | | result = self.__api.SubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sh:{result}") |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | |
| | | result = self.__api.SubscribeMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"市场订阅结果sh:{result}") |
| | |
| | | self.l2_data_upload_manager.distribute_upload_queue(c) |
| | | l2_data_manager.add_target_code(c) |
| | | except Exception as e: |
| | | #TODO 清除原来还没释放掉的数据 |
| | | # TODO 清除原来还没释放掉的数据 |
| | | logger_system.error(f"L2代码分配上传队列出错:{str(e)}") |
| | | logger_system.exception(e) |
| | | self.__subscribe(add_codes) |
| | |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | def OnRspSubNGTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | async_log_util.info(logger_local_huaxin_l2_subscript, |
| | | f"订阅结果:{pSpecificSecurity['SecurityID']} {pRspInfo['ErrorID']} {pRspInfo['ErrorMsg']}") |
| | | if pRspInfo["ErrorID"] == 0: |
| | | print("订阅成功") |
| | | self.subscripted_codes.add(pSpecificSecurity['SecurityID']) |
| | | if bIsLast == 1: |
| | | print("订阅响应结束", self.subscripted_codes) |
| | | l2_data_manager.add_subscript_codes(self.subscripted_codes) |
| | | |
| | | def OnRspUnSubNGTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | try: |
| | | code = pSpecificSecurity['SecurityID'] |
| | | self.subscripted_codes.discard(code) |
| | | if bIsLast == 1: |
| | | print("取消订阅响应结束", self.subscripted_codes) |
| | | l2_data_manager.add_subscript_codes(self.subscripted_codes) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | def OnRspSubBondMarketData(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | print("OnRspSubBondMarketData") |
| | | |
| | |
| | | |
| | | def OnRspSubXTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | print("OnRspSubXTSTick") |
| | | |
| | | # 4.0.5版本接口 |
| | | def OnRspSubNGTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | print("OnRspSubNGTSTick") |
| | | |
| | | def OnRtnMarketData(self, pDepthMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, |
| | | FirstLevelSellOrderVolumes): |
| | |
| | | self.l2_data_upload_manager.add_transaction_detail(item) |
| | | |
| | | def OnRtnOrderDetail(self, pOrderDetail): |
| | | # can_listen = False |
| | | # code = str(pOrderDetail['SecurityID']) |
| | | # start_time = 0 |
| | | # if code in self.special_code_volume_for_order_dict: |
| | | # start_time = time.time() |
| | | # if self.special_code_volume_for_order_dict[code][0] == pOrderDetail[ |
| | | # 'Volume'] or constant.SHADOW_ORDER_VOLUME == pOrderDetail['Volume']: |
| | | # # 监控目标订单与影子订单 |
| | | # if self.special_code_volume_for_order_dict[code][1] > time.time(): |
| | | # # 特殊量监听 |
| | | # can_listen = True |
| | | # else: |
| | | # self.special_code_volume_for_order_dict.pop(code) |
| | | # if not can_listen: |
| | | # min_volume, limit_up_price = self.codes_volume_and_price_dict.get(code) |
| | | # if min_volume is None: |
| | | # # 默认筛选50w |
| | | # if pOrderDetail['Price'] * pOrderDetail['Volume'] < 500000: |
| | | # return |
| | | # elif pOrderDetail['Volume'] < min_volume: |
| | | # return |
| | | # 输出逐笔委托数据 |
| | | # 上证OrderStatus=b"D"表示撤单 |
| | | item = {"SecurityID": pOrderDetail['SecurityID'], "Price": pOrderDetail['Price'], |
| | | "Volume": pOrderDetail['Volume'], |
| | |
| | | "OrderStatus": pOrderDetail['OrderStatus'].decode()} |
| | | self.l2_data_upload_manager.add_l2_order_detail(item, 0) |
| | | |
| | | def OnRtnNGTSTick(self, pTick): |
| | | """ |
| | | 上证股票的逐笔委托与成交 |
| | | @param pTick: |
| | | @return: |
| | | """ |
| | | try: |
| | | if pTick['TickType'] == b'T': |
| | | # 成交 |
| | | item = {"SecurityID": pTick['SecurityID'], "TradePrice": pTick['Price'], |
| | | "TradeVolume": pTick['Volume'], |
| | | "OrderTime": pTick['TickTime'], "MainSeq": pTick['MainSeq'], |
| | | "SubSeq": pTick['SubSeq'], "BuyNo": pTick['BuyNo'], |
| | | "SellNo": pTick['SellNo'], |
| | | "ExecType": '1'} |
| | | self.l2_data_upload_manager.add_transaction_detail(item) |
| | | elif pTick['TickType'] == b'A' or pTick['TickType'] == b'D': |
| | | # 撤单 |
| | | item = {"SecurityID": pTick['SecurityID'], "Price": pTick['Price'], |
| | | "Volume": pTick['Volume'], |
| | | "Side": pTick['Side'].decode(), "OrderType": pTick['TickType'].decode(), |
| | | "OrderTime": pTick['TickTime'], "MainSeq": pTick['MainSeq'], |
| | | "SubSeq": pTick['SubSeq'], "OrderNO": '', |
| | | "OrderStatus": pTick['TickType'].decode()} |
| | | if pTick['Side'] == b'1': |
| | | item['OrderNO'] = pTick['BuyNo'] |
| | | elif pTick['Side'] == b'2': |
| | | item['OrderNO'] = pTick['SellNo'] |
| | | self.l2_data_upload_manager.add_l2_order_detail(item, 0) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_subscript.exception(e) |
| | | |
| | | def OnRtnBondMarketData(self, pDepthMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, |
| | | FirstLevelSellOrderVolumes): |
| | | # 输出行情快照数据 |