a
This commit is contained in:
parent
6d1f45ac9a
commit
ba15992aeb
|
|
@ -569,6 +569,48 @@ class BinanceClient:
|
||||||
|
|
||||||
return adjusted
|
return adjusted
|
||||||
|
|
||||||
|
def _adjust_quantity_precision_up(self, quantity: float, symbol_info: Dict) -> float:
|
||||||
|
"""
|
||||||
|
向上取整调整数量精度,使其符合币安要求
|
||||||
|
|
||||||
|
Args:
|
||||||
|
quantity: 原始数量
|
||||||
|
symbol_info: 交易对信息
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
调整后的数量(向上取整)
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
|
||||||
|
if not symbol_info:
|
||||||
|
# 如果没有交易对信息,向上取整到3位小数
|
||||||
|
return round(math.ceil(quantity * 1000) / 1000, 3)
|
||||||
|
|
||||||
|
quantity_precision = symbol_info.get('quantityPrecision', 8)
|
||||||
|
step_size = symbol_info.get('stepSize', 0)
|
||||||
|
min_qty = symbol_info.get('minQty', 0)
|
||||||
|
|
||||||
|
# 如果有stepSize,按照stepSize向上取整
|
||||||
|
if step_size > 0:
|
||||||
|
# 向上取整到stepSize的倍数
|
||||||
|
adjusted = math.ceil(quantity / step_size) * step_size
|
||||||
|
else:
|
||||||
|
# 否则按照精度向上取整
|
||||||
|
multiplier = 10 ** quantity_precision
|
||||||
|
adjusted = math.ceil(quantity * multiplier) / multiplier
|
||||||
|
|
||||||
|
# 确保不小于最小数量
|
||||||
|
if min_qty > 0 and adjusted < min_qty:
|
||||||
|
adjusted = min_qty
|
||||||
|
|
||||||
|
# 最终精度调整
|
||||||
|
adjusted = round(adjusted, quantity_precision)
|
||||||
|
|
||||||
|
if adjusted != quantity:
|
||||||
|
logger.info(f"数量向上取整调整: {quantity} -> {adjusted} (精度: {quantity_precision}, stepSize: {step_size}, minQty: {min_qty})")
|
||||||
|
|
||||||
|
return adjusted
|
||||||
|
|
||||||
async def place_order(
|
async def place_order(
|
||||||
self,
|
self,
|
||||||
symbol: str,
|
symbol: str,
|
||||||
|
|
@ -667,12 +709,32 @@ class BinanceClient:
|
||||||
# 调整数量以满足最小保证金要求
|
# 调整数量以满足最小保证金要求
|
||||||
if current_price > 0:
|
if current_price > 0:
|
||||||
new_quantity = required_notional_value / current_price
|
new_quantity = required_notional_value / current_price
|
||||||
# 调整到符合精度要求
|
# 先尝试向下取整调整
|
||||||
adjusted_quantity = self._adjust_quantity_precision(new_quantity, symbol_info)
|
adjusted_quantity = self._adjust_quantity_precision(new_quantity, symbol_info)
|
||||||
# 重新计算名义价值和保证金
|
# 重新计算名义价值和保证金
|
||||||
notional_value = adjusted_quantity * current_price
|
notional_value = adjusted_quantity * current_price
|
||||||
required_margin = notional_value / current_leverage
|
required_margin = notional_value / current_leverage
|
||||||
|
|
||||||
|
# 如果调整后保证金仍然不足,使用向上取整
|
||||||
|
if required_margin < min_margin_usdt:
|
||||||
|
logger.warning(
|
||||||
|
f" ⚠ 向下取整后保证金仍不足: {required_margin:.4f} USDT < {min_margin_usdt:.2f} USDT"
|
||||||
|
)
|
||||||
|
adjusted_quantity = self._adjust_quantity_precision_up(new_quantity, symbol_info)
|
||||||
|
# 重新计算名义价值和保证金
|
||||||
|
notional_value = adjusted_quantity * current_price
|
||||||
|
required_margin = notional_value / current_leverage
|
||||||
|
|
||||||
|
# 再次检查保证金
|
||||||
|
if required_margin < min_margin_usdt:
|
||||||
|
logger.error(
|
||||||
|
f" ❌ 调整后保证金仍不足: {required_margin:.4f} USDT < {min_margin_usdt:.2f} USDT"
|
||||||
|
)
|
||||||
|
logger.error(
|
||||||
|
f" 💡 建议: 增加账户余额或降低杠杆倍数,才能满足最小保证金要求"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f" ✓ 调整数量: {quantity:.4f} -> {adjusted_quantity:.4f}, "
|
f" ✓ 调整数量: {quantity:.4f} -> {adjusted_quantity:.4f}, "
|
||||||
f"名义价值: {notional_value:.2f} USDT, "
|
f"名义价值: {notional_value:.2f} USDT, "
|
||||||
|
|
@ -682,6 +744,14 @@ class BinanceClient:
|
||||||
logger.error(f" ❌ 无法获取 {symbol} 的当前价格,无法调整订单大小")
|
logger.error(f" ❌ 无法获取 {symbol} 的当前价格,无法调整订单大小")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# 最终检查:确保调整后的保证金满足要求
|
||||||
|
if required_margin < min_margin_usdt:
|
||||||
|
logger.error(
|
||||||
|
f"❌ {symbol} 订单保证金不足: {required_margin:.4f} USDT < "
|
||||||
|
f"最小保证金要求: {min_margin_usdt:.2f} USDT,拒绝下单"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
f" 保证金检查通过: {required_margin:.4f} USDT >= "
|
f" 保证金检查通过: {required_margin:.4f} USDT >= "
|
||||||
f"最小要求: {min_margin_usdt:.2f} USDT (杠杆: {current_leverage}x)"
|
f"最小要求: {min_margin_usdt:.2f} USDT (杠杆: {current_leverage}x)"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user