From 70bbbc724f11ffa7d36aadcc0fed99e9394b2e82 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期五, 14 六月 2024 15:19:43 +0800
Subject: [PATCH] L2接口更新

---
 huaxin_client/l2_client_for_cb.py |  105 ++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 84 insertions(+), 21 deletions(-)

diff --git a/huaxin_client/l2_client_for_cb.py b/huaxin_client/l2_client_for_cb.py
index afecc2f..8f5ded0 100644
--- a/huaxin_client/l2_client_for_cb.py
+++ b/huaxin_client/l2_client_for_cb.py
@@ -73,57 +73,85 @@
         self.codes = codes
 
     def __split_codes(self, codes):
+        """
+        鍒嗙浠g爜
+        :param codes:
+        :return:涓婅瘉鑲$エ浠g爜, 涓婅瘉闈炶偂绁ㄤ唬鐮�, 娣辫瘉浠g爜
+        """
         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)
@@ -215,6 +243,20 @@
         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'])
@@ -244,6 +286,25 @@
                         (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):
@@ -278,7 +339,8 @@
             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:
@@ -296,7 +358,8 @@
         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'],

--
Gitblit v1.8.0