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
+3 -11
View File
@@ -13,7 +13,7 @@
// ============================================================
import { logger } from "../utils/logger";
import type { Kline, MarketInfo, RestClientConfig } from "../types";
import type { Kline, MarketInfo, FetchKlinesParams, RestClientConfig } from "../types";
import { DEFAULT_REST_CONFIG } from "../types/base";
// ============================================================
@@ -83,17 +83,9 @@ export abstract class BaseRestClient {
* 2. 将原始数据转换为本系统标准化 Kline 结构
* 3. 处理分页逻辑(若时间跨度超过单次请求上限)
*
* @param symbol - 交易对符号(如 BTCUSDT)
* @param startTime - 起始时间(Unix ms
* @param limit - 单次最大条数(默认取自 config.defaultLimit
* @param endTime - 结束时间(Unix ms),可选
* @param params - fetchKlines 统一参数对象
*/
abstract fetchKlines(
symbol: string,
startTime: number,
limit?: number,
endTime?: number,
): Promise<Kline[]>;
abstract fetchKlines(params: FetchKlinesParams): Promise<Kline[]>;
/**
* 获取交易所交易对信息(REST)。
+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);
}
+32
View File
@@ -1,5 +1,6 @@
import { BaseRestClient } from "./base";
import { BinanceRestClient, BinanceFuturesRestClient } from "./binance/rest";
import type { FetchKlinesParams, Kline } from "../types";
/** 交易所 ID 到 RestClient 构造器的注册表 */
const registry: Record<string, () => BaseRestClient> = {
@@ -28,6 +29,37 @@ export function createRestClient(exchangeId: string): BaseRestClient {
return factory();
}
/**
* 拉取历史 K 线(静态便捷方法)。
*
* 根据 exchange + type 自动路由到正确的交易所客户端,
* 调用方无需手动创建 client 或选择 spot/futures 子类。
*
* 路由规则:
* - type = "spot" → exchange(如 "binance"
* - type = "um"/"cm" → `${exchange}_futures`(如 "binance_futures"
*
* @param params - fetchKlines 统一参数对象
* @returns 标准化 K 线数组,按时间升序
*
* @example
* const klines = await fetchKlines({
* exchange: 'binance',
* type: 'um',
* symbol: 'BTCUSDT',
* startTime: 1700000000000,
* limit: 500,
* });
*/
export async function fetchKlines(params: FetchKlinesParams): Promise<Kline[]> {
const exchangeId =
params.type === "spot"
? params.exchange
: `${params.exchange}_futures`;
const client = createRestClient(exchangeId);
return client.fetchKlines(params);
}
export { BaseRestClient } from "./base";
export { BinanceRestClient, BinanceFuturesRestClient } from "./binance/rest";
export { KLINE_INTERVAL_MS } from "./constants";