diff --git a/trading_system/binance_client.py b/trading_system/binance_client.py index 299c8c9..c93d8c6 100644 --- a/trading_system/binance_client.py +++ b/trading_system/binance_client.py @@ -1399,8 +1399,13 @@ class BinanceClient: order = await _submit(order_params) except BinanceAPIException as e: code = getattr(e, "code", None) - if code in (-4061, -1106): + if code in (-4061, -1106, -1022): retry_params = dict(order_params) + # 关键修复:重试时必须清除之前的 timestamp 和 signature, + # 让 python-binance 重新生成,否则会报 -1022 Signature invalid + retry_params.pop('timestamp', None) + retry_params.pop('signature', None) + if code == -4061: logger.error(f"{symbol} 触发 -4061(持仓模式不匹配),尝试自动兜底重试一次") if "positionSide" in retry_params: diff --git a/trading_system/position_manager.py b/trading_system/position_manager.py index a3c97ef..25e7683 100644 --- a/trading_system/position_manager.py +++ b/trading_system/position_manager.py @@ -1509,14 +1509,25 @@ class PositionManager: except Exception as e: logger.debug(f"{symbol} 检查止盈触发条件时出错: {e}") - tp_order = await self.client.place_trigger_close_position_order( - symbol=symbol, - position_direction=side, - trigger_type="TAKE_PROFIT_MARKET", - stop_price=take_profit, - current_price=current_price, - working_type="MARK_PRICE", - ) + try: + tp_order = await self.client.place_trigger_close_position_order( + symbol=symbol, + position_direction=side, + trigger_type="TAKE_PROFIT_MARKET", + stop_price=take_profit, + current_price=current_price, + working_type="MARK_PRICE", + ) + except Exception as e: + # 处理 -2021: Order would immediately trigger + error_msg = str(e) + if "-2021" in error_msg or "immediately trigger" in error_msg: + logger.warning(f"{symbol} ⚠️ 止盈单会立即触发(-2021),视为已到达止盈位,立即执行市价止盈") + await self.close_position(symbol, reason='take_profit') + return + else: + logger.warning(f"{symbol} 挂止盈单失败: {e}") + tp_order = None if tp_order: logger.info(f"{symbol} ✓ 止盈单已成功挂到交易所: {tp_order.get('algoId', 'N/A')}") else: