fix(trade): LIMIT calculated quantity algorithm

Closes #6
This commit is contained in:
2025-08-10 03:01:45 +08:00
parent 49904b3822
commit 82314a022b

20
main.py
View File

@@ -368,18 +368,23 @@ class MexcSpotTrade:
order_type = order_type.upper() order_type = order_type.upper()
processed_kwargs = kwargs.copy() processed_kwargs = kwargs.copy()
# 记录未经过偏移的价格以供LIMIT订单只有quoteOrderQty没有quantity的情况使用
# 参数有price时直接使用否则就为实时价格
clean_price = processed_kwargs.get("price")
# 处理无price的情况获取实时价格 # 处理无price的情况获取实时价格
if order_type in ["LIMIT", "LIMIT_MAKER"] and "price" not in processed_kwargs: if order_type in ["LIMIT", "LIMIT_MAKER"] and "price" not in processed_kwargs:
current_price = self.market.get_price(symbol) current_price = self.market.get_price(symbol)
if current_price is None: if current_price is None:
logger.error("无法获取实时价格,交易取消") logger.error("无法获取实时价格,交易取消")
return None return None
clean_price = current_price
# 防止挂单不成交 # 防止挂单不成交
if side == "BUY": if side == "BUY":
processed_kwargs["price"] = current_price * 1.01 # 买入加价0.5% processed_kwargs["price"] = current_price * 1.01 # 买入加价1%
elif side == "SELL": elif side == "SELL":
processed_kwargs["price"] = current_price * 0.91 # 卖出减价0.5% processed_kwargs["price"] = current_price * 0.99 # 卖出减价1%
logger.info("使用调整0.5%%后价格作为限价: %f", processed_kwargs["price"]) logger.info("使用调整1%%后价格作为限价: %f", processed_kwargs["price"])
# 处理LIMIT订单只有quoteOrderQty没有quantity的情况 # 处理LIMIT订单只有quoteOrderQty没有quantity的情况
if ( if (
@@ -390,12 +395,7 @@ class MexcSpotTrade:
try: try:
exchange_info = self.market.get_exchange_info(symbol) exchange_info = self.market.get_exchange_info(symbol)
quote_amount = float(processed_kwargs["quoteOrderQty"]) quote_amount = float(processed_kwargs["quoteOrderQty"])
price = ( quantity = quote_amount / clean_price
float(current_price)
if not current_price is None
else float(processed_kwargs["price"])
)
quantity = quote_amount / price
base_asset_precision = int( base_asset_precision = int(
exchange_info["symbols"][0]["baseAssetPrecision"] exchange_info["symbols"][0]["baseAssetPrecision"]
) )
@@ -403,7 +403,7 @@ class MexcSpotTrade:
exchange_info["symbols"][0]["quoteAmountPrecision"] exchange_info["symbols"][0]["quoteAmountPrecision"]
) )
processed_quantity = self._tool_calculate_quantity( processed_quantity = self._tool_calculate_quantity(
quantity, price, base_asset_precision, quote_amount_precision quantity, clean_price, base_asset_precision, quote_amount_precision
) )
logger.info("根据quoteOrderQty计算quantity: %f", processed_quantity) logger.info("根据quoteOrderQty计算quantity: %f", processed_quantity)
processed_kwargs["quantity"] = str(processed_quantity) processed_kwargs["quantity"] = str(processed_quantity)