| | |
| | | from huaxin_api import l2_data_manager |
| | | import lev2mdapi |
| | | from huaxin_api.command_manager import L2ActionCallback |
| | | from huaxin_api.mylog import logger_l2_orderdetail, logger_l2_transaction, logger_l2_subscript |
| | | from log_module import log |
| | | from log_module.log import logger_local_huaxin_l2_subscript, logger_local_huaxin_l2_orderdetail, \ |
| | | logger_local_huaxin_l2_transaction |
| | | |
| | | Front_Address = "tcp://10.0.1.101:6900" |
| | | Multicast_Address = "udp://224.224.2.19:7889" |
| | |
| | | # 取消订阅 |
| | | def __unsubscribe(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | logger_l2_subscript.info(f"取消订阅上证:{sh}") |
| | | logger_l2_subscript.info(f"取消订阅深证:{sz}") |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅上证:{sh}") |
| | | logger_local_huaxin_l2_subscript.info(f"取消订阅深证:{sz}") |
| | | if sh: |
| | | # 取消订阅逐笔委托 |
| | | self.__api.UnSubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | |
| | | |
| | | def __subscribe(self, _codes): |
| | | sh, sz = self.__split_codes(_codes) |
| | | logger_l2_subscript.info(f"订阅上证:{sh}") |
| | | logger_l2_subscript.info(f"订阅深证:{sz}") |
| | | logger_local_huaxin_l2_subscript.info(f"订阅上证:{sh}") |
| | | logger_local_huaxin_l2_subscript.info(f"订阅深证:{sz}") |
| | | if sh: |
| | | # 订阅逐笔委托 |
| | | result = self.__api.SubscribeOrderDetail(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_l2_subscript.info(f"逐笔委托订阅结果sh:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sh:{result}") |
| | | # 订阅逐笔成交 |
| | | result = self.__api.SubscribeTransaction(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sh:{result}") |
| | | |
| | | result = self.__api.SubscribeMarketData(sh, lev2mdapi.TORA_TSTP_EXD_SSE) |
| | | logger_l2_subscript.info(f"市场订阅结果sh:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"市场订阅结果sh:{result}") |
| | | if sz: |
| | | result = self.__api.SubscribeOrderDetail(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | logger_l2_subscript.info(f"逐笔委托订阅结果sz:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔委托订阅结果sz:{result}") |
| | | result = self.__api.SubscribeTransaction(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | logger_l2_subscript.info(f"逐笔成交订阅结果sz:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"逐笔成交订阅结果sz:{result}") |
| | | result = self.__api.SubscribeMarketData(sz, lev2mdapi.TORA_TSTP_EXD_SZSE) |
| | | logger_l2_subscript.info(f"市场订阅结果sz:{result}") |
| | | logger_local_huaxin_l2_subscript.info(f"市场订阅结果sz:{result}") |
| | | |
| | | def __process_codes_data(self, codes_data): |
| | | |
| | |
| | | # try: |
| | | print("订阅结果:", pSpecificSecurity["ExchangeID"], pSpecificSecurity["SecurityID"], pRspInfo["ErrorID"], |
| | | pRspInfo["ErrorMsg"]) |
| | | logger_l2_subscript.info( |
| | | logger_local_huaxin_l2_subscript.info( |
| | | f"订阅结果:{pSpecificSecurity['SecurityID']} {pRspInfo['ErrorID']} {pRspInfo['ErrorMsg']}") |
| | | if pRspInfo["ErrorID"] == 0: |
| | | print("订阅成功") |
| | |
| | | # print("逐笔成交", item) |
| | | l2_data_manager.add_transaction_detail(item) |
| | | |
| | | logger_l2_transaction.info( |
| | | logger_local_huaxin_l2_transaction.info( |
| | | "OnRtnTransaction SecurityID[%s] TradePrice[%.2f] TradeVolume[%d] TradeTime[%d] MainSeq[%d] SubSeq[%d] BuyNo[%d] SellNo[%d] ExecType[%s]" % ( |
| | | pTransaction['SecurityID'], |
| | | pTransaction['TradePrice'], |
| | |
| | | print("逐笔委托", item) |
| | | l2_data_manager.add_l2_order_detail(item) |
| | | |
| | | logger_l2_orderdetail.info( |
| | | logger_local_huaxin_l2_orderdetail.info( |
| | | "OnRtnOrderDetail SecurityID[%s] Price[%.2f] Volume[%d] Side[%s] OrderType[%s] OrderTime[%d] MainSeq[%d] SubSeq[%d] OrderNO[%s] OrderStatus[%s] Info1[%d] Info2[%d] Info3[%d]" % ( |
| | | pOrderDetail['SecurityID'], |
| | | pOrderDetail['Price'], |
| | |
| | | from huaxin_api import socket_util |
| | | |
| | | from huaxin_api.client_network import SendResponseSkManager |
| | | from huaxin_api.mylog import logger_l2_error, logger_l2_upload |
| | | |
| | | # 活动时间 |
| | | from log_module.log import logger_local_huaxin_l2_upload, logger_local_huaxin_l2_error |
| | | |
| | | order_detail_upload_active_time_dict = {} |
| | | transaction_upload_active_time_dict = {} |
| | | # 临时数据 |
| | |
| | | # print("请求开始", uid, len(datas), len(fdata), f"{fdata[:20]}...{fdata[-20:]}") |
| | | result = None |
| | | start_time = time.time() |
| | | logger_l2_upload.info(f"{code} 上传数据开始-{_type}") |
| | | logger_local_huaxin_l2_upload.info(f"{code} 上传数据开始-{_type}") |
| | | try: |
| | | result = send_response(key, fdata.encode('utf-8')) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | finally: |
| | | # print("请求结束", uid, result) |
| | | logger_l2_upload.info(f"{code} 上传数据耗时-{_type}: {round((time.time() - start_time) * 1000, 1)} 数据量:{len(datas)}") |
| | | logger_local_huaxin_l2_upload.info(f"{code} 上传数据耗时-{_type}: {round((time.time() - start_time) * 1000, 1)} 数据量:{len(datas)}") |
| | | # print("上传结果", result) |
| | | |
| | | |
| | |
| | | time.sleep(0.01) |
| | | |
| | | except Exception as e: |
| | | logger_l2_error.error(f"上传订单数据出错:{str(e)}") |
| | | logger_local_huaxin_l2_error.error(f"上传订单数据出错:{str(e)}") |
| | | pass |
| | | |
| | | |
| | |
| | | upload_data(code, "l2_trans", udatas) |
| | | time.sleep(0.01) |
| | | except Exception as e: |
| | | logger_l2_error.error(f"上传成交数据出错:{str(e)}") |
| | | logger_local_huaxin_l2_error.error(f"上传成交数据出错:{str(e)}") |
| | | |
| | | |
| | | def __run_upload_common(): |
| | |
| | | upload_data(temp[0], temp[1], temp[2]) |
| | | time.sleep(0.01) |
| | | except Exception as e: |
| | | logger_l2_error.exception(e) |
| | | logger_l2_error.error(f"上传普通数据出错:{str(e)}") |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | logger_local_huaxin_l2_error.error(f"上传普通数据出错:{str(e)}") |
| | | |
| | | |
| | | # 运行上传任务 |
| | |
| | | from huaxin_api.log import logger |
| | | |
| | | # 正式账号 |
| | | from huaxin_api.mylog import logger_trade_debug |
| | | from log_module.log import logger_local_huaxin_trade_debug |
| | | |
| | | UserID = '388000013349' |
| | | # 登陆密码 |
| | |
| | | % (nRequestID, pRspInfoField.ErrorID, pRspInfoField.ErrorMsg)) |
| | | |
| | | def OnRtnOrder(self, pOrderField: "CTORATstpOrderField") -> "void": |
| | | logger_trade_debug.info( |
| | | logger_local_huaxin_trade_debug.info( |
| | | '[%d] OnRtnOrder: SInfo[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] LimitPrice[%.2f] VolumeTotalOriginal[%d] OrderSysID[%s] OrderStatus[%s]' |
| | | % (round(time.time() * 1000), pOrderField.SInfo, pOrderField.InvestorID, pOrderField.SecurityID, |
| | | pOrderField.OrderRef, pOrderField.OrderLocalID, |
| | |
| | | daemon=True).start() |
| | | |
| | | def OnRtnTrade(self, pTradeField: "CTORATstpTradeField") -> "void": |
| | | logger_trade_debug.info( |
| | | logger_local_huaxin_trade_debug.info( |
| | | 'OnRtnTrade: TradeID[%s] InvestorID[%s] SecurityID[%s] OrderRef[%d] OrderLocalID[%s] Price[%.2f] Volume[%d]' |
| | | % (pTradeField.TradeID, pTradeField.InvestorID, pTradeField.SecurityID, |
| | | pTradeField.OrderRef, pTradeField.OrderLocalID, pTradeField.Price, pTradeField.Volume)) |
| | |
| | | |
| | | def OnTrade(self, client_id, request_id, type_, data): |
| | | if type_ == 1: |
| | | logger_trade_debug.info(f"---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info(f"---------------------\n请求下单:client_id-{client_id} request_id-{request_id}") |
| | | # 下单 |
| | | # 1-买 2-卖 |
| | | direction = data["direction"] |
| | |
| | | request_id) |
| | | |
| | | elif type_ == 2: |
| | | logger_trade_debug.info( |
| | | logger_local_huaxin_trade_debug.info( |
| | | f"---------------------\n请求撤单:client_id-{client_id} request_id-{request_id} data-{data}") |
| | | # 撤单 |
| | | direction = data["direction"] |
| | |
| | | request_id) |
| | | |
| | | def OnDealList(self, client_id, request_id): |
| | | logger_trade_debug.info(f"请求成交列表:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info(f"请求成交列表:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | print("开始请求成交列表") |
| | | req_id = self.__tradeSimpleApi.list_traded_orders() |
| | |
| | | SendResponseSkManager.send_error_response("common", request_id, client_id, str(e)) |
| | | |
| | | def OnDelegateList(self, client_id, request_id, is_cancel): |
| | | logger_trade_debug.info(f"请求委托列表:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info(f"请求委托列表:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.list_delegate_orders(is_cancel) |
| | | req_rid_dict[req_id] = (client_id, request_id) |
| | |
| | | request_id) |
| | | |
| | | def OnMoney(self, client_id, request_id): |
| | | logger_trade_debug.info(f"请求账户:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info(f"请求账户:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.get_money_account() |
| | | req_rid_dict[req_id] = (client_id, request_id) |
| | |
| | | request_id) |
| | | |
| | | def OnPositionList(self, client_id, request_id): |
| | | logger_trade_debug.info(f"请求持仓:client_id-{client_id} request_id-{request_id}") |
| | | logger_local_huaxin_trade_debug.info(f"请求持仓:client_id-{client_id} request_id-{request_id}") |
| | | try: |
| | | req_id = self.__tradeSimpleApi.list_positions() |
| | | req_rid_dict[req_id] = (client_id, request_id) |
| | |
| | | |
| | | # 交易反馈回调 |
| | | def __traderapi_callback(type, req_id, data): |
| | | logger_trade_debug.info("回调:type-{} req_id-{}", type, req_id) |
| | | logger_local_huaxin_trade_debug.info("回调:type-{} req_id-{}", type, req_id) |
| | | key = req_id |
| | | if type == TYPE_ORDER or type == TYPE_CANCEL_ORDER: |
| | | key = data["sinfo"] |
| | |
| | | local_order_id = temp_params[2] |
| | | data["localOrderId"] = local_order_id |
| | | |
| | | logger_trade_debug.info("API回调 request_id-{}", request_id) |
| | | logger_local_huaxin_trade_debug.info("API回调 request_id-{}", request_id) |
| | | # 测试 |
| | | send_response( |
| | | json.dumps({"type": "response", "data": {"code": 0, "data": data}, "client_id": client_id, |
| | | "request_id": request_id}), type, client_id, request_id) |
| | | logger_trade_debug.info("API回调结束 req_id-{} request_id-{}", req_id, request_id) |
| | | logger_local_huaxin_trade_debug.info("API回调结束 req_id-{} request_id-{}", req_id, request_id) |
| | | print("API回调结束") |
| | | else: |
| | | logger_trade_debug.info("非API回调 req_id-{}", req_id) |
| | | logger_local_huaxin_trade_debug.info("非API回调 req_id-{}", req_id) |
| | | print("非API回调") |
| | | # 非API回调 |
| | | send_response( |
| | |
| | | """ |
| | | 日志 |
| | | """ |
| | | import logging |
| | | import os |
| | | import sys |
| | | from loguru import logger |
| | | import constant |
| | |
| | | filter=lambda record: record["extra"].get("name") == "code_operate", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | # 显示在控制台 |
| | | logger.add(sys.stdout, |
| | | filter=lambda record: record["extra"].get("name") == "code_operate", enqueue=True) |
| | | # logger.add(sys.stdout, |
| | | # filter=lambda record: record["extra"].get("name") == "code_operate", enqueue=True) |
| | | |
| | | logger.add(self.get_path("device", "device"), filter=lambda record: record["extra"].get("name") == "device", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | |
| | | filter=lambda record: record["extra"].get("name") == "hx_trade_loop", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | logger.add(self.get_local_huaxin_path("l2", "transaction"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_transaction", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | logger.add(self.get_local_huaxin_path("l2", "orderdetail"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_orderdetail", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | logger.add(self.get_local_huaxin_path("l2", "upload"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_upload", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | logger.add(self.get_local_huaxin_path("l2", "subscript"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_subscript", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | logger.add(self.get_local_huaxin_path("contact", "debug"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_debug", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | logger.add(self.get_local_huaxin_path("trade", "trade_debug"), |
| | | filter=lambda record: record["extra"].get("name") == "local_huaxin_trade_debug", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | def get_path(self, dir_name, log_name): |
| | | path_str = "{}/logs/gp/{}/{}".format(constant.get_path_prefix(), dir_name, log_name) + ".{time:YYYY-MM-DD}.log" |
| | | # print(path_str) |
| | | return path_str |
| | | |
| | | def get_hx_path(self, dir_name, log_name): |
| | | path_str = "{}/logs/huaxin/{}/{}".format(constant.get_path_prefix(), dir_name, log_name) + ".{time:YYYY-MM-DD}.log" |
| | | path_str = "{}/logs/huaxin/{}/{}".format(constant.get_path_prefix(), dir_name, |
| | | log_name) + ".{time:YYYY-MM-DD}.log" |
| | | # print(path_str) |
| | | return path_str |
| | | |
| | | def get_local_huaxin_path(self, dir_name, log_name): |
| | | path_str = "{}/logs/huaxin_local/{}/{}".format(constant.get_path_prefix(), dir_name, |
| | | log_name) + ".{time:YYYY-MM-DD}.log" |
| | | # print(path_str) |
| | | return path_str |
| | | |
| | |
| | | hx_logger_trade_debug = __mylogger.get_logger("hx_trade_debug") |
| | | hx_logger_trade_loop = __mylogger.get_logger("hx_trade_loop") |
| | | |
| | | # -------------------------------华鑫本地日志--------------------------------- |
| | | logger_local_huaxin_l2_transaction = __mylogger.get_logger("local_huaxin_transaction") |
| | | logger_local_huaxin_l2_orderdetail = __mylogger.get_logger("local_huaxin_orderdetail") |
| | | logger_local_huaxin_l2_upload = __mylogger.get_logger("local_huaxin_upload") |
| | | logger_local_huaxin_l2_error = __mylogger.get_logger("local_huaxin_error") |
| | | logger_local_huaxin_l2_subscript = __mylogger.get_logger("local_huaxin_subscript") |
| | | logger_local_huaxin_contact_debug = __mylogger.get_logger("local_huaxin_debug") |
| | | logger_local_huaxin_trade_debug = __mylogger.get_logger("local_huaxin_trade_debug") |
| | | |
| | | |
| | | def close_print(): |
| | | if True: |
| | | return |
| | | logging.basicConfig(level=logging.ERROR) |
| | | if not constant.is_windows(): |
| | | os.close(1) |
| | | os.open('/dev/null', os.O_WRONLY) |
| | | pass |
| | | # logging.basicConfig(level=logging.ERROR) |
| | | # if not constant.is_windows(): |
| | | # os.close(1) |
| | | # os.open('/dev/null', os.O_WRONLY) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | logger_l2_trade.info("测试") |
| | | input() |