| | |
| | | self.codes = codes |
| | | |
| | | def __split_codes(self, codes): |
| | | """ |
| | | 分离代码 |
| | | :param codes: |
| | | :return:上证股票代码, 上证非股票代码, 深证代码 |
| | | """ |
| | | szse_codes = [] |
| | | sse_codes = [] |
| | | sse_other_codes = [] |
| | | sse_stock_codes = [] |
| | | for code in codes: |
| | | market_type = tool.get_market_type(code) |
| | | if market_type == tool.MARKET_TYPE_SZSE: |
| | | szse_codes.append(code.encode()) |
| | | elif market_type == tool.MARKET_TYPE_SSE: |
| | | sse_codes.append(code.encode()) |
| | | return sse_codes, szse_codes |
| | | if tool.is_stock(code): |
| | | sse_stock_codes.append(code.encode) |
| | | else: |
| | | sse_other_codes.append(code.encode()) |
| | | return sse_stock_codes, sse_other_codes, szse_codes |
| | | |
| | | # 新增订阅 |
| | | |
| | | # 取消订阅 |
| | | def __unsubscribe_trans(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅上证:{sh}") |
| | | sh_stock, sh_other, sz = self.__split_codes(_codes) |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅上证:{sh_stock}") |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅上证:{sh_other}") |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅深证:{sz}") |
| | | if sh: |
| | | if sh_other: |
| | | # 取消订阅逐笔成交 |
| | | self.__api.UnSubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeTransaction(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | if sh_stock: |
| | | # 取消订阅逐笔成交 |
| | | self.__api.UnSubscribeNGTSTick(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | if sz: |
| | | self.__api.UnSubscribeTransaction(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | |
| | | def __subscribe_trans(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | logger_local_huaxin_l2_subscript.info(f"订阅上证:{sh}") |
| | | sh_stock, sh_other, sz = self.__split_codes(_codes) |
| | | logger_local_huaxin_l2_subscript.info(f"订阅上证:{sh_stock}") |
| | | logger_local_huaxin_l2_subscript.info(f"订阅上证:{sh_other}") |
| | | logger_local_huaxin_l2_subscript.info(f"订阅深证:{sz}") |
| | | if sh: |
| | | if sh_other: |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | result = self.__api.SubscribeTransaction(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | if sh_stock: |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeNGTSTick(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | if sz: |
| | | result = self.__api.SubscribeTransaction(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sz:{result}") |
| | | |
| | | def __unsubscribe_market(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | if sh: |
| | | sh_stock, sh_other, sz = self.__split_codes(_codes) |
| | | if sh_other: |
| | | # 取消订阅逐笔成交 |
| | | self.__api.UnSubscribeMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeXTSMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeMarketData(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeXTSMarketData(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | if sh_stock: |
| | | self.__api.UnSubscribeMarketData(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | self.__api.UnSubscribeNGTSTick(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | |
| | | if sz: |
| | | self.__api.UnSubscribeMarketData(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | |
| | | def __subscribe_market(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | if sh: |
| | | sh_stock, sh_other, sz = self.__split_codes(_codes) |
| | | if sh_stock: |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | result = self.__api.SubscribeMarketData(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | result = self.__api.SubscribeXTSMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | result = self.__api.SubscribeXTSMarketData(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | if sh_other: |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeMarketData(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | result = self.__api.SubscribeXTSMarketData(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | if sz: |
| | | result = self.__api.SubscribeMarketData(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | def OnRspSubNGTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | if pRspInfo["ErrorID"] == 0: |
| | | self.subscripted_transaction_codes.add(pSpecificSecurity['SecurityID']) |
| | | logger_local_huaxin_l2_subscript.info(f"成交订阅成功:{pSpecificSecurity['SecurityID']}") |
| | | |
| | | def OnRspUnSubNGTSTick(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | try: |
| | | code = pSpecificSecurity['SecurityID'] |
| | | self.subscripted_transaction_codes.discard(code) |
| | | logger_local_huaxin_l2_subscript.info(f"成交取消订阅:{pSpecificSecurity['SecurityID']}") |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | |
| | | def OnRspSubMarketData(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): |
| | | if pRspInfo["ErrorID"] == 0: |
| | | self.subscripted_market_codes.add(pSpecificSecurity['SecurityID']) |
| | |
| | | (pTransaction['SecurityID'], pTransaction['TradePrice'], pTransaction['TradeTime'])) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | |
| | | def OnRtnNGTSTick(self, pTick): |
| | | 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'} |
| | | huaxin_l2_log.info(logger_local_huaxin_l2_transaction, f"{item}") |
| | | if self.buyStrategyDataManager.add_transaction_info(item): |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTick['SecurityID'], pTick['Price'], pTick['TickTime'])) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | |
| | | |
| | | |
| | | def OnRtnMarketData(self, pDepthMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, |
| | | FirstLevelSellOrderVolumes): |
| | |
| | | market_call_back_queue.put_nowait((pDepthMarketData['SecurityID'], pDepthMarketData['LastPrice'], rate, |
| | | pDepthMarketData['BidPrice1'], pDepthMarketData['BidVolume1'], |
| | | pDepthMarketData['TotalVolumeTrade'], pDepthMarketData['TotalBidVolume'], |
| | | pDepthMarketData['TotalAskVolume'], pDepthMarketData['PreClosePrice'], pDepthMarketData['DataTimeStamp'])) |
| | | pDepthMarketData['TotalAskVolume'], pDepthMarketData['PreClosePrice'], |
| | | pDepthMarketData['DataTimeStamp'])) |
| | | code = pDepthMarketData['SecurityID'] |
| | | if code.find("00") == 0 or code.find("60") == 0: |
| | | if rate >= 0.05: |
| | |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | |
| | | def OnRtnXTSMarketData(self, pMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, FirstLevelSellOrderVolumes): |
| | | def OnRtnXTSMarketData(self, pMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, |
| | | FirstLevelSellOrderVolumes): |
| | | d = {"dataTimeStamp": pMarketData['DataTimeStamp'], "securityID": pMarketData['SecurityID'], |
| | | "preClosePrice": pMarketData['PreClosePrice'], |
| | | "lastPrice": pMarketData['LastPrice'], |