From 90691e84cf103449eae88c0a69a50b9f41f18da2 Mon Sep 17 00:00:00 2001 From: zhiyong Date: Tue, 13 May 2025 00:31:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8F=AA=E6=9C=89=E5=AE=9E=E7=9B=98?= =?UTF-8?q?=E5=9C=A8=E9=9D=9E=E4=BA=A4=E6=98=93=E6=97=B6=E9=97=B4=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/trade_server.py | 76 ++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/src/trade_server.py b/src/trade_server.py index 6f31149..a82d384 100644 --- a/src/trade_server.py +++ b/src/trade_server.py @@ -285,27 +285,29 @@ def buy(): if order_type == ORDER_TYPE_LIMIT and (price <= 0 or amount <= 0): raise ValueError("Price and amount must be positive") - # 检查交易系统是否可用 trader = get_trader() - if is_real_mode() and not trader.is_available(): - return jsonify({ - "success": False, - "error": trader.connection_error_message or "交易系统连接失败,请稍后再试" - }), 503 - - # 检查是否在交易时间内 - if not BaseTrader.is_trading_time(): - logger.warning( - f"交易失败 - 非交易时间不能交易 - 代码: {code}, 价格: {price}, 数量: {amount}" - ) - return ( - jsonify( - {"success": False, "error": f"交易失败: 非交易时间不能实盘交易"} - ), - 400, - ) if is_real_mode(): + # 检查是否在交易时间内 + if not BaseTrader.is_trading_time(): + logger.warning( + f"交易失败 - 非交易时间不能交易 - 代码: {code}, 价格: {price}, 数量: {amount}" + ) + return ( + jsonify( + {"success": False, "error": f"交易失败: 非交易时间不能实盘交易"} + ), + 400, + ) + + # 检查交易系统是否可用 + if is_real_mode() and not trader.is_available(): + return jsonify({ + "success": False, + "error": trader.connection_error_message or "交易系统连接失败,请稍后再试" + }), 503 + + # 开始买入 logger.info( f"使用RealTraderManager执行买入: 代码={code}, 价格={price}, 数量={amount}, 策略={strategy_name}" ) @@ -354,27 +356,29 @@ def sell(): if order_type == ORDER_TYPE_LIMIT and (price <= 0 or amount <= 0): raise ValueError("Price and amount must be positive") - # 检查交易系统是否可用 trader = get_trader() - if is_real_mode() and not trader.is_available(): - return jsonify({ - "success": False, - "error": trader.connection_error_message or "交易系统连接失败,请稍后再试" - }), 503 - - # 检查是否在交易时间内 - if not BaseTrader.is_trading_time(): - logger.warning( - f"交易失败 - 非交易时间不能交易 - 代码: {code}, 价格: {price}, 数量: {amount}" - ) - return ( - jsonify( - {"success": False, "error": f"交易失败: 非交易时间不能实盘交易"} - ), - 400, - ) if is_real_mode(): + # 检查是否在交易时间内 + if not BaseTrader.is_trading_time(): + logger.warning( + f"交易失败 - 非交易时间不能交易 - 代码: {code}, 价格: {price}, 数量: {amount}" + ) + return ( + jsonify( + {"success": False, "error": f"交易失败: 非交易时间不能实盘交易"} + ), + 400, + ) + + # 检查交易系统是否可用 + if not trader.is_available(): + return jsonify({ + "success": False, + "error": trader.connection_error_message or "交易系统连接失败,请稍后再试" + }), 503 + + # 开始卖出 logger.info( f"使用RealTraderManager执行卖出: 代码={code}, 价格={price}, 数量={amount}, 策略={strategy_name}" )