mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 09:18:08 +00:00
Update Binance WebSocket configuration and enhance depth handling
- Changed WebSocket base URL to support both spot and futures trading. - Adjusted depth tracking parameters for improved performance, increasing the ratio and reducing speed. - Enhanced payload parsing to accommodate additional data structures from Binance, ensuring robust handling of bids and asks. - Updated comments for clarity on connection behavior and heartbeat monitoring.
This commit is contained in:
@@ -6,10 +6,10 @@ const WebSocketCtor: typeof globalThis.WebSocket =
|
|||||||
? globalThis.WebSocket
|
? globalThis.WebSocket
|
||||||
: ((NodeWebSocket as unknown) as typeof globalThis.WebSocket);
|
: ((NodeWebSocket as unknown) as typeof globalThis.WebSocket);
|
||||||
|
|
||||||
const DEFAULT_BASE_URL = "wss://fstream.binance.com/ws";
|
const DEFAULT_BASE_URL = "wss://stream.binance.com:9443/ws";
|
||||||
|
|
||||||
// ========== Binance WebSocket 连接管理常量 ==========
|
// ========== Binance WebSocket 连接管理常量 ==========
|
||||||
// Binance 服务器每 3 分钟发送 ping,10 分钟无 pong 会断连
|
// Binance 会发送 ping,若长时间无消息则认为连接异常
|
||||||
// 我们设置 5 分钟作为心跳超时阈值(保守值)
|
// 我们设置 5 分钟作为心跳超时阈值(保守值)
|
||||||
const HEARTBEAT_TIMEOUT_MS = 5 * 60 * 1000;
|
const HEARTBEAT_TIMEOUT_MS = 5 * 60 * 1000;
|
||||||
// 心跳检查间隔(每 30 秒检查一次)
|
// 心跳检查间隔(每 30 秒检查一次)
|
||||||
@@ -247,7 +247,7 @@ export class BinanceDepthTracker {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动心跳监控
|
* 启动心跳监控
|
||||||
* 根据 Binance 文档:10 分钟无 pong 会断连
|
* 根据 Binance 文档:长时间无 pong 会断连
|
||||||
* 我们设置 5 分钟作为心跳超时阈值
|
* 我们设置 5 分钟作为心跳超时阈值
|
||||||
*/
|
*/
|
||||||
private startHeartbeatMonitor(): void {
|
private startHeartbeatMonitor(): void {
|
||||||
@@ -338,10 +338,10 @@ export class BinanceDepthTracker {
|
|||||||
private handlePayload(data: unknown): void {
|
private handlePayload(data: unknown): void {
|
||||||
const payload = this.parsePayload(data);
|
const payload = this.parsePayload(data);
|
||||||
if (!payload) return;
|
if (!payload) return;
|
||||||
const bids = Array.isArray(payload.b) ? payload.b : [];
|
const bids = Array.isArray(payload.b) ? payload.b : Array.isArray(payload.bids) ? payload.bids : [];
|
||||||
const asks = Array.isArray(payload.a) ? payload.a : [];
|
const asks = Array.isArray(payload.a) ? payload.a : Array.isArray(payload.asks) ? payload.asks : [];
|
||||||
const depth = {
|
const depth = {
|
||||||
lastUpdateId: Number(payload.u ?? Date.now()),
|
lastUpdateId: Number(payload.lastUpdateId ?? payload.u ?? Date.now()),
|
||||||
bids,
|
bids,
|
||||||
asks,
|
asks,
|
||||||
};
|
};
|
||||||
@@ -366,13 +366,22 @@ export class BinanceDepthTracker {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private parsePayload(data: unknown): { b?: [string, string][]; a?: [string, string][]; u?: number } | null {
|
private parsePayload(
|
||||||
|
data: unknown
|
||||||
|
): { b?: [string, string][]; a?: [string, string][]; bids?: [string, string][]; asks?: [string, string][]; u?: number; lastUpdateId?: number } | null {
|
||||||
try {
|
try {
|
||||||
const text = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf-8") : null;
|
const text = typeof data === "string" ? data : Buffer.isBuffer(data) ? data.toString("utf-8") : null;
|
||||||
if (!text) return null;
|
if (!text) return null;
|
||||||
const parsed = JSON.parse(text);
|
const parsed = JSON.parse(text);
|
||||||
if (!parsed || typeof parsed !== "object") return null;
|
if (!parsed || typeof parsed !== "object") return null;
|
||||||
return parsed as { b?: [string, string][]; a?: [string, string][]; u?: number };
|
return parsed as {
|
||||||
|
b?: [string, string][];
|
||||||
|
a?: [string, string][];
|
||||||
|
bids?: [string, string][];
|
||||||
|
asks?: [string, string][];
|
||||||
|
u?: number;
|
||||||
|
lastUpdateId?: number;
|
||||||
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,10 +190,10 @@ export class MakerPointsEngine {
|
|||||||
this.priceTick = Math.max(1e-9, this.config.priceTick);
|
this.priceTick = Math.max(1e-9, this.config.priceTick);
|
||||||
this.qtyStep = Math.max(1e-9, this.config.qtyStep);
|
this.qtyStep = Math.max(1e-9, this.config.qtyStep);
|
||||||
this.binanceDepth = new BinanceDepthTracker(resolveBinanceSymbol(this.config.symbol), {
|
this.binanceDepth = new BinanceDepthTracker(resolveBinanceSymbol(this.config.symbol), {
|
||||||
baseUrl: process.env.BINANCE_WS_URL,
|
baseUrl: process.env.BINANCE_SPOT_WS_URL ?? process.env.BINANCE_WS_URL,
|
||||||
levels: 20,
|
levels: 20,
|
||||||
ratio: 5,
|
ratio: 9,
|
||||||
speedMs: 500,
|
speedMs: 100,
|
||||||
logger: (context, error) => {
|
logger: (context, error) => {
|
||||||
this.tradeLog.push("warn", `Binance ${context} 异常: ${extractMessage(error)}`);
|
this.tradeLog.push("warn", `Binance ${context} 异常: ${extractMessage(error)}`);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user