mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-10 08:48:07 +00:00
更新 MakerEngine 和 TrendEngine,添加启动时订单管理逻辑;更新 AsterGateway,增加持仓同步功能;更新 README.md,修正项目描述及功能列表
This commit is contained in:
@@ -66,6 +66,8 @@ export class MakerEngine {
|
||||
private sessionQuoteVolume = 0;
|
||||
private prevPositionAmt = 0;
|
||||
private initializedPosition = false;
|
||||
private initialOrderSnapshotReady = false;
|
||||
private initialOrderResetDone = false;
|
||||
|
||||
constructor(private readonly config: MakerConfig, private readonly exchange: ExchangeAdapter) {
|
||||
this.tradeLog = createTradeLog(this.config.maxLogEntries);
|
||||
@@ -128,6 +130,7 @@ export class MakerEngine {
|
||||
this.pendingCancelOrders.delete(id);
|
||||
}
|
||||
}
|
||||
this.initialOrderSnapshotReady = true;
|
||||
this.emitUpdate();
|
||||
});
|
||||
|
||||
@@ -170,6 +173,10 @@ export class MakerEngine {
|
||||
this.emitUpdate();
|
||||
return;
|
||||
}
|
||||
if (!(await this.ensureStartupOrderReset())) {
|
||||
this.emitUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
const depth = this.depthSnapshot!;
|
||||
const bidLevel = depth.bids?.[0];
|
||||
@@ -209,6 +216,31 @@ export class MakerEngine {
|
||||
}
|
||||
}
|
||||
|
||||
private async ensureStartupOrderReset(): Promise<boolean> {
|
||||
if (this.initialOrderResetDone) return true;
|
||||
if (!this.initialOrderSnapshotReady) return false;
|
||||
if (!this.openOrders.length) {
|
||||
this.initialOrderResetDone = true;
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
await this.exchange.cancelAllOrders({ symbol: this.config.symbol });
|
||||
this.pendingCancelOrders.clear();
|
||||
unlockOperating(this.locks, this.timers, this.pending, "LIMIT");
|
||||
this.tradeLog.push("order", "启动时清理历史挂单");
|
||||
this.initialOrderResetDone = true;
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (isUnknownOrderError(error)) {
|
||||
this.tradeLog.push("order", "历史挂单已消失,跳过启动清理");
|
||||
this.initialOrderResetDone = true;
|
||||
return true;
|
||||
}
|
||||
this.tradeLog.push("error", `启动撤单失败: ${String(error)}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async syncOrders(targets: DesiredOrder[]): Promise<void> {
|
||||
const tolerance = this.config.priceChaseThreshold;
|
||||
const unmatched = new Set(targets.map((_, idx) => idx));
|
||||
|
||||
@@ -81,6 +81,9 @@ export class TrendEngine {
|
||||
private cancelAllRequested = false;
|
||||
private readonly pendingCancelOrders = new Set<number>();
|
||||
|
||||
private ordersSnapshotReady = false;
|
||||
private startupLogged = false;
|
||||
|
||||
private readonly listeners = new Map<TrendEngineEvent, Set<TrendEngineListener>>();
|
||||
|
||||
constructor(private readonly config: TradingConfig, private readonly exchange: ExchangeAdapter) {
|
||||
@@ -142,6 +145,7 @@ export class TrendEngine {
|
||||
if (this.openOrders.length === 0 || this.pendingCancelOrders.size === 0) {
|
||||
this.cancelAllRequested = false;
|
||||
}
|
||||
this.ordersSnapshotReady = true;
|
||||
this.emitUpdate();
|
||||
});
|
||||
this.exchange.watchDepth(this.config.symbol, (depth) => {
|
||||
@@ -182,10 +186,15 @@ export class TrendEngine {
|
||||
if (this.processing) return;
|
||||
this.processing = true;
|
||||
try {
|
||||
if (!this.ordersSnapshotReady) {
|
||||
this.emitUpdate();
|
||||
return;
|
||||
}
|
||||
if (!this.isReady()) {
|
||||
this.emitUpdate();
|
||||
return;
|
||||
}
|
||||
this.logStartupState();
|
||||
const sma30 = getSMA(this.klineSnapshot, 30);
|
||||
if (sma30 == null) {
|
||||
return;
|
||||
@@ -216,6 +225,22 @@ export class TrendEngine {
|
||||
}
|
||||
}
|
||||
|
||||
private logStartupState(): void {
|
||||
if (this.startupLogged) return;
|
||||
const position = getPosition(this.accountSnapshot, this.config.symbol);
|
||||
const hasPosition = Math.abs(position.positionAmt) > 1e-5;
|
||||
if (hasPosition) {
|
||||
this.tradeLog.push(
|
||||
"info",
|
||||
`检测到已有持仓: ${position.positionAmt > 0 ? "多" : "空"} ${Math.abs(position.positionAmt).toFixed(4)} @ ${position.entryPrice.toFixed(2)}`
|
||||
);
|
||||
}
|
||||
if (this.openOrders.length > 0) {
|
||||
this.tradeLog.push("info", `检测到已有挂单 ${this.openOrders.length} 笔,将按策略规则接管`);
|
||||
}
|
||||
this.startupLogged = true;
|
||||
}
|
||||
|
||||
private async handleOpenPosition(currentPrice: number, currentSma: number): Promise<void> {
|
||||
if (this.lastPrice == null) {
|
||||
this.lastPrice = currentPrice;
|
||||
|
||||
Reference in New Issue
Block a user