# -*- coding: utf-8 -*- import json import logging import multiprocessing import os import queue import threading import time import concurrent.futures from huaxin_client import command_manager from huaxin_client import constant from huaxin_client import l2_data_manager import lev2mdapi from huaxin_client.code_queue_distribute_manager import CodeDataCallbackDistributeManager from huaxin_client.command_manager import L2ActionCallback from huaxin_client.l2_data_manager import L2DataUploadManager from log_module import log, async_log_util from log_module.async_log_util import huaxin_l2_log from log_module.log import logger_local_huaxin_l2_subscript, logger_system, logger_l2_codes_subscript, printlog, logger_debug, logger_local_huaxin_l2_market from utils import tool ###B类### Front_Address = "tcp://10.0.1.101:6900" Multicast_Address = "udp://224.224.2.19:7889" Multicast_Address2 = "udp://224.224.224.234:7890" Local_Interface_Address = constant.LOCAL_IP g_SubMarketData = False g_SubTransaction = False g_SubOrderDetail = False g_SubXTSTick = False g_SubXTSMarketData = False g_SubNGTSTick = False g_SubBondMarketData = False g_SubBondTransaction = False g_SubBondOrderDetail = False SH_Securities = [b"603000", b"600225", b"600469", b"600616", b"600059", b"002849", b"605188", b"603630", b"600105", b"603773", b"603915", b"603569", b"603322", b"603798", b"605198", b"603079", b"600415", b"600601"] SH_XTS_Securities = [b"018003", b"113565"] SZ_Securities = [b"002456", b"002849", b"002281", b"002336", b"000936", b"000920", b"000757", b"002896", b"002725", b"000952", b"000526", b"000753", b"000681", b"002088", b"002436"] SZ_Bond_Securities = [b"100303", b"109559", b"112617"] set_codes_data_queue = queue.Queue() market_code_dict = {} class Lev2MdSpi(lev2mdapi.CTORATstpLev2MdSpi): latest_codes_set = set() special_code_volume_for_order_dict = {} # 已经订阅的代码 subscripted_codes = set() # 代码的上次成交的订单唯一索引 __last_transaction_keys_dict = {} # 买入的大单订单号 def __init__(self, api, l2_data_upload_manager: L2DataUploadManager): lev2mdapi.CTORATstpLev2MdSpi.__init__(self) self.__api = api self.is_login = False self.l2_data_upload_manager = l2_data_upload_manager def __split_codes(self, codes): """ 分离代码 :param codes: :return:上证股票代码, 上证非股票代码, 深证代码 """ szse_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: 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(self, _codes): 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_stock: # 取消订阅 result = self.__api.UnSubscribeNGTSTick(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) logger_local_huaxin_l2_subscript.info(f"逐笔NGTS订阅结果sh:{result}") self.__api.UnSubscribeMarketData(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) if sh_other: self.__api.UnSubscribeOrderDetail(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) self.__api.UnSubscribeTransaction(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) if sz: self.__api.UnSubscribeOrderDetail(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) self.__api.UnSubscribeTransaction(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) self.__api.UnSubscribeMarketData(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) def __subscribe(self, _codes): 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_stock: # 订阅上证逐笔 result = self.__api.SubscribeNGTSTick(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) logger_local_huaxin_l2_subscript.info(f"逐笔NGTS订阅结果sh:{result}") result = self.__api.SubscribeMarketData(sh_stock, lev2mdapi.TORA_TSTP_EXD_SSE) logger_local_huaxin_l2_subscript.info(f"市场订阅结果sh:{result}") if sh_other: # 订阅上证逐笔 result = self.__api.SubscribeOrderDetail(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sh:{result}") result = self.__api.SubscribeTransaction(sh_other, lev2mdapi.TORA_TSTP_EXD_SSE) logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") if sz: result = self.__api.SubscribeOrderDetail(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sz:{result}") result = self.__api.SubscribeTransaction(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sz:{result}") result = self.__api.SubscribeMarketData(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) logger_local_huaxin_l2_subscript.info(f"市场订阅结果sz:{result}") def __process_codes_data(self, codes, from_cache=False, delay=0.0): codes = set(codes) if not self.is_login and not constant.TEST: raise Exception("L2尚未登录") if delay > 0: time.sleep(delay) add_codes = codes - self.subscripted_codes del_codes = self.subscripted_codes - codes printlog("add del codes", add_codes, del_codes) try: for c in del_codes: self.l2_data_upload_manager.release_distributed_upload_queue(c) l2_data_manager.del_target_code(c) for c in codes: self.l2_data_upload_manager.distribute_upload_queue(c) l2_data_manager.add_target_code(c) except Exception as e: logger_system.error(f"L2代码分配上传队列出错:{str(e)}") logger_system.exception(e) self.__subscribe(add_codes) self.__unsubscribe(del_codes) if add_codes: logger_system.info(f"新增L2订阅代码数量({'缓存' if from_cache else ''}):{len(add_codes)}") logger_l2_codes_subscript.info("华鑫L2订阅结束,add-{} del-{}", len(add_codes), len(del_codes)) # 设置最近的代码列表 self.latest_codes_set = codes # 订阅代码,[代码,...] def set_codes_data(self, codes): try: self.__process_codes_data(codes) except Exception as e: logging.exception(e) logger_l2_codes_subscript.exception(e) finally: # 保存一份最新的数据 self.__set_latest_datas(codes) @classmethod def __set_latest_datas(cls, codes_data): data_str = json.dumps([tool.get_now_date_str(), codes_data]) with open(constant.L2_CODES_INFO_PATH, mode='w') as f: f.write(data_str) @classmethod def __get_latest_datas(cls): if os.path.exists(constant.L2_CODES_INFO_PATH): with open(constant.L2_CODES_INFO_PATH, mode='r') as f: str_ = f.readline() data_json = json.loads(str_) if data_json[0] == tool.get_now_date_str(): return data_json[1] return [] def OnFrontConnected(self): printlog("OnFrontConnected") logger_system.info(f"l2_client OnFrontConnected 线程ID:{tool.get_thread_id()}") logout_req = lev2mdapi.CTORATstpUserLogoutField() self.__api.ReqUserLogout(logout_req, 1) time.sleep(1) # 请求登录 login_req = lev2mdapi.CTORATstpReqUserLoginField() self.__api.ReqUserLogin(login_req, 2) def OnRspUserLogin(self, pRspUserLogin, pRspInfo, nRequestID, bIsLast): printlog("OnRspUserLogin: ErrorID[%d] ErrorMsg[%s] RequestID[%d] IsLast[%d]" % ( pRspInfo['ErrorID'], pRspInfo['ErrorMsg'], nRequestID, bIsLast)) if pRspInfo['ErrorID'] == 0: printlog("----L2行情登录成功----") self.is_login = True logger_system.info(f"L2行情登录成功") # 初始设置值 threading.Thread( target=lambda: self.__process_codes_data(self.__get_latest_datas(), from_cache=True, delay=6.0), daemon=True).start() def OnRspSubOrderDetail(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): printlog("OnRspSubOrderDetail", pRspInfo) # try: printlog("订阅结果:", pSpecificSecurity["ExchangeID"], pSpecificSecurity["SecurityID"], pRspInfo["ErrorID"], pRspInfo["ErrorMsg"]) async_log_util.info(logger_local_huaxin_l2_subscript, f"订阅结果:{pSpecificSecurity['SecurityID']} {pRspInfo['ErrorID']} {pRspInfo['ErrorMsg']}") if pRspInfo["ErrorID"] == 0: printlog("订阅成功") self.subscripted_codes.add(pSpecificSecurity['SecurityID']) if bIsLast == 1: printlog("订阅响应结束", self.subscripted_codes) l2_data_manager.add_subscript_codes(self.subscripted_codes) def OnRspUnSubOrderDetail(self, pSpecificSecurity, pRspInfo, nRequestID, bIsLast): printlog("OnRspUnSubOrderDetail", bIsLast) try: code = pSpecificSecurity['SecurityID'] self.subscripted_codes.discard(code) if bIsLast == 1: printlog("取消订阅响应结束", self.subscripted_codes) l2_data_manager.add_subscript_codes(self.subscripted_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"NGTS订阅结果:{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'] logger_local_huaxin_l2_subscript.info(f"NGTS取消订阅:{code}") 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 OnRtnTransaction(self, pTransaction): # 输出逐笔成交数据 if pTransaction['ExecType'] == b"2": # 撤单 item = {"SecurityID": pTransaction['SecurityID'], "Price": pTransaction['TradePrice'], "Volume": pTransaction['TradeVolume'], "OrderType": "2", "OrderTime": pTransaction['TradeTime'], "MainSeq": pTransaction['MainSeq'], "SubSeq": pTransaction['SubSeq'], "OrderStatus": "D"} buyNo = pTransaction['BuyNo'] sellNo = pTransaction['SellNo'] if buyNo > 0: # 买 item["OrderNO"] = buyNo item["Side"] = "1" elif sellNo > 0: # 卖 item["OrderNO"] = sellNo item["Side"] = "2" self.l2_data_upload_manager.add_l2_order_detail(item, 0, True) else: item = {"SecurityID": pTransaction['SecurityID'], "TradePrice": pTransaction['TradePrice'], "TradeVolume": pTransaction['TradeVolume'], "OrderTime": pTransaction['TradeTime'], "MainSeq": pTransaction['MainSeq'], "SubSeq": pTransaction['SubSeq'], "BuyNo": pTransaction['BuyNo'], "SellNo": pTransaction['SellNo'], "ExecType": pTransaction['ExecType'].decode()} self.l2_data_upload_manager.add_transaction_detail(item) def OnRtnOrderDetail(self, pOrderDetail): # 输出逐笔委托数据 # 上证OrderStatus=b"D"表示撤单 item = {"SecurityID": pOrderDetail['SecurityID'], "Price": pOrderDetail['Price'], "Volume": pOrderDetail['Volume'], "Side": pOrderDetail['Side'].decode(), "OrderType": pOrderDetail['OrderType'].decode(), "OrderTime": pOrderDetail['OrderTime'], "MainSeq": pOrderDetail['MainSeq'], "SubSeq": pOrderDetail['SubSeq'], "OrderNO": pOrderDetail['OrderNO'], "OrderStatus": pOrderDetail['OrderStatus'].decode()} self.l2_data_upload_manager.add_l2_order_detail(item, 0) def OnRtnNGTSTick(self, pTick): # logger_debug.info(f"OnRtnNGTSTick: {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'} 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_debug.exception(e) def OnRtnMarketData(self, pDepthMarketData, FirstLevelBuyNum, FirstLevelBuyOrderVolumes, FirstLevelSellNum, FirstLevelSellOrderVolumes): # 传入:时间,现价,成交总量,买1,买2,买3,买4,买5,卖1,卖2,卖3,卖4,卖5 try: buys = [(pDepthMarketData['BidPrice1'], pDepthMarketData['BidVolume1']), (pDepthMarketData['BidPrice2'], pDepthMarketData['BidVolume2']), (pDepthMarketData['BidPrice3'], pDepthMarketData['BidVolume3']), (pDepthMarketData['BidPrice4'], pDepthMarketData['BidVolume4']), (pDepthMarketData['BidPrice5'], pDepthMarketData['BidVolume5'])] sells = [ (pDepthMarketData['AskPrice1'], pDepthMarketData['AskVolume1']), (pDepthMarketData['AskPrice2'], pDepthMarketData['AskVolume2']), (pDepthMarketData['AskPrice3'], pDepthMarketData['AskVolume3']), (pDepthMarketData['AskPrice4'], pDepthMarketData['AskVolume4']), (pDepthMarketData['AskPrice5'], pDepthMarketData['AskVolume5']) ] d = {"dataTimeStamp": pDepthMarketData['DataTimeStamp'], "securityID": pDepthMarketData['SecurityID'], "lastPrice": pDepthMarketData['LastPrice'], "totalVolumeTrade": pDepthMarketData['TotalVolumeTrade'], "totalValueTrade": pDepthMarketData['TotalValueTrade'], "totalAskVolume": pDepthMarketData['TotalAskVolume'], "avgAskPrice": pDepthMarketData["AvgAskPrice"], "buy": buys, "sell": sells} huaxin_l2_log.info(logger_local_huaxin_l2_market, f"{d}") # market_code_dict[pDepthMarketData['SecurityID']] = time.time() self.l2_data_upload_manager.add_market_data(d) except: pass class MyL2ActionCallback(L2ActionCallback): def OnSetL2Position(self, codes): huaxin_l2_log.info(logger_l2_codes_subscript, "华鑫L2代码处理队列获取到数据:数量-{}", len(codes)) try: spi.set_codes_data(codes) except Exception as e: logging.exception(e) def __init_l2(l2_data_upload_manager): printlog(lev2mdapi.CTORATstpLev2MdApi_GetApiVersion()) # case 1: Tcp方式 # g_SubMode=lev2mdapi.TORA_TSTP_MST_TCP # case 2: 组播方式 g_SubMode = lev2mdapi.TORA_TSTP_MST_MCAST # case 1缓存模式 api = lev2mdapi.CTORATstpLev2MdApi_CreateTstpLev2MdApi(g_SubMode, True) # case 2非缓存模式 # api = lev2mdapi.CTORATstpLev2MdApi_CreateTstpLev2MdApi(g_SubMode, False) global spi spi = Lev2MdSpi(api, l2_data_upload_manager) api.RegisterSpi(spi) # -------------------正式模式------------------------------------- if g_SubMode != lev2mdapi.TORA_TSTP_MST_MCAST: api.RegisterFront(Front_Address) else: # case 1 从一个组播地址收取行情 api.RegisterMulticast(Multicast_Address, Local_Interface_Address, "") # api.RegisterMulticast(Multicast_Address2, Local_Interface_Address, "") # case 2:注册多个组播地址同时收行情 # api.RegisterMulticast(Multicast_Address, Local_Interface_Address, ""); # api.RegisterMulticast(Multicast_Address2, Local_Interface_Address, ""); # case 3:efvi模式收行情 # api.RegisterMulticast(Multicast_Address, Local_Interface_Address, "", "enp101s0f0",4096, True); # case 1 不绑核运行 api.Init() __l2_cmd_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=3) def __receive_from_queue_trade(queue_trade_w_l2_r: multiprocessing.Queue): logger_system.info(f"l2_client __receive_from_pipe_trade 线程ID:{tool.get_thread_id()}") while True: try: value = queue_trade_w_l2_r.get() if value: if type(value) == bytes: value = value.decode("utf-8") data = json.loads(value) _type = data["type"] if _type == "l2_cmd": huaxin_l2_log.info(logger_local_huaxin_l2_subscript, f"订阅代码:{data}") __start_time = time.time() # 线程池 __l2_cmd_thread_pool.submit( lambda: l2CommandManager.process_command(command_manager.CLIENT_TYPE_CMD_L2, None, data)) use_time = time.time() - __start_time if use_time > 0.005: huaxin_l2_log.info(logger_local_huaxin_l2_subscript, f"l2_cmd耗时:{use_time}s") except Exception as e: logging.exception(e) pipe_strategy = None def test_add_codes(queue_r): time.sleep(10) # if value: # if type(value) == bytes: # value = value.decode("utf-8") # data = json.loads(value) # _type = data["type"] # if _type == "listen_volume": # volume = data["data"]["volume"] # code = data["data"]["code"] # spi.set_code_special_watch_volume(code, volume) # elif _type == "l2_cmd": # l2CommandManager.process_command(command_manager.CLIENT_TYPE_CMD_L2, None, data) time.sleep(2) demo_datas = ["603002", "002654", "603701", "002908"] queue_r.put_nowait(json.dumps({"type": "l2_cmd", "data": demo_datas})) time.sleep(10) while True: try: spi.l2_data_upload_manager.add_l2_order_detail( {'SecurityID': '603002', 'Price': 6.35, 'Volume': 275000, 'Side': "1", 'OrderType': '0', 'OrderTime': '13000015', 'MainSeq': 2, 'SubSeq': 6739147, 'OrderNO': 5512466, 'OrderStatus': 'D'}, 0) spi.l2_data_upload_manager.add_l2_order_detail( {'SecurityID': '603002', 'Price': 6.35, 'Volume': 200, 'Side': "1", 'OrderType': '0', 'OrderTime': '13000015', 'MainSeq': 2, 'SubSeq': 6739148, 'OrderNO': 5512467, 'OrderStatus': 'D'}, 0) # queue_r.put_nowait(json.dumps({"type": "listen_volume", "data": {"code": "603002", "volume": 100}})) time.sleep(0.1) spi.l2_data_upload_manager.add_l2_order_detail( {'SecurityID': '603002', 'Price': 6.35, 'Volume': 100, 'Side': "1", 'OrderType': '0', 'OrderTime': '13000015', 'MainSeq': 2, 'SubSeq': 6739148, 'OrderNO': 5512467, 'OrderStatus': 'D'}, 0) except Exception as e: logging.exception(e) finally: time.sleep(10) def run(queue_r: multiprocessing.Queue, data_callbacks: list) -> None: logger_system.info("L2进程ID:{}", os.getpid()) logger_system.info(f"l2_client 线程ID:{tool.get_thread_id()}") try: log.close_print() if queue_r is not None: t1 = threading.Thread(target=lambda: __receive_from_queue_trade(queue_r), daemon=True) t1.start() # 初始化 data_callback_distribute_manager = CodeDataCallbackDistributeManager(data_callbacks) l2_data_upload_manager = L2DataUploadManager(data_callback_distribute_manager) __init_l2(l2_data_upload_manager) threading.Thread(target=huaxin_l2_log.run_sync, daemon=True).start() # TODO 测试 # threading.Thread(target=lambda: test_add_codes(queue_r), daemon=True).start() global l2CommandManager l2CommandManager = command_manager.L2CommandManager() l2CommandManager.init(MyL2ActionCallback()) logger_system.info("L2订阅服务启动成功") except Exception as e: logger_system.exception(e) while True: time.sleep(2) if __name__ == "__main__": input()