86 lines
3.2 KiB
TypeScript
86 lines
3.2 KiB
TypeScript
import {
|
|
validateHistoricalDataRequest,
|
|
type HistoricalDatasetCatalogEntry,
|
|
} from '@stock-bot/types';
|
|
|
|
export const historicalIntervals = ['1m', '5m', '15m', '1h', '1d'] as const;
|
|
export interface HistoricalSelection {
|
|
symbols: string[];
|
|
startDate: string;
|
|
endDate: string;
|
|
dataFrequency?: (typeof historicalIntervals)[number];
|
|
dataSource?: 'auto' | 'questdb' | 'eod';
|
|
priceAdjustment?: 'raw' | 'provider-adjusted';
|
|
datasetHash?: string;
|
|
}
|
|
|
|
/** Selecting a retained snapshot adopts its exact request, never a cropped range. */
|
|
export function retainedDatasetSelection(
|
|
entry: HistoricalDatasetCatalogEntry
|
|
): HistoricalSelection {
|
|
const manifest = entry.manifest;
|
|
if (entry.status !== 'available' || manifest?.version !== 1 || manifest.hash !== entry.hash) {
|
|
throw new Error(entry.reason ?? 'This dataset is unavailable in the current workbench');
|
|
}
|
|
const interval = historicalIntervals.find(value => value === manifest.interval);
|
|
if (!interval) {
|
|
throw new Error('This dataset timeframe is unsupported');
|
|
}
|
|
return {
|
|
symbols: manifest.instruments.map(instrument => instrument.id),
|
|
startDate: manifest.startDate,
|
|
endDate: manifest.endDate,
|
|
dataFrequency: interval,
|
|
dataSource: 'auto',
|
|
priceAdjustment: manifest.policy.adjustment,
|
|
datasetHash: entry.hash,
|
|
};
|
|
}
|
|
|
|
export function historicalSelectionError(
|
|
value: HistoricalSelection,
|
|
retained?: HistoricalDatasetCatalogEntry | null
|
|
): string | null {
|
|
try {
|
|
const frequency = value.dataFrequency ?? '1d';
|
|
if (!['auto', 'questdb', 'eod'].includes(value.dataSource ?? 'auto')) {
|
|
return 'Choose a supported historical source';
|
|
}
|
|
validateHistoricalDataRequest({
|
|
symbols: value.symbols,
|
|
startDate: value.startDate,
|
|
endDate: value.endDate,
|
|
dataFrequency: frequency,
|
|
dataPolicy: {
|
|
adjustment: value.priceAdjustment ?? 'raw',
|
|
timestamp: frequency === '1d' ? 'utc-session-date' : 'utc-instant',
|
|
},
|
|
datasetHash: value.datasetHash,
|
|
});
|
|
if (value.datasetHash !== undefined) {
|
|
if (!retained || retained.hash !== value.datasetHash) {
|
|
return 'Select an available frozen dataset and wait for its metadata';
|
|
}
|
|
const exact = retainedDatasetSelection(retained);
|
|
if (
|
|
JSON.stringify([...value.symbols].sort()) !== JSON.stringify([...exact.symbols].sort()) ||
|
|
new Date(value.startDate).toISOString() !== exact.startDate ||
|
|
new Date(value.endDate).toISOString() !== exact.endDate ||
|
|
frequency !== exact.dataFrequency ||
|
|
(value.priceAdjustment ?? 'raw') !== exact.priceAdjustment ||
|
|
(value.dataSource ?? 'auto') !== 'auto'
|
|
) {
|
|
return 'These settings do not match the frozen dataset. Apply its exact settings or switch to a source request.';
|
|
}
|
|
}
|
|
return null;
|
|
} catch (error) {
|
|
return error instanceof Error ? error.message : 'Invalid historical data settings';
|
|
}
|
|
}
|
|
|
|
/** A daily control displays the UTC session date; intraday controls retain the
|
|
* complete explicit-offset string, including milliseconds. */
|
|
export function historicalDateInput(value: string, frequency: string): string {
|
|
return frequency === '1d' ? value.slice(0, 10) : value;
|
|
}
|