admin
昨天 9b2ca188bb84998f089a03f3e5fa46a7a439399f
huaxin_client/l1_api_client.py
@@ -5,7 +5,7 @@
import constant
import qcvalueaddproapi
from log_module.log import logger_system
from log_module.log import logger_system, logger_debug
from utils import tool
global g_userid, g_passwd, g_address, g_port, g_seqnum
@@ -22,6 +22,8 @@
class sampleSpi(qcvalueaddproapi.CQCValueAddProSpi):
    __result_cache = {}
    __temp_cache = {}
    # 指数数据
    stock_index_data_dict = {}
    def __init__(self, t_tapi):
        qcvalueaddproapi.CQCValueAddProSpi.__init__(self)
@@ -157,6 +159,17 @@
        except Exception as e:
            logging.exception(e)
    def __subStockIndex(self):
        """
        订阅股票指数行情
        """
        # 沪深300
        self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_COMM, "000300")  # 沪深300
        self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "000300")
        self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SSE, "000001")  # 上证
        self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "399006")  # 创业板指数
        self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "399001")  # 深圳成指
    def OnFrontConnected(self):
        print("OnFrontConnected")
        # 连接上后去登录
@@ -184,6 +197,27 @@
            # threading.Thread(target=lambda: print("交易日历:", self.queryTradeCalendar("2024-03-08", "2024-12-31"))).start()
            # threading.Thread(
            #     target=lambda: print("日K:", self.queryBars("601298", "2024-12-15", "2024-12-31"))).start()
            try:
                self.__subStockIndex()
            except:
                pass
    def OnRtnStockIndexData(self, pStockIndexData):
        # 指数数据
        try:
            data = {
                "PreClosePrice":pStockIndexData.PreClosePrice,
                "LastPrice": pStockIndexData.LastPrice,
                "SecurityID": pStockIndexData.SecurityID,
                "UpdateTime": pStockIndexData.UpdateTime,
                "Volume": pStockIndexData.Volume,
                "Turnover": pStockIndexData.Turnover,
                "LXLastPrice": pStockIndexData.LXLastPrice,
            }
            self.stock_index_data_dict[data["SecurityID"]] = data
            # logger_debug.info(f"指数行情应答:{data}")
        except Exception as e:
            logging.exception(e)
    def ReqQryGGTEODPrices(self):
        QryField = qcvalueaddproapi.CQCVDQryGGTEODPricesField()
@@ -244,7 +278,6 @@
def __read_request(request_queue: multiprocessing.Queue, response_queue: multiprocessing.Queue):
    def __set_response_data(request_id, response_data):
        response_queue.put_nowait({"request_id": request_id, "data": response_data})
@@ -271,11 +304,25 @@
            pass
def run(request_queue: multiprocessing.Queue, response_queue: multiprocessing.Queue):
def __upload_datas(data_queue: multiprocessing.Queue):
    # 1s上传一次
    while True:
        try:
            if thespi.stock_index_data_dict:
                data_queue.put_nowait(("stock_index_datas", thespi.stock_index_data_dict))
        except:
            pass
        finally:
            time.sleep(1)
def run(request_queue: multiprocessing.Queue, response_queue: multiprocessing.Queue,
        data_callback_queue: multiprocessing.Queue):
    """
    运行
    @param request_queue: 请求队列
    @param response_queue: 响应队列
    @param data_callback_queue: 数据回调
    @return:
    """
    global g_userid, g_passwd, g_address, g_port
@@ -290,18 +337,17 @@
    #IP(192.168.84.61)、端口(25557
    # 回测交易是由历史行情来驱动撮合成交:
    # 因此必须同时使用traderapi和mdapi,不能单独使用traderapi,并且mdapi至少需要订阅一个以上行情。
    # 用户可使用回测traderapi的RegisterFront函数来注册此地址去连接上回测服务器
    print("GetApiVersion():", qcvalueaddproapi.CQCValueAddProApi_GetApiVersion())
    theapi = qcvalueaddproapi.CQCValueAddProApi_CreateInfoQryApi()
    global  thespi
    global thespi
    thespi = sampleSpi(theapi)
    theapi.RegisterSpi(thespi)
    theapi.RegisterFront(g_address, g_port)
    threading.Thread(target=__read_request, args=(request_queue, response_queue, ), daemon=True).start()
    threading.Thread(target=__read_request, args=(request_queue, response_queue,), daemon=True).start()
    threading.Thread(target=__upload_datas, args=(data_callback_queue,), daemon=True).start()
    theapi.Run()