a
This commit is contained in:
parent
2b7e26fd36
commit
e06687b7a5
|
|
@ -407,22 +407,55 @@ async def close_position(symbol: str):
|
||||||
side = 'SELL' if position_amt > 0 else 'BUY'
|
side = 'SELL' if position_amt > 0 else 'BUY'
|
||||||
quantity = abs(position_amt)
|
quantity = abs(position_amt)
|
||||||
|
|
||||||
logger.info(f"开始执行平仓操作: {symbol} {side} {quantity:.4f} @ MARKET...")
|
logger.info(f"开始执行平仓操作: {symbol} {side} {quantity:.4f} @ MARKET (reduceOnly=true)...")
|
||||||
|
|
||||||
# 直接调用 BinanceClient 平仓(使用 reduceOnly=True)
|
# 直接调用币安 API 平仓,绕过 BinanceClient 的检查逻辑
|
||||||
order = await client.place_order(
|
# 对于平仓操作,应该直接使用币安 API,不需要名义价值和保证金检查
|
||||||
|
try:
|
||||||
|
# 获取交易对精度信息,调整数量精度
|
||||||
|
symbol_info = await client.get_symbol_info(symbol)
|
||||||
|
if symbol_info:
|
||||||
|
quantity_precision = symbol_info.get('quantityPrecision', 8)
|
||||||
|
step_size = symbol_info.get('stepSize', 0)
|
||||||
|
min_qty = symbol_info.get('minQty', 0)
|
||||||
|
|
||||||
|
# 调整数量精度
|
||||||
|
if step_size > 0:
|
||||||
|
adjusted_quantity = float(int(quantity / step_size)) * step_size
|
||||||
|
else:
|
||||||
|
adjusted_quantity = round(quantity, quantity_precision)
|
||||||
|
|
||||||
|
# 确保不小于最小数量
|
||||||
|
if min_qty > 0 and adjusted_quantity < min_qty:
|
||||||
|
adjusted_quantity = min_qty
|
||||||
|
|
||||||
|
adjusted_quantity = round(adjusted_quantity, quantity_precision)
|
||||||
|
if adjusted_quantity != quantity:
|
||||||
|
logger.info(f"数量精度调整: {quantity} -> {adjusted_quantity}")
|
||||||
|
quantity = adjusted_quantity
|
||||||
|
|
||||||
|
# 直接调用币安 API 下单(使用 reduceOnly="true")
|
||||||
|
order = await client.client.futures_create_order(
|
||||||
symbol=symbol,
|
symbol=symbol,
|
||||||
side=side,
|
side=side,
|
||||||
|
type='MARKET',
|
||||||
quantity=quantity,
|
quantity=quantity,
|
||||||
order_type='MARKET',
|
reduceOnly="true" # 使用字符串格式,符合币安API要求
|
||||||
reduce_only=True
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not order:
|
if not order:
|
||||||
error_msg = f"{symbol} 平仓失败:下单返回 None,请检查日志"
|
error_msg = f"{symbol} 平仓失败:币安API返回 None"
|
||||||
logger.error(error_msg)
|
logger.error(error_msg)
|
||||||
raise HTTPException(status_code=500, detail=error_msg)
|
raise HTTPException(status_code=500, detail=error_msg)
|
||||||
|
|
||||||
|
except Exception as order_error:
|
||||||
|
error_msg = f"{symbol} 平仓失败:下单异常 - {str(order_error)}"
|
||||||
|
logger.error(error_msg)
|
||||||
|
logger.error(f" 错误类型: {type(order_error).__name__}")
|
||||||
|
import traceback
|
||||||
|
logger.error(f" 完整错误堆栈:\n{traceback.format_exc()}")
|
||||||
|
raise HTTPException(status_code=500, detail=error_msg)
|
||||||
|
|
||||||
order_id = order.get('orderId')
|
order_id = order.get('orderId')
|
||||||
logger.info(f"✓ {symbol} 平仓订单已提交 (订单ID: {order_id})")
|
logger.info(f"✓ {symbol} 平仓订单已提交 (订单ID: {order_id})")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user