From 8d0e9578c407114180f2024bd2f248da1d70fc32 Mon Sep 17 00:00:00 2001 From: discountry Date: Wed, 8 Oct 2025 04:23:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E7=BD=91=E6=A0=BC?= =?UTF-8?q?=E5=BC=95=E6=93=8E=EF=BC=8C=E4=BC=98=E5=8C=96=E4=B9=B0=E5=8D=96?= =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=9B=B8=E5=90=8C=E4=BB=B7=E6=A0=BC=E5=87=BA?= =?UTF-8?q?=E5=8F=A3=E6=84=8F=E5=9B=BE=E6=A3=80=E6=9F=A5=E4=BB=A5=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E6=84=8F=E5=9B=BE=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/strategy/grid-engine.ts | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/strategy/grid-engine.ts b/src/strategy/grid-engine.ts index 6a69216..6a06ab2 100644 --- a/src/strategy/grid-engine.ts +++ b/src/strategy/grid-engine.ts @@ -649,7 +649,13 @@ export class GridEngine { this.log("info", `跳过 BUY @ ${this.formatPrice(levelPrice)}:等待对应平仓成交`); continue; // wait until close filled } - const key = this.getOrderKey("BUY", this.formatPrice(levelPrice), "ENTRY"); + const priceStr = this.formatPrice(levelPrice); + const key = this.getOrderKey("BUY", priceStr, "ENTRY"); + // If an EXIT at the same side+price is planned/active, skip ENTRY to avoid intent conflict + const exitKeySame = this.getOrderKey("BUY", priceStr, "EXIT"); + if ((plannedKeyCounts.get(exitKeySame) ?? 0) >= 1 || desiredKeySet.has(exitKeySame)) { + continue; + } const until = this.pendingKeyUntil.get(key); const nowTs = this.now(); if (until && until > nowTs) { @@ -657,7 +663,7 @@ export class GridEngine { } if ((plannedKeyCounts.get(key) ?? 0) >= 1) continue; if (!desiredKeySet.has(key)) { - desired.push({ level, side: "BUY", price: this.formatPrice(levelPrice), amount: this.config.orderSize, intent: "ENTRY" }); + desired.push({ level, side: "BUY", price: priceStr, amount: this.config.orderSize, intent: "ENTRY" }); desiredKeySet.add(key); plannedKeyCounts.set(key, (plannedKeyCounts.get(key) ?? 0) + 1); } @@ -675,7 +681,13 @@ export class GridEngine { this.log("info", `跳过 SELL @ ${this.formatPrice(levelPrice)}:等待对应平仓成交`); continue; } - const key = this.getOrderKey("SELL", this.formatPrice(levelPrice), "ENTRY"); + const priceStr = this.formatPrice(levelPrice); + const key = this.getOrderKey("SELL", priceStr, "ENTRY"); + // If an EXIT at the same side+price is planned/active, skip ENTRY to avoid intent conflict + const exitKeySame = this.getOrderKey("SELL", priceStr, "EXIT"); + if ((plannedKeyCounts.get(exitKeySame) ?? 0) >= 1 || desiredKeySet.has(exitKeySame)) { + continue; + } const until = this.pendingKeyUntil.get(key); const nowTs = this.now(); if (until && until > nowTs) { @@ -683,7 +695,7 @@ export class GridEngine { } if ((plannedKeyCounts.get(key) ?? 0) >= 1) continue; if (!desiredKeySet.has(key)) { - desired.push({ level, side: "SELL", price: this.formatPrice(levelPrice), amount: this.config.orderSize, intent: "ENTRY" }); + desired.push({ level, side: "SELL", price: priceStr, amount: this.config.orderSize, intent: "ENTRY" }); desiredKeySet.add(key); plannedKeyCounts.set(key, (plannedKeyCounts.get(key) ?? 0) + 1); }