Freeze historical datasets and qualify backtest instrument identities
This commit is contained in:
parent
63c3856797
commit
d6b09eb635
43 changed files with 2249 additions and 1230 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import type { HistoricalDatasetManifest } from './historical-data';
|
||||
|
||||
/** Transport-neutral contract shared by backtest entry points and result readers. */
|
||||
export interface BacktestRunConfiguration {
|
||||
name: string;
|
||||
|
|
@ -10,6 +12,8 @@ export interface BacktestRunConfiguration {
|
|||
slippage: number;
|
||||
dataFrequency: string;
|
||||
parameters: Record<string, unknown>;
|
||||
/** Missing only on historical results without frozen input provenance. */
|
||||
dataset?: HistoricalDatasetManifest;
|
||||
riskLimits?: {
|
||||
maxPositionSize: number;
|
||||
maxOrderSize: number;
|
||||
|
|
|
|||
117
libs/core/types/src/historical-data.ts
Normal file
117
libs/core/types/src/historical-data.ts
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
/** Listing IDs use the existing EOD provider exchange namespace, not a guessed MIC. */
|
||||
export interface InstrumentIdentity {
|
||||
id: string;
|
||||
symbol: string;
|
||||
exchange: string;
|
||||
}
|
||||
|
||||
export class HistoricalDataRequestError extends Error {}
|
||||
|
||||
export function parseInstrumentId(id: string): InstrumentIdentity {
|
||||
const match = /^([A-Z0-9][A-Z0-9._^-]*)\.([A-Z0-9][A-Z0-9_-]*)$/.exec(id);
|
||||
if (!match?.[1] || !match[2] || id.length > 128) {
|
||||
throw new HistoricalDataRequestError(
|
||||
'Use an exchange-qualified instrument ID, for example AAPL.US'
|
||||
);
|
||||
}
|
||||
return { id, symbol: match[1], exchange: match[2] };
|
||||
}
|
||||
|
||||
export interface HistoricalDataPolicy {
|
||||
adjustment: 'raw' | 'provider-adjusted';
|
||||
timestamp: 'utc-session-date' | 'utc-instant';
|
||||
}
|
||||
|
||||
/** Validate transport-independent data choices before accepting a durable run. */
|
||||
export function validateHistoricalDataRequest(input: {
|
||||
symbols: string[];
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
dataFrequency?: unknown;
|
||||
dataPolicy?: unknown;
|
||||
datasetHash?: unknown;
|
||||
}): void {
|
||||
if (
|
||||
!Array.isArray(input.symbols) ||
|
||||
!input.symbols.length ||
|
||||
new Set(input.symbols).size !== input.symbols.length
|
||||
) {
|
||||
throw new HistoricalDataRequestError(
|
||||
'Choose one or more distinct exchange-qualified instruments'
|
||||
);
|
||||
}
|
||||
input.symbols.forEach(parseInstrumentId);
|
||||
const frequency = input.dataFrequency ?? '1d';
|
||||
if (typeof frequency !== 'string' || !['1m', '5m', '15m', '1h', '1d'].includes(frequency)) {
|
||||
throw new HistoricalDataRequestError('Unsupported historical interval');
|
||||
}
|
||||
if (
|
||||
![input.startDate, input.endDate].every(
|
||||
date =>
|
||||
typeof date === 'string' &&
|
||||
(/^\d{4}-\d{2}-\d{2}$/.test(date) || /(Z|[+-]\d{2}:\d{2})$/.test(date))
|
||||
)
|
||||
) {
|
||||
throw new HistoricalDataRequestError('Historical instants require an explicit UTC offset');
|
||||
}
|
||||
const start = Date.parse(input.startDate),
|
||||
end = Date.parse(input.endDate);
|
||||
if (
|
||||
!Number.isFinite(start) ||
|
||||
!Number.isFinite(end) ||
|
||||
end <= start ||
|
||||
(frequency === '1d' && (start % 86400000 !== 0 || end % 86400000 !== 0))
|
||||
) {
|
||||
throw new HistoricalDataRequestError(
|
||||
'Use an increasing range; daily ranges require UTC midnight session dates'
|
||||
);
|
||||
}
|
||||
if (input.dataPolicy !== undefined) {
|
||||
const policy = input.dataPolicy;
|
||||
if (
|
||||
!policy ||
|
||||
typeof policy !== 'object' ||
|
||||
!('adjustment' in policy) ||
|
||||
!('timestamp' in policy) ||
|
||||
!['raw', 'provider-adjusted'].includes(String(policy.adjustment)) ||
|
||||
policy.timestamp !== (frequency === '1d' ? 'utc-session-date' : 'utc-instant')
|
||||
) {
|
||||
throw new HistoricalDataRequestError('Invalid historical adjustment/timestamp policy');
|
||||
}
|
||||
}
|
||||
if (
|
||||
input.datasetHash !== undefined &&
|
||||
(typeof input.datasetHash !== 'string' || !/^[a-f0-9]{64}$/.test(input.datasetHash))
|
||||
) {
|
||||
throw new HistoricalDataRequestError('Invalid dataset hash');
|
||||
}
|
||||
}
|
||||
|
||||
export interface HistoricalDatasetManifest {
|
||||
version: 1;
|
||||
hash: string;
|
||||
instruments: InstrumentIdentity[];
|
||||
startDate: string;
|
||||
endDate: string;
|
||||
interval: string;
|
||||
policy: HistoricalDataPolicy;
|
||||
/** Adjusted-close ratios include splits/dividends; reported volume is never rescaled. */
|
||||
volumePolicy: 'reported';
|
||||
qualityPolicy: 'reject-invalid-no-interpolation';
|
||||
sources: { instrumentId: string; provider: 'eod' | 'questdb' | 'synthetic'; revision: string }[];
|
||||
barCount: number;
|
||||
}
|
||||
|
||||
export interface HistoricalDataset {
|
||||
manifest: HistoricalDatasetManifest;
|
||||
bars: {
|
||||
symbol: string;
|
||||
timestamp: number;
|
||||
open: number;
|
||||
high: number;
|
||||
low: number;
|
||||
close: number;
|
||||
volume: number;
|
||||
vwap: number;
|
||||
}[];
|
||||
}
|
||||
|
|
@ -93,3 +93,14 @@ export {
|
|||
parseReferenceDataResetRequest,
|
||||
} from './reference-data';
|
||||
export type { ReferenceDataResetRequest, ReferenceDataResetScope } from './reference-data';
|
||||
export {
|
||||
parseInstrumentId,
|
||||
HistoricalDataRequestError,
|
||||
validateHistoricalDataRequest,
|
||||
} from './historical-data';
|
||||
export type {
|
||||
InstrumentIdentity,
|
||||
HistoricalDataPolicy,
|
||||
HistoricalDataset,
|
||||
HistoricalDatasetManifest,
|
||||
} from './historical-data';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue