From 9bf3e97d877b7e2fe8f2147eadc9da1960b5d5d8 Mon Sep 17 00:00:00 2001 From: discountry Date: Tue, 23 Sep 2025 16:25:15 +0800 Subject: [PATCH] fix position size --- src/core/trend-engine.ts | 14 ++++++++++---- src/ui/TrendApp.tsx | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/core/trend-engine.ts b/src/core/trend-engine.ts index 5cb5289..5086df2 100644 --- a/src/core/trend-engine.ts +++ b/src/core/trend-engine.ts @@ -214,7 +214,11 @@ export class TrendEngine { try { await this.exchange.cancelAllOrders({ symbol: this.config.symbol }); } catch (err) { - this.tradeLog.push("error", `撤销挂单失败: ${String(err)}`); + if (isUnknownOrderError(err)) { + this.tradeLog.push("order", "撤单时部分订单已不存在,忽略"); + } else { + this.tradeLog.push("error", `撤销挂单失败: ${String(err)}`); + } } } if (this.lastPrice > currentSma && currentPrice < currentSma) { @@ -327,10 +331,10 @@ export class TrendEngine { this.timers, this.pending, direction === "long" ? "SELL" : "BUY", - this.config.tradeAmount, + Math.abs(position.positionAmt), (type, detail) => this.tradeLog.push(type, detail) ); - this.tradeLog.push("close", `止损平仓: ${direction === "long" ? "SELL" : "BUY"}`); + this.tradeLog.push("close", `止损平仓: ${direction === "long" ? "SELL" : "BUY"}`); } catch (err) { if (isUnknownOrderError(err)) { this.tradeLog.push("order", "止损平仓时目标订单已不存在"); @@ -350,6 +354,8 @@ export class TrendEngine { lastPrice: number ): Promise { try { + const position = getPosition(this.accountSnapshot, this.config.symbol); + const quantity = Math.abs(position.positionAmt) || this.config.tradeAmount; await placeStopLossOrder( this.exchange, this.config.symbol, @@ -359,7 +365,7 @@ export class TrendEngine { this.pending, side, stopPrice, - this.config.tradeAmount, + quantity, lastPrice, (type, detail) => this.tradeLog.push(type, detail) ); diff --git a/src/ui/TrendApp.tsx b/src/ui/TrendApp.tsx index 4a4faa4..6eef9a2 100644 --- a/src/ui/TrendApp.tsx +++ b/src/ui/TrendApp.tsx @@ -77,7 +77,7 @@ export function TrendApp({ onExit }: TrendAppProps) { ); } - const { position, tradeLog, openOrders, trend, ready, lastPrice, sma30 } = snapshot; + const { position, tradeLog, openOrders, trend, ready, lastPrice, sma30, sessionVolume } = snapshot; const hasPosition = Math.abs(position.positionAmt) > 1e-5; const lastLogs = tradeLog.slice(-5); const sortedOrders = [...openOrders].sort((a, b) => (Number(b.updateTime ?? 0) - Number(a.updateTime ?? 0)) || Number(b.orderId) - Number(a.orderId)); @@ -132,7 +132,7 @@ export function TrendApp({ onExit }: TrendAppProps) { 累计交易次数: {snapshot.totalTrades} | 累计收益: {formatNumber(snapshot.totalProfit, 4)} USDT - 累计成交量: {formatNumber(snapshot.sessionVolume, 2)} USDT + 累计成交量: {formatNumber(sessionVolume, 2)} USDT {snapshot.lastOpenSignal.side ? (