Administrator
2024-02-19 0837c43e2d6ebb82c9712b9e5e64dbbb3dcd353f
客户端手动买入调整
1个文件已修改
41 ■■■■ 已修改文件
trade/huaxin/huaxin_trade_server.py 41 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_server.py
@@ -289,6 +289,7 @@
    __sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10)
    __process_l1_data_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=10)
    __current_price_dict = {}
    current_buy1_dict = {}
    @classmethod
    def __sell(cls, datas):
@@ -351,6 +352,7 @@
            # 格式 (代码,现价,涨幅,量,更新时间,买1价格,买1量)
            price = d[1]
            cls.__current_price_dict[code] = price
            cls.current_buy1_dict[code] = (d[5], d[6])
    # 获取L1现价
    @classmethod
@@ -635,6 +637,12 @@
            except Exception as e1:
                logging.exception(e1)
    # 撤长期没有成交的单
    def __cancel_not_deal_order(self, code, order_ref, timeout=2):
        time.sleep(timeout)
        # 撤买单
        huaxin_trade_api.cancel_order(1, code, "", orderRef = order_ref)
    # 交易
    def OnTrade(self, client_id, request_id, data):
        try:
@@ -665,8 +673,27 @@
                        else:
                            raise e
                else:
                    result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type, sinfo=sinfo,
                                                    blocking=True, request_id=request_id)
                    if not price:
                        # 没有传入价格,以最新价买入
                        current_price = TradeServerProcessor.get_l1_current_price(code)
                        if not current_price:
                            raise Exception("尚未获取到现价")
                        # 获取买1金额
                        price = round(float(current_price), 2)
                        buy1_info = TradeServerProcessor.current_buy1_dict.get(code)
                        if buy1_info and buy1_info[0] * buy1_info[1] > 50 * 10000:
                            # 如果买1在50w以上就加一档
                            price += 0.01
                        order_ref = huaxin_util.create_order_ref()
                        result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type,
                                                        sinfo=sinfo, order_ref=order_ref,
                                                        blocking=True, request_id=request_id)
                        # 2s内没成交就撤单
                        self.__cancel_sell_thread_pool.submit(self.__cancel_not_deal_order, code, order_ref)
                    else:
                        result = huaxin_trade_api.order(direction, code, volume, price, price_type=price_type,
                                                        sinfo=sinfo,
                                                        blocking=True, request_id=request_id)
                    self.send_response({"code": 0, "data": result}, client_id, request_id)
            elif trade_type == outside_api_command_manager.TRADE_TYPE_CANCEL_ORDER:
                print("手动撤单:", data)
@@ -1192,10 +1219,7 @@
                    fdata["total"] = d["prePosition"]
                    fdata["available"] = d["availablePosition"]
                    fdata["cost_price"] = round(float(d["historyPosPrice"]), 2)
                    # 有现价就获取现价
                    current_price = TradeServerProcessor.get_l1_current_price(code)
                    if current_price:
                        fdata["cost_price"] = current_price
                    deal_order_system_id_infos = {}
                    # 获取已经卖的单数
                    deal_list = self.__DealRecordManager.list_sell_by_code_cache(code)
@@ -1220,6 +1244,11 @@
                    fdata["sell_orders"] = [k[0] for k in deal_list]
                    break
            # 有现价就获取现价
            current_price = TradeServerProcessor.get_l1_current_price(code)
            if current_price:
                fdata["cost_price"] = current_price
            async_log_util.info(logger_trade_position_api_request, f"{fdata}")
            result = {"code": 0, "data": fdata}
            self.send_response(result, client_id, request_id)