| | |
| | | |
| | | 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 |
| | |
| | | class sampleSpi(qcvalueaddproapi.CQCValueAddProSpi): |
| | | __result_cache = {} |
| | | __temp_cache = {} |
| | | # 指数数据 |
| | | stock_index_data_dict = {} |
| | | |
| | | def __init__(self, t_tapi): |
| | | qcvalueaddproapi.CQCValueAddProSpi.__init__(self) |
| | |
| | | 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") |
| | | # 连接上后去登录 |
| | |
| | | # 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() |
| | |
| | | |
| | | |
| | | 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}) |
| | | |
| | |
| | | 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 |
| | |
| | | |
| | | #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() |
| | | |
| | | |