更新 MakerEngine 和 TrendEngine,添加待撤销订单管理逻辑;更新 MakerApp,调整累计成交量显示位置

This commit is contained in:
discountry
2025-09-23 21:18:03 +08:00
parent 1e0242a036
commit 2330dbe9ec
4 changed files with 40 additions and 6 deletions
+17
View File
@@ -54,6 +54,7 @@ export class MakerEngine {
private readonly locks: OrderLockMap = {};
private readonly timers: OrderTimerMap = {};
private readonly pending: OrderPendingMap = {};
private readonly pendingCancelOrders = new Set<number>();
private readonly tradeLog: ReturnType<typeof createTradeLog>;
private readonly listeners = new Map<MakerEvent, Set<MakerListener>>();
@@ -121,6 +122,12 @@ export class MakerEngine {
this.openOrders = Array.isArray(orders)
? orders.filter((order) => order.type !== "MARKET" && order.symbol === this.config.symbol)
: [];
const currentIds = new Set(this.openOrders.map((order) => order.orderId));
for (const id of Array.from(this.pendingCancelOrders)) {
if (!currentIds.has(id)) {
this.pendingCancelOrders.delete(id);
}
}
this.emitUpdate();
});
@@ -214,6 +221,9 @@ export class MakerEngine {
continue;
}
const reduceOnly = order.reduceOnly === true;
if (this.pendingCancelOrders.has(order.orderId)) {
continue;
}
const matchedIndex = targets.findIndex((target, index) => {
if (!unmatched.has(index)) return false;
if (target.side !== order.side) return false;
@@ -228,14 +238,17 @@ export class MakerEngine {
}
for (const order of toCancel) {
this.pendingCancelOrders.add(order.orderId);
try {
await this.exchange.cancelOrder({ symbol: this.config.symbol, orderId: order.orderId });
this.tradeLog.push("order", `撤销不匹配订单 ${order.side} @ ${order.price} reduceOnly=${order.reduceOnly}`);
} catch (error) {
if (isUnknownOrderError(error)) {
this.tradeLog.push("order", "撤销时发现订单已被成交/取消,忽略");
this.pendingCancelOrders.delete(order.orderId);
} else {
this.tradeLog.push("error", `撤销订单失败: ${String(error)}`);
this.pendingCancelOrders.delete(order.orderId);
}
}
}
@@ -303,13 +316,17 @@ export class MakerEngine {
private async flushOrders(): Promise<void> {
if (!this.openOrders.length) return;
for (const order of this.openOrders) {
if (this.pendingCancelOrders.has(order.orderId)) continue;
this.pendingCancelOrders.add(order.orderId);
try {
await this.exchange.cancelOrder({ symbol: this.config.symbol, orderId: order.orderId });
} catch (error) {
if (isUnknownOrderError(error)) {
this.tradeLog.push("order", "订单已不存在,撤销跳过");
this.pendingCancelOrders.delete(order.orderId);
} else {
this.tradeLog.push("error", `撤销订单失败: ${String(error)}`);
this.pendingCancelOrders.delete(order.orderId);
}
}
}
+16 -1
View File
@@ -78,6 +78,8 @@ export class TrendEngine {
private sessionQuoteVolume = 0;
private prevPositionAmt = 0;
private initializedPosition = false;
private cancelAllRequested = false;
private readonly pendingCancelOrders = new Set<number>();
private readonly listeners = new Map<TrendEngineEvent, Set<TrendEngineListener>>();
@@ -131,6 +133,15 @@ export class TrendEngine {
this.openOrders = Array.isArray(orders)
? orders.filter((order) => order.type !== "MARKET" && order.symbol === this.config.symbol)
: [];
const currentIds = new Set(this.openOrders.map((order) => order.orderId));
for (const id of Array.from(this.pendingCancelOrders)) {
if (!currentIds.has(id)) {
this.pendingCancelOrders.delete(id);
}
}
if (this.openOrders.length === 0 || this.pendingCancelOrders.size === 0) {
this.cancelAllRequested = false;
}
this.emitUpdate();
});
this.exchange.watchDepth(this.config.symbol, (depth) => {
@@ -210,14 +221,17 @@ export class TrendEngine {
this.lastPrice = currentPrice;
return;
}
if (this.openOrders.length > 0) {
if (this.openOrders.length > 0 && !this.cancelAllRequested) {
try {
await this.exchange.cancelAllOrders({ symbol: this.config.symbol });
this.cancelAllRequested = true;
} catch (err) {
if (isUnknownOrderError(err)) {
this.tradeLog.push("order", "撤单时部分订单已不存在,忽略");
this.cancelAllRequested = true;
} else {
this.tradeLog.push("error", `撤销挂单失败: ${String(err)}`);
this.cancelAllRequested = false;
}
}
}
@@ -315,6 +329,7 @@ export class TrendEngine {
const orderIdList = this.openOrders.map((order) => order.orderId);
try {
await this.exchange.cancelOrders({ symbol: this.config.symbol, orderIdList });
orderIdList.forEach((id) => this.pendingCancelOrders.add(id));
} catch (err) {
if (isUnknownOrderError(err)) {
this.tradeLog.push("order", "止损前撤单发现订单已不存在");