From 571ceff0f08c7901c2d652caa5722aff4f606a8c Mon Sep 17 00:00:00 2001 From: mxyhi Date: Thu, 30 Oct 2025 13:06:14 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(trader):=20aster=E5=B9=B3?= =?UTF-8?q?=E4=BB=93=E5=90=8E=E8=87=AA=E5=8A=A8=E5=8F=96=E6=B6=88=E6=8C=82?= =?UTF-8?q?=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整 CloseLong/CloseShort 逻辑, 在平仓后调用 CancelAllOrders 清理挂单 --- trader/aster_trader.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/trader/aster_trader.go b/trader/aster_trader.go index 9aaf078c..e3a96aed 100644 --- a/trader/aster_trader.go +++ b/trader/aster_trader.go @@ -195,11 +195,11 @@ func (t *AsterTrader) formatQuantity(symbol string, quantity float64) (float64, func (t *AsterTrader) formatFloatWithPrecision(value float64, precision int) string { // 使用指定精度格式化 formatted := strconv.FormatFloat(value, 'f', precision, 64) - + // 去除末尾的0和小数点(如果有) formatted = strings.TrimRight(formatted, "0") formatted = strings.TrimRight(formatted, ".") - + return formatted } @@ -556,7 +556,7 @@ func (t *AsterTrader) OpenLong(symbol string, quantity float64, leverage int) (m priceStr := t.formatFloatWithPrecision(formattedPrice, prec.PricePrecision) qtyStr := t.formatFloatWithPrecision(formattedQty, prec.QuantityPrecision) - log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", + log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", limitPrice, priceStr, prec.PricePrecision, quantity, qtyStr, prec.QuantityPrecision) params := map[string]interface{}{ @@ -618,7 +618,7 @@ func (t *AsterTrader) OpenShort(symbol string, quantity float64, leverage int) ( priceStr := t.formatFloatWithPrecision(formattedPrice, prec.PricePrecision) qtyStr := t.formatFloatWithPrecision(formattedQty, prec.QuantityPrecision) - log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", + log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", limitPrice, priceStr, prec.PricePrecision, quantity, qtyStr, prec.QuantityPrecision) params := map[string]interface{}{ @@ -693,7 +693,7 @@ func (t *AsterTrader) CloseLong(symbol string, quantity float64) (map[string]int priceStr := t.formatFloatWithPrecision(formattedPrice, prec.PricePrecision) qtyStr := t.formatFloatWithPrecision(formattedQty, prec.QuantityPrecision) - log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", + log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", limitPrice, priceStr, prec.PricePrecision, quantity, qtyStr, prec.QuantityPrecision) params := map[string]interface{}{ @@ -717,6 +717,12 @@ func (t *AsterTrader) CloseLong(symbol string, quantity float64) (map[string]int } log.Printf("✓ 平多仓成功: %s 数量: %s", symbol, qtyStr) + + // 平仓后取消该币种的所有挂单(止损止盈单) + if err := t.CancelAllOrders(symbol); err != nil { + log.Printf(" ⚠ 取消挂单失败: %v", err) + } + return result, nil } @@ -770,7 +776,7 @@ func (t *AsterTrader) CloseShort(symbol string, quantity float64) (map[string]in priceStr := t.formatFloatWithPrecision(formattedPrice, prec.PricePrecision) qtyStr := t.formatFloatWithPrecision(formattedQty, prec.QuantityPrecision) - log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", + log.Printf(" 📏 精度处理: 价格 %.8f -> %s (精度=%d), 数量 %.8f -> %s (精度=%d)", limitPrice, priceStr, prec.PricePrecision, quantity, qtyStr, prec.QuantityPrecision) params := map[string]interface{}{ @@ -794,6 +800,12 @@ func (t *AsterTrader) CloseShort(symbol string, quantity float64) (map[string]in } log.Printf("✓ 平空仓成功: %s 数量: %s", symbol, qtyStr) + + // 平仓后取消该币种的所有挂单(止损止盈单) + if err := t.CancelAllOrders(symbol); err != nil { + log.Printf(" ⚠ 取消挂单失败: %v", err) + } + return result, nil }