mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 09:18:08 +00:00
feat: 更新网格引擎,增强入口数量限制逻辑,支持按买卖方向分别计算待处理订单数量
This commit is contained in:
@@ -680,12 +680,12 @@ export class GridEngine {
|
|||||||
if (capped <= EPSILON) continue;
|
if (capped <= EPSILON) continue;
|
||||||
d.amount = capped;
|
d.amount = capped;
|
||||||
} else {
|
} else {
|
||||||
const capped = this.capEntryQty(d.amount);
|
const capped = this.capEntryQty(d.amount, d.side);
|
||||||
if (capped <= EPSILON) continue;
|
if (capped <= EPSILON) continue;
|
||||||
d.amount = capped;
|
d.amount = capped;
|
||||||
}
|
}
|
||||||
const key = this.getOrderKey(d.side, d.price, intent);
|
const key = this.getOrderKey(d.side, d.price, intent);
|
||||||
if ((plannedKeyCounts.get(key) ?? 0) >= 1) continue;
|
if ((activeKeyCounts.get(key) ?? 0) >= 1) continue;
|
||||||
try {
|
try {
|
||||||
const placed = await placeOrder(
|
const placed = await placeOrder(
|
||||||
this.exchange,
|
this.exchange,
|
||||||
@@ -704,6 +704,7 @@ export class GridEngine {
|
|||||||
);
|
);
|
||||||
if (placed) {
|
if (placed) {
|
||||||
plannedKeyCounts.set(key, (plannedKeyCounts.get(key) ?? 0) + 1);
|
plannedKeyCounts.set(key, (plannedKeyCounts.get(key) ?? 0) + 1);
|
||||||
|
activeKeyCounts.set(key, (activeKeyCounts.get(key) ?? 0) + 1);
|
||||||
if (placed.orderId != null) {
|
if (placed.orderId != null) {
|
||||||
const record: { side: "BUY" | "SELL"; price: string; level: number; intent: "ENTRY" | "EXIT"; sourceLevel?: number } = {
|
const record: { side: "BUY" | "SELL"; price: string; level: number; intent: "ENTRY" | "EXIT"; sourceLevel?: number } = {
|
||||||
side: d.side,
|
side: d.side,
|
||||||
@@ -751,10 +752,11 @@ export class GridEngine {
|
|||||||
return Math.min(desiredQty, remain);
|
return Math.min(desiredQty, remain);
|
||||||
}
|
}
|
||||||
|
|
||||||
private estimatePendingEntryQty(): number {
|
private estimatePendingEntryQty(side: "BUY" | "SELL"): number {
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
for (const o of this.openOrders) {
|
for (const o of this.openOrders) {
|
||||||
if (!this.isActiveLimitOrder(o)) continue;
|
if (!this.isActiveLimitOrder(o)) continue;
|
||||||
|
if (o.side !== side) continue;
|
||||||
const meta = this.orderIntentById.get(String(o.orderId));
|
const meta = this.orderIntentById.get(String(o.orderId));
|
||||||
if (!meta || meta.intent !== "ENTRY") continue;
|
if (!meta || meta.intent !== "ENTRY") continue;
|
||||||
const orig = Number(o.origQty || 0);
|
const orig = Number(o.origQty || 0);
|
||||||
@@ -764,10 +766,11 @@ export class GridEngine {
|
|||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
private capEntryQty(desiredQty: number): number {
|
private capEntryQty(desiredQty: number, side: "BUY" | "SELL"): number {
|
||||||
|
// Cap per side to allow simultaneous BUY/SELL grids without mutual blocking
|
||||||
const absPos = Math.abs(this.position.positionAmt);
|
const absPos = Math.abs(this.position.positionAmt);
|
||||||
const pendingEntry = this.estimatePendingEntryQty();
|
const pendingEntrySameSide = this.estimatePendingEntryQty(side);
|
||||||
const remain = Math.max(this.config.maxPositionSize - absPos - pendingEntry, 0);
|
const remain = Math.max(this.config.maxPositionSize - absPos - pendingEntrySameSide, 0);
|
||||||
return Math.min(desiredQty, remain);
|
return Math.min(desiredQty, remain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user