Files
trade/data/run/exchange.ts
T
Rekey ebaef5042e 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 合约交易对
2026-06-16 19:02:16 +08:00

53 lines
1.9 KiB
TypeScript

import { logger } from "../utils/logger";
import { getAllPairs, updatePairLastBackfillTime } from '../service/pair';
import { upsertOrUpdateKlines } from "../service/kline";
import { fetchKlines } from '../exchanges';
function getNowMinuteMS() {
const minuteMS = 1000 * 60;
return Math.floor(Date.now() / minuteMS) * minuteMS
}
const allPairs = await getAllPairs();
for (const pair of allPairs) {
let lastBackfillTime = pair.last_backfill_time.getTime();
try {
while (lastBackfillTime < getNowMinuteMS()) {
console.log('lastBackfillTime', lastBackfillTime);
const klines = await fetchKlines({
exchange: 'binance',
type: pair.type,
symbol: pair.symbol,
startTime: lastBackfillTime,
limit: 500,
});
console.log(`拉取到 ${klines.length} 条 K 线`);
if (klines.length > 0) {
await upsertOrUpdateKlines(klines);
const lastK = klines[klines.length - 1];
if (lastK) {
await updatePairLastBackfillTime(lastK?.symbol, new Date(lastK.openTime), pair.type);
if (lastBackfillTime === lastK.openTime) {
break;
}
lastBackfillTime = lastK.openTime;
}
}
await new Promise((resolve) => {
setTimeout(resolve, Math.random() * 1000);
});
}
} catch (err) {
console.error("拉取失败:", err);
}
}
// 所有币种回补完成以后等待1秒关闭pgsql连接退出此进程
await new Promise((resolve) => setTimeout(resolve, 1000));
const { AppDataSource } = await import("../db/data-source");
if (AppDataSource.isInitialized) {
await AppDataSource.destroy();
console.log("pgsql 连接已关闭");
}
process.exit(0);