From 402999203af95077c6e5422974ed5a54dc114d7a Mon Sep 17 00:00:00 2001 From: discountry Date: Wed, 24 Sep 2025 01:47:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20MakerEngine=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8C=81=E4=BB=93=E5=9D=87=E4=BB=B7=E6=9C=AA?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E7=9A=84=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E6=AD=A2=E6=8D=9F?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91=E4=BB=A5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=9C=AA=E5=AE=9A=E4=B9=89=E7=9A=84=E6=8C=81=E4=BB=93=E5=9D=87?= =?UTF-8?q?=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/maker-engine.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/core/maker-engine.ts b/src/core/maker-engine.ts index 470b882..f05e3ab 100644 --- a/src/core/maker-engine.ts +++ b/src/core/maker-engine.ts @@ -68,6 +68,7 @@ export class MakerEngine { private initializedPosition = false; private initialOrderSnapshotReady = false; private initialOrderResetDone = false; + private entryPricePendingLogged = false; constructor(private readonly config: MakerConfig, private readonly exchange: ExchangeAdapter) { this.tradeLog = createTradeLog(this.config.maxLogEntries); @@ -195,6 +196,7 @@ export class MakerEngine { const desired: DesiredOrder[] = []; if (absPosition < EPS) { + this.entryPricePendingLogged = false; desired.push({ side: "BUY", price: bidPrice, amount: this.config.tradeAmount, reduceOnly: false }); desired.push({ side: "SELL", price: askPrice, amount: this.config.tradeAmount, reduceOnly: false }); } else { @@ -317,11 +319,30 @@ export class MakerEngine { const absPosition = Math.abs(position.positionAmt); if (absPosition < EPS) return; + const hasEntryPrice = Number.isFinite(position.entryPrice) && Math.abs(position.entryPrice) > 1e-8; + if (!hasEntryPrice) { + if (!this.entryPricePendingLogged) { + this.tradeLog.push("info", "做市持仓均价未同步,等待账户快照刷新后再执行止损判断"); + this.entryPricePendingLogged = true; + } + return; + } + this.entryPricePendingLogged = false; + const pnl = position.positionAmt > 0 ? (bidPrice - position.entryPrice) * absPosition : (position.entryPrice - askPrice) * absPosition; + const unrealized = Number.isFinite(position.unrealizedProfit) + ? position.unrealizedProfit + : null; + const derivedLoss = pnl < -this.config.lossLimit; + const snapshotLoss = Boolean( + unrealized != null && + unrealized < -this.config.lossLimit && + Math.sign(unrealized) === Math.sign(pnl) + ); - if (pnl < -this.config.lossLimit || position.unrealizedProfit < -this.config.lossLimit) { + if (derivedLoss || snapshotLoss) { this.tradeLog.push( "stop", `触发止损,方向=${position.positionAmt > 0 ? "多" : "空"} 当前亏损=${pnl.toFixed(4)} USDT`