feat: enhance precision synchronization in trading strategies and improve order quantity normalization logic

This commit is contained in:
discountry
2025-10-27 18:42:44 +08:00
parent 295c6a47b7
commit 5bba8169b5
8 changed files with 325 additions and 14 deletions
+52 -4
View File
@@ -77,6 +77,9 @@ export class MakerEngine {
private readonly tradeLog: ReturnType<typeof createTradeLog>;
private readonly events = new StrategyEventEmitter<MakerEvent, MakerEngineSnapshot>();
private readonly sessionVolume = new SessionVolumeTracker();
private priceTick: number = 0.1;
private qtyStep: number = 0.001;
private precisionSync: Promise<void> | null = null;
private timer: ReturnType<typeof setInterval> | null = null;
private processing = false;
@@ -114,6 +117,9 @@ export class MakerEngine {
this.rateLimit = new RateLimitController(this.config.refreshIntervalMs, (type, detail) =>
this.tradeLog.push(type, detail)
);
this.priceTick = Math.max(1e-9, this.config.priceTick);
this.qtyStep = Math.max(1e-9, this.qtyStep);
this.syncPrecision();
this.bootstrap();
}
@@ -290,7 +296,7 @@ export class MakerEngine {
}
// 直接使用orderbook价格,格式化为字符串避免精度问题
const priceDecimals = Math.max(0, Math.floor(Math.log10(1 / this.config.priceTick)));
const priceDecimals = this.getPriceDecimals();
const closeBidPrice = formatPriceToString(topBid, priceDecimals);
const closeAskPrice = formatPriceToString(topAsk, priceDecimals);
const bidPrice = formatPriceToString(topBid - this.config.bidOffset, priceDecimals);
@@ -340,7 +346,7 @@ export class MakerEngine {
if (Math.abs(position.positionAmt) < EPS) return;
const { topBid, topAsk } = getTopPrices(this.depthSnapshot);
if (topBid == null || topAsk == null) return;
const priceDecimals = Math.max(0, Math.floor(Math.log10(1 / this.config.priceTick)));
const priceDecimals = this.getPriceDecimals();
const closeBidPrice = formatPriceToString(topBid, priceDecimals);
const closeAskPrice = formatPriceToString(topAsk, priceDecimals);
await this.checkRisk(position, Number(closeBidPrice), Number(closeAskPrice));
@@ -431,8 +437,8 @@ export class MakerEngine {
maxPct: this.config.maxCloseSlippagePct,
},
{
priceTick: this.config.priceTick,
qtyStep: 0.001, // 默认数量步长
priceTick: this.priceTick,
qtyStep: this.qtyStep,
}
);
} catch (error) {
@@ -527,6 +533,48 @@ export class MakerEngine {
}
}
private syncPrecision(): void {
if (this.precisionSync) return;
const getPrecision = this.exchange.getPrecision?.bind(this.exchange);
if (!getPrecision) return;
this.precisionSync = getPrecision()
.then((precision) => {
if (!precision) return;
let updated = false;
if (Number.isFinite(precision.priceTick) && precision.priceTick > 0) {
if (Math.abs(precision.priceTick - this.priceTick) > 1e-12) {
this.priceTick = precision.priceTick;
this.config.priceTick = precision.priceTick;
updated = true;
}
}
if (Number.isFinite(precision.qtyStep) && precision.qtyStep > 0) {
if (Math.abs(precision.qtyStep - this.qtyStep) > 1e-12) {
this.qtyStep = precision.qtyStep;
updated = true;
}
}
if (updated) {
this.tradeLog.push(
"info",
`已同步交易精度: priceTick=${precision.priceTick} qtyStep=${precision.qtyStep}`
);
}
})
.catch((error) => {
this.tradeLog.push("error", `同步精度失败: ${extractMessage(error)}`);
this.precisionSync = null;
setTimeout(() => this.syncPrecision(), 2000);
});
}
private getPriceDecimals(): number {
const tick = Math.max(1e-9, this.priceTick);
const raw = Math.log10(1 / tick);
if (!Number.isFinite(raw)) return 0;
return Math.max(0, Math.floor(raw + 1e-9));
}
private emitUpdate(): void {
try {
const snapshot = this.buildSnapshot();