refactor(exchanges): fetchKlines 改为 params 对象签名,新增 type/exchange 参数

- 新增 FetchKlinesParams 接口统一 fetchKlines 参数(exchange/type/symbol/startTime/limit/endTime)
- BaseRestClient 抽象方法 + MarketDataFeed 接口改为 params 对象签名
- BinanceRestClient / BinanceFuturesRestClient 由调用方传入 type,不再硬编码 spot/um
- index.ts 新增静态 fetchKlines(params) 便捷函数,按 exchange+type 自动路由
- exchange.ts / bnkline.ts 调用方适配新签名
- 初始化 SQL 补充 BNBUSDT/SOLUSDT 合约交易对
This commit is contained in:
Rekey
2026-06-16 19:02:16 +08:00
parent 705a2f6ea0
commit ebaef5042e
7 changed files with 87 additions and 56 deletions
+9 -20
View File
@@ -3,7 +3,7 @@ import { MainClient, USDMClient, type Kline as BinanceRestKline } from "binance"
import { logger } from "../../utils/logger";
import { exchange } from "../../config";
import { BaseRestClient } from "../base";
import type { Kline, MarketInfo, KlineInterval, PairType } from "../../types";
import type { Kline, MarketInfo, KlineInterval, PairType, FetchKlinesParams } from "../../types";
// ============================================================
// Binance REST K 线 → 本系统标准化 Kline 转换
@@ -95,17 +95,10 @@ export class BinanceRestClient extends BaseRestClient {
* Binance 硬限制单次最多 1000 条,超限自动裁切。
* 高周期 K 线通过 TimescaleDB 连续聚合视图生成。
*
* @param symbol - 交易对(如 BTCUSDT
* @param startTime - 起始时间(Unix ms
* @param limit - 单次拉取条数,默认取自 config.defaultLimit
* @param endTime - 结束时间(Unix ms),可选
* @param params - fetchKlines 统一参数对象
*/
async fetchKlines(
symbol: string,
startTime: number,
limit?: number,
endTime?: number,
): Promise<Kline[]> {
async fetchKlines(params: FetchKlinesParams): Promise<Kline[]> {
const { symbol, startTime, limit, endTime, type } = params;
const effectiveLimit = limit ?? this.config.defaultLimit;
// Binance 硬限制:单次最多 1000 条
const safeLimit = Math.min(effectiveLimit, 1000);
@@ -132,7 +125,7 @@ export class BinanceRestClient extends BaseRestClient {
// 按 openTime 升序排序(防御性),转换为标准化 Kline
return rawKlines
.map((k) => convertBinanceKline(k, symbol, "1m", "spot"))
.map((k) => convertBinanceKline(k, symbol, "1m", type))
.sort((a, b) => a.openTime - b.openTime);
}
@@ -166,14 +159,10 @@ export class BinanceFuturesRestClient extends BaseRestClient {
* 拉取 USDT-M 永续合约 1m K 线。
*
* USDMClient.getKlines() 返回与 MainClient 同构的 12 元组,
* convertBinanceKline 直接复用,type 固定为 'um'
* convertBinanceKline 直接复用,type 由调用方通过 params 传入
*/
async fetchKlines(
symbol: string,
startTime: number,
limit?: number,
endTime?: number,
): Promise<Kline[]> {
async fetchKlines(params: FetchKlinesParams): Promise<Kline[]> {
const { symbol, startTime, limit, endTime, type } = params;
const effectiveLimit = limit ?? this.config.defaultLimit;
const safeLimit = Math.min(effectiveLimit, 1000);
@@ -197,7 +186,7 @@ export class BinanceFuturesRestClient extends BaseRestClient {
}
return rawKlines
.map((k) => convertBinanceKline(k, symbol, "1m", "um"))
.map((k) => convertBinanceKline(k, symbol, "1m", type))
.sort((a, b) => a.openTime - b.openTime);
}