continuation
This commit is contained in:
parent
32e43c9243
commit
879e9b4228
42 changed files with 2607 additions and 1306 deletions
|
|
@ -50,7 +50,9 @@ export class StorageService {
|
|||
async initialize(config: ModeConfig): Promise<void> {
|
||||
this.mode = config.mode;
|
||||
// Clients are already injected via DI
|
||||
await this.createTables();
|
||||
if (config.mode !== 'backtest') {
|
||||
await this.createTables();
|
||||
}
|
||||
}
|
||||
|
||||
private async createTables(): Promise<void> {
|
||||
|
|
@ -337,7 +339,67 @@ export class StorageService {
|
|||
);
|
||||
}
|
||||
|
||||
/** One historical-data policy for native and TypeScript execution. */
|
||||
async getHistoricalBars(
|
||||
symbol: string,
|
||||
startTime: Date,
|
||||
endTime: Date,
|
||||
interval = '1m'
|
||||
): Promise<HistoricalBar[]> {
|
||||
if (
|
||||
!Number.isFinite(startTime.getTime()) ||
|
||||
!Number.isFinite(endTime.getTime()) ||
|
||||
endTime <= startTime
|
||||
) {
|
||||
throw new Error('Invalid historical date range');
|
||||
}
|
||||
if (!['1m', '5m', '15m', '1h', '1d'].includes(interval)) {
|
||||
throw new Error('Unsupported historical interval');
|
||||
}
|
||||
const rows = await this.readHistoricalBars(symbol, startTime, endTime, interval);
|
||||
const bars = rows
|
||||
.map(row => {
|
||||
const timestamp = new Date(row.timestamp);
|
||||
const numeric = (value: unknown) =>
|
||||
typeof value === 'number'
|
||||
? value
|
||||
: typeof value === 'string' && value.trim()
|
||||
? Number(value)
|
||||
: NaN;
|
||||
const open = numeric(row.open),
|
||||
high = numeric(row.high),
|
||||
low = numeric(row.low),
|
||||
close = numeric(row.close),
|
||||
volume = numeric(row.volume);
|
||||
if (
|
||||
!Number.isFinite(timestamp.getTime()) ||
|
||||
![open, high, low, close, volume].every(Number.isFinite) ||
|
||||
low <= 0 ||
|
||||
high < Math.max(open, close) ||
|
||||
low > Math.min(open, close) ||
|
||||
volume < 0
|
||||
) {
|
||||
throw new Error(`Invalid historical OHLCV data for ${symbol}`);
|
||||
}
|
||||
return { ...row, symbol, timestamp, open, high, low, close, volume };
|
||||
})
|
||||
.filter(bar => bar.timestamp >= startTime && bar.timestamp < endTime)
|
||||
.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
||||
if (!bars.length) {
|
||||
throw new Error(`No historical data for ${symbol}`);
|
||||
}
|
||||
if (
|
||||
bars.some(
|
||||
(bar, index) =>
|
||||
index > 0 && bars[index - 1]?.timestamp.getTime() === bar.timestamp.getTime()
|
||||
)
|
||||
) {
|
||||
throw new Error(`Duplicate historical timestamps for ${symbol}`);
|
||||
}
|
||||
return bars;
|
||||
}
|
||||
|
||||
private async readHistoricalBars(
|
||||
symbol: string,
|
||||
startTime: Date,
|
||||
endTime: Date,
|
||||
|
|
@ -484,14 +546,6 @@ export class StorageService {
|
|||
}
|
||||
|
||||
async shutdown(): Promise<void> {
|
||||
if (this.questdb) {
|
||||
await this.questdb.disconnect();
|
||||
this.questdb = null;
|
||||
}
|
||||
|
||||
if (this.postgres) {
|
||||
await this.postgres.disconnect();
|
||||
this.postgres = null;
|
||||
}
|
||||
// All adapters are borrowed from DI. Only their lifecycle owner may close them.
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue