feat: Crypto.com-Client und Backfill-Script
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
34
src/server/scripts/backfill.ts
Normal file
34
src/server/scripts/backfill.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { PAIRS } from '../types';
|
||||
import { fetchCandles } from '../market/cryptocom';
|
||||
import { insertCandles, getCoverage } from '../market/candle-store';
|
||||
import { sql } from '../db/client';
|
||||
|
||||
const M15 = 15 * 60 * 1000;
|
||||
const TARGET_MONTHS = 14;
|
||||
const since = Date.now() - TARGET_MONTHS * 30 * 24 * 60 * 60 * 1000;
|
||||
|
||||
for (const pair of PAIRS) {
|
||||
let endTs: number | undefined = undefined;
|
||||
let total = 0;
|
||||
for (;;) {
|
||||
const batch = await fetchCandles(pair, '15m', 300, endTs);
|
||||
if (batch.length === 0) break;
|
||||
|
||||
// Sanity: 15-min-Raster
|
||||
for (const c of batch) {
|
||||
if (c.ts % M15 !== 0) throw new Error(`${pair}: Timestamp ${c.ts} nicht im 15m-Raster — Konvention prüfen!`);
|
||||
}
|
||||
// Noch laufende Candle verwerfen
|
||||
const closed = batch.filter((c) => c.ts + M15 <= Date.now());
|
||||
await insertCandles(pair, closed);
|
||||
total += closed.length;
|
||||
|
||||
const oldest = Math.min(...batch.map((c) => c.ts));
|
||||
if (oldest <= since) break;
|
||||
endTs = oldest - 1;
|
||||
await Bun.sleep(200); // Rate-Limit-Schonung
|
||||
}
|
||||
const cov = await getCoverage(pair);
|
||||
console.log(`${pair}: +${total} Candles, Coverage ${cov.from?.toISOString()} → ${cov.to?.toISOString()} (${cov.count})`);
|
||||
}
|
||||
await sql.end();
|
||||
Reference in New Issue
Block a user