T-123: M1.3h — @stock-bot/research: pure event-study panel builder (MarketDataStore daily bars + events table → Vault-safe EventStudyInput) with an end-to-end qmFilings event study test (store → runEventStudy → trial_ledger → DSR) and local benchmark (no new dependencies)
This commit is contained in:
parent
5e626b083b
commit
ee983cb349
5 changed files with 459 additions and 114 deletions
|
|
@ -7,6 +7,7 @@ import {
|
|||
runEventStudy,
|
||||
type DuckDbExecutor,
|
||||
type DuckDbParam,
|
||||
type EventStudyExchangeCalendars,
|
||||
type TableRow,
|
||||
type TrialLedgerExecutor,
|
||||
} from '@stock-bot/research';
|
||||
|
|
@ -145,6 +146,7 @@ interface QmFilingsFixture {
|
|||
readonly barRows: TableRow[];
|
||||
readonly adjFactorRows: AdjFactorRow[];
|
||||
readonly eventRows: TableRow[];
|
||||
readonly exchangeCalendars: EventStudyExchangeCalendars;
|
||||
readonly dividendCase: {
|
||||
readonly listingId: number;
|
||||
readonly exDate: string;
|
||||
|
|
@ -152,6 +154,16 @@ interface QmFilingsFixture {
|
|||
readonly expectedTotalReturn: number;
|
||||
readonly rawDropReturn: number;
|
||||
};
|
||||
readonly halfDayCase: {
|
||||
readonly usListingId: number;
|
||||
readonly caListingId: number;
|
||||
readonly halfDayDate: string;
|
||||
readonly halfDayIdx: number;
|
||||
readonly nextSessionDate: string;
|
||||
readonly nextSessionIdx: number;
|
||||
readonly usHalfDayReturn: number;
|
||||
readonly usNextSessionReturn: number;
|
||||
};
|
||||
}
|
||||
|
||||
function makeFencedAdjKvRows(vaultCutoffTs: number, rowsWritten: number): TableRow[] {
|
||||
|
|
@ -183,6 +195,42 @@ function buildQmFilingsFixture(poisonPostCutoff: boolean): QmFilingsFixture {
|
|||
const adjFactorRows: AdjFactorRow[] = [];
|
||||
const eventRows: TableRow[] = [];
|
||||
|
||||
// Black Friday 2022-11-25: NYSE closes early at 13:00 ET (18:00 UTC) while TSX closes at
|
||||
// 16:00 ET (21:00 UTC), even though curated bars_1d.ts stamps both at 16:00 ET (21:00 UTC).
|
||||
const halfDayDate = '2022-11-25';
|
||||
const halfDayIdx = allDates.indexOf(halfDayDate);
|
||||
if (halfDayIdx < 1 || halfDayIdx + 1 >= preCutoffSessions) {
|
||||
throw new Error(`Expected half-day ${halfDayDate} inside pre-cutoff range.`);
|
||||
}
|
||||
const nextSessionIdx = halfDayIdx + 1;
|
||||
const nextSessionDate = allDates[nextSessionIdx]!;
|
||||
const halfDayUsIdx = 0; // listingId 10_001 (XNYS)
|
||||
const halfDayCaIdx = 12; // listingId 50_001 (XTSE)
|
||||
const halfDayUsListingId = listingIds[halfDayUsIdx]!;
|
||||
const halfDayCaListingId = listingIds[halfDayCaIdx]!;
|
||||
let usHalfDayReturn = 0;
|
||||
let usNextSessionReturn = 0;
|
||||
|
||||
const nyseCalendar = allDates.map(date => ({
|
||||
date,
|
||||
closeTs: makeUtcMs(date, date === halfDayDate ? 18 : 21), // 13:00 ET on Black Friday, else 16:00 ET
|
||||
}));
|
||||
const tsxCalendar = allDates.map(date => ({
|
||||
date,
|
||||
closeTs: makeUtcMs(date, 21), // 16:00 ET on all sessions including Black Friday
|
||||
}));
|
||||
const listingCalendarId = new Map<number, string>();
|
||||
for (let l = 0; l < listingCount; l += 1) {
|
||||
listingCalendarId.set(listingIds[l]!, l < 12 ? 'XNYS' : 'XTSE');
|
||||
}
|
||||
const exchangeCalendars: EventStudyExchangeCalendars = {
|
||||
calendars: new Map([
|
||||
['XNYS', nyseCalendar],
|
||||
['XTSE', tsxCalendar],
|
||||
]),
|
||||
listingCalendarId,
|
||||
};
|
||||
|
||||
// Plant a 10% cash dividend inside a filing window for listing 0 (US, 10-q):
|
||||
// On t = 12, listing l = ((12/4)*2 + 0) % 24 = 6 files after close; choose listing 6
|
||||
// and ex-date t = 15 (day +2 inside its [0..+5] filing window).
|
||||
|
|
@ -233,7 +281,14 @@ function buildQmFilingsFixture(poisonPostCutoff: boolean): QmFilingsFixture {
|
|||
divRawDropReturn = nextRaw / prevRaw - 1;
|
||||
rawPrices[l] = nextRaw;
|
||||
} else {
|
||||
rawPrices[l] = Math.max(0.5, rawPrices[l]! * (1 + dailyRet));
|
||||
const prevRaw = rawPrices[l]!;
|
||||
const nextRaw = Math.max(0.5, prevRaw * (1 + dailyRet));
|
||||
if (l === halfDayUsIdx && t === halfDayIdx) {
|
||||
usHalfDayReturn = nextRaw / prevRaw - 1;
|
||||
} else if (l === halfDayUsIdx && t === nextSessionIdx) {
|
||||
usNextSessionReturn = nextRaw / prevRaw - 1;
|
||||
}
|
||||
rawPrices[l] = nextRaw;
|
||||
}
|
||||
|
||||
const closeVal = poisonPostCutoff && isPostCutoff ? 999_999 : rawPrices[l]!;
|
||||
|
|
@ -283,6 +338,37 @@ function buildQmFilingsFixture(poisonPostCutoff: boolean): QmFilingsFixture {
|
|||
payload_json: '{}',
|
||||
});
|
||||
}
|
||||
|
||||
// Emit half-day filings at 14:00 ET (19:00 UTC) on Black Friday 2022-11-25:
|
||||
// - NYSE already closed at 13:00 ET (18:00 UTC) -> anchors to Monday 2022-11-28.
|
||||
// - TSX closes at 16:00 ET (21:00 UTC) -> anchors to Friday 2022-11-25.
|
||||
if (t === halfDayIdx) {
|
||||
const halfDay14EtTs = makeUtcMs(date, 19);
|
||||
eventRows.push({
|
||||
listing_id: halfDayUsListingId,
|
||||
ts: halfDay14EtTs,
|
||||
known_at: halfDay14EtTs,
|
||||
known_at_source: 'exact',
|
||||
kind: 'filing',
|
||||
subtype: '10-q',
|
||||
payload_json: JSON.stringify({ market: 'US', form: '10-q', halfDay: true }),
|
||||
});
|
||||
eventRows.push({
|
||||
listing_id: halfDayCaListingId,
|
||||
ts: halfDay14EtTs,
|
||||
known_at: halfDay14EtTs,
|
||||
known_at_source: 'exact',
|
||||
kind: 'filing',
|
||||
subtype: 'interim_financial_statements',
|
||||
payload_json: JSON.stringify({
|
||||
market: 'CA',
|
||||
form: 'interim_financial_statements',
|
||||
halfDay: true,
|
||||
}),
|
||||
});
|
||||
remainingBoostSessions[halfDayUsIdx] = 5;
|
||||
remainingBoostSessions[halfDayCaIdx] = 5;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
@ -295,6 +381,7 @@ function buildQmFilingsFixture(poisonPostCutoff: boolean): QmFilingsFixture {
|
|||
barRows,
|
||||
adjFactorRows,
|
||||
eventRows,
|
||||
exchangeCalendars,
|
||||
dividendCase: {
|
||||
listingId: divListingId,
|
||||
exDate: divExDate,
|
||||
|
|
@ -302,6 +389,16 @@ function buildQmFilingsFixture(poisonPostCutoff: boolean): QmFilingsFixture {
|
|||
expectedTotalReturn: divExpectedTotalReturn,
|
||||
rawDropReturn: divRawDropReturn,
|
||||
},
|
||||
halfDayCase: {
|
||||
usListingId: halfDayUsListingId,
|
||||
caListingId: halfDayCaListingId,
|
||||
halfDayDate,
|
||||
halfDayIdx,
|
||||
nextSessionDate,
|
||||
nextSessionIdx,
|
||||
usHalfDayReturn,
|
||||
usNextSessionReturn,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -474,6 +571,7 @@ describe('qmFilings end-to-end event study (headless, no database)', () => {
|
|||
events,
|
||||
vaultCutoffDate: fixture.vaultCutoffDate,
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars: fixture.exchangeCalendars,
|
||||
});
|
||||
|
||||
const studyResult = await runEventStudy(ledger.executor, {
|
||||
|
|
@ -545,6 +643,31 @@ describe('qmFilings end-to-end event study (headless, no database)', () => {
|
|||
const divSeries = clean.built.input.entityReturns.get(divCase.listingId)!;
|
||||
expect(divSeries[divCase.exDayIdx]!).toBeCloseTo(divCase.expectedTotalReturn, 10);
|
||||
|
||||
// Half-day anchoring: a US ('10-q') filing published at 14:00 ET on Black Friday 2022-11-25
|
||||
// (after NYSE's 13:00 ET close, even though curated bars_1d.ts stamps 16:00 ET) MUST anchor
|
||||
// to Monday 2022-11-28 (not Friday), while the CA ('interim_financial_statements') filing at
|
||||
// 14:00 ET anchors to Friday 2022-11-25 (before TSX's 16:00 ET close).
|
||||
const halfDay = clean.fixture.halfDayCase;
|
||||
expect(
|
||||
clean.built.input.events.some(
|
||||
e => e.entityId === halfDay.usListingId && e.dayIndex === halfDay.halfDayIdx
|
||||
)
|
||||
).toBe(false);
|
||||
expect(
|
||||
clean.built.input.events.some(
|
||||
e => e.entityId === halfDay.usListingId && e.dayIndex === halfDay.nextSessionIdx
|
||||
)
|
||||
).toBe(true);
|
||||
expect(
|
||||
clean.built.input.events.some(
|
||||
e => e.entityId === halfDay.caListingId && e.dayIndex === halfDay.halfDayIdx
|
||||
)
|
||||
).toBe(true);
|
||||
const usHalfDaySeries = clean.built.input.entityReturns.get(halfDay.usListingId)!;
|
||||
expect(clean.built.input.sessionDates[halfDay.nextSessionIdx]).toBe(halfDay.nextSessionDate);
|
||||
expect(usHalfDaySeries[halfDay.nextSessionIdx]!).toBeCloseTo(halfDay.usNextSessionReturn, 12);
|
||||
expect(halfDay.usNextSessionReturn).not.toBeCloseTo(halfDay.usHalfDayReturn, 6);
|
||||
|
||||
// Event study succeeded and appended exactly one `event_study` row to trial_ledger.
|
||||
expect(clean.studyResult.status).toBe('ok');
|
||||
if (clean.studyResult.status !== 'ok') {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import {
|
|||
type BarRecord,
|
||||
type EventObservation,
|
||||
type EventPanelErrorCode,
|
||||
type EventStudyExchangeCalendars,
|
||||
type Table,
|
||||
} from '@stock-bot/research';
|
||||
import {
|
||||
|
|
@ -51,6 +52,22 @@ function makeBar(listingId: number, date: string, close: number, hourUtc = 21):
|
|||
};
|
||||
}
|
||||
|
||||
function makeExchangeCalendars(
|
||||
listingIds: readonly number[],
|
||||
dates: readonly string[],
|
||||
hourUtc = 21
|
||||
): EventStudyExchangeCalendars {
|
||||
const sessions = dates.map(date => ({ date, closeTs: makeUtcMs(date, hourUtc) }));
|
||||
const listingCalendarId = new Map<number, string>();
|
||||
for (const id of listingIds) {
|
||||
listingCalendarId.set(id, 'XNYS');
|
||||
}
|
||||
return {
|
||||
calendars: new Map([['XNYS', sessions]]),
|
||||
listingCalendarId,
|
||||
};
|
||||
}
|
||||
|
||||
function createSeededRng(seed: number): () => number {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
|
|
@ -311,10 +328,13 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
{ listingId: 10, knownAt: makeUtcMs(dates[1]!, 21), kind: 'filing', subtype: '10-Q' },
|
||||
];
|
||||
|
||||
const exchangeCalendars = makeExchangeCalendars([10, 20], dates);
|
||||
|
||||
const resNone = buildEventStudyInput({
|
||||
bars,
|
||||
events,
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars,
|
||||
});
|
||||
|
||||
expect(resNone.input.sessionDates).toEqual(dates);
|
||||
|
|
@ -340,6 +360,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events,
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars,
|
||||
});
|
||||
expect(resEq.input.benchmarkReturns).toBeInstanceOf(Float64Array);
|
||||
const b = resEq.input.benchmarkReturns!;
|
||||
|
|
@ -366,6 +387,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars: makeExchangeCalendars([1], dates),
|
||||
});
|
||||
|
||||
const series = res.input.entityReturns.get(1)!;
|
||||
|
|
@ -428,6 +450,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars,
|
||||
events,
|
||||
vaultCutoffDate: '2024-01-13',
|
||||
exchangeCalendars: makeExchangeCalendars([1, 2], dates),
|
||||
});
|
||||
|
||||
expect(res.input.events).toEqual([
|
||||
|
|
@ -450,10 +473,13 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
const b1 = makeBar(1, dates[1]!, 12);
|
||||
const b2 = makeBar(1, dates[2]!, Number.NaN);
|
||||
|
||||
const exchangeCalendars = makeExchangeCalendars([1], dates);
|
||||
|
||||
const exactDup = buildEventStudyInput({
|
||||
bars: [b0, { ...b0 }, b1, b2, { ...b2 }],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars,
|
||||
});
|
||||
expect(exactDup.diagnostics.duplicateBars).toBe(2);
|
||||
expect(exactDup.input.entityReturns.get(1)![1]).toBeCloseTo(0.2, 12);
|
||||
|
|
@ -465,6 +491,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: [b0, { ...b0, close: 10.01 }, b1],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars,
|
||||
}),
|
||||
'duplicate_bar'
|
||||
);
|
||||
|
|
@ -476,6 +503,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: [b0, { ...b0, ts: b0.ts + 1_000 }, b1],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars,
|
||||
}),
|
||||
'duplicate_bar'
|
||||
);
|
||||
|
|
@ -533,11 +561,14 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
{ listingId: 10, knownAt: makeUtcMs(dates[5]!, 22), kind: 'filing', subtype: '10-Q' },
|
||||
];
|
||||
|
||||
const exchangeCalendars = makeExchangeCalendars([10], dates);
|
||||
|
||||
const trappedResult = buildEventStudyInput({
|
||||
bars: [...preBars, ...postBars],
|
||||
events,
|
||||
vaultCutoffDate,
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars,
|
||||
});
|
||||
|
||||
expect(postCutoffCloseReads).toBe(0);
|
||||
|
|
@ -578,6 +609,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
],
|
||||
vaultCutoffDate,
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars,
|
||||
});
|
||||
expectBitIdenticalEventStudyInput(poisonedResult.input, trappedResult.input);
|
||||
});
|
||||
|
|
@ -607,6 +639,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
});
|
||||
}
|
||||
|
||||
const exchangeCalendars = makeExchangeCalendars([10, 20, 30], dates);
|
||||
const frozenBars = Object.freeze(bars.map(b => Object.freeze({ ...b })));
|
||||
const frozenEvents = Object.freeze(events.map(e => Object.freeze({ ...e })));
|
||||
const frozenParams = Object.freeze({
|
||||
|
|
@ -614,6 +647,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: frozenEvents,
|
||||
vaultCutoffDate: dates[6]!,
|
||||
benchmark: 'equal_weight' as const,
|
||||
exchangeCalendars,
|
||||
});
|
||||
|
||||
const baseline = buildEventStudyInput(frozenParams);
|
||||
|
|
@ -623,6 +657,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: shuffle(events, seed + 17),
|
||||
vaultCutoffDate: dates[6]!,
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars,
|
||||
});
|
||||
expectBitIdenticalEventStudyInput(permuted.input, baseline.input);
|
||||
expect(permuted.diagnostics).toEqual(baseline.diagnostics);
|
||||
|
|
@ -631,6 +666,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
});
|
||||
|
||||
it('handles empty and post-cutoff-only inputs cleanly', () => {
|
||||
const emptyCalendars: EventStudyExchangeCalendars = { calendars: {}, listingCalendarId: {} };
|
||||
const emptyRes = buildEventStudyInput({
|
||||
bars: [],
|
||||
events: [
|
||||
|
|
@ -638,6 +674,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
{ listingId: 1, knownAt: makeUtcMs('2024-02-01', 0), kind: 'filing', subtype: '10-Q' },
|
||||
],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
});
|
||||
expect(emptyRes.input.sessionDates).toEqual([]);
|
||||
expect(emptyRes.input.entityReturns.size).toBe(0);
|
||||
|
|
@ -659,6 +696,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
});
|
||||
expect(postOnlyRes.input.sessionDates).toEqual([]);
|
||||
expect(postOnlyRes.input.benchmarkReturns).toEqual(new Float64Array(0));
|
||||
|
|
@ -666,6 +704,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
});
|
||||
|
||||
it('throws typed EventPanelError for every remaining validation error code', () => {
|
||||
const emptyCalendars: EventStudyExchangeCalendars = { calendars: {}, listingCalendarId: {} };
|
||||
expectEventPanelError(
|
||||
() => buildEventStudyInput(null as unknown as Parameters<typeof buildEventStudyInput>[0]),
|
||||
'invalid_input'
|
||||
|
|
@ -676,6 +715,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: 'not-array' as unknown as BarRecord[],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_input'
|
||||
);
|
||||
|
|
@ -685,16 +725,29 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: [],
|
||||
events: 'not-array' as unknown as EventObservation[],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_input'
|
||||
);
|
||||
|
||||
expectEventPanelError(
|
||||
() => buildEventStudyInput({ bars: [], events: [], vaultCutoffDate: '2024-02-30' }),
|
||||
() =>
|
||||
buildEventStudyInput({
|
||||
bars: [],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-30',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_date'
|
||||
);
|
||||
expectEventPanelError(
|
||||
() => buildEventStudyInput({ bars: [], events: [], vaultCutoffDate: '2024/02/01' }),
|
||||
() =>
|
||||
buildEventStudyInput({
|
||||
bars: [],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024/02/01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_date'
|
||||
);
|
||||
|
||||
|
|
@ -705,6 +758,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: 'cap_weight' as unknown as 'none',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_option'
|
||||
);
|
||||
|
|
@ -715,6 +769,17 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: null as unknown as 'none',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_option'
|
||||
);
|
||||
expectEventPanelError(
|
||||
() =>
|
||||
buildEventStudyInput({
|
||||
bars: [],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: undefined as unknown as EventStudyExchangeCalendars,
|
||||
}),
|
||||
'invalid_option'
|
||||
);
|
||||
|
|
@ -733,6 +798,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: [badBar as BarRecord],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_bar'
|
||||
);
|
||||
|
|
@ -757,6 +823,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
bars: [],
|
||||
events: [badEv as EventObservation],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: emptyCalendars,
|
||||
}),
|
||||
'invalid_event'
|
||||
);
|
||||
|
|
@ -837,6 +904,12 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
{ entityId: tsxId, dayIndex: 2 }, // 2024-11-29 (14:00 ET still before TSX 16:00 ET close)
|
||||
{ entityId: nyseId, dayIndex: 3 }, // 2024-12-02 (14:00 ET after NYSE 13:00 ET close)
|
||||
]);
|
||||
// Cross-border holiday: NYSE was closed on Thanksgiving 2024-11-28, so its Friday
|
||||
// 2024-11-29 return (index 2) is a valid 1-session NYSE return (102 / 100 - 1 = 0.02).
|
||||
const nyseRets = mapResult.input.entityReturns.get(nyseId)!;
|
||||
expect(Number.isNaN(nyseRets[1]!)).toBe(true);
|
||||
expect(nyseRets[2]!).toBeCloseTo(0.02, 12);
|
||||
expect(nyseRets[3]!).toBeCloseTo(105 / 102 - 1, 12);
|
||||
expect(mapResult.diagnostics.duplicateEvents).toBe(1);
|
||||
expect(mapResult.diagnostics.droppedAfterLastSession).toBe(0);
|
||||
expect(mapResult.diagnostics.droppedMissingCalendarSession).toBe(0);
|
||||
|
|
@ -991,9 +1064,7 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
() =>
|
||||
buildEventStudyInput({
|
||||
bars,
|
||||
events: [
|
||||
{ listingId: nyseId, knownAt: makeUtcMs('2024-11-27', 15), kind: 'filing', subtype: '10-q' },
|
||||
],
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-12-02',
|
||||
exchangeCalendars: {
|
||||
calendars: { XNYS: nyseCalendar },
|
||||
|
|
@ -1003,4 +1074,108 @@ describe('buildEventStudyInput — returns, PIT anchoring, Vault fence, and inva
|
|||
'invalid_option'
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects incomplete exchange calendar coverage when a calendar starts after or omits pre-Vault bar dates', () => {
|
||||
const nyseId = 10_001;
|
||||
const dec2023AndJan2024Bars: BarRecord[] = [
|
||||
makeBar(nyseId, '2023-12-28', 100, 21),
|
||||
makeBar(nyseId, '2023-12-29', 101, 21),
|
||||
makeBar(nyseId, '2024-01-02', 103, 21),
|
||||
makeBar(nyseId, '2024-01-03', 104, 21),
|
||||
];
|
||||
|
||||
// Calendar starts on Jan 2, 2024 while bars include December 2023 -> must throw invalid_option.
|
||||
expectEventPanelError(
|
||||
() =>
|
||||
buildEventStudyInput({
|
||||
bars: dec2023AndJan2024Bars,
|
||||
events: [
|
||||
{
|
||||
listingId: nyseId,
|
||||
knownAt: makeUtcMs('2023-12-29', 19), // 14:00 ET on Dec 29
|
||||
kind: 'filing',
|
||||
subtype: '10-q',
|
||||
},
|
||||
],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: {
|
||||
calendars: {
|
||||
XNYS: [
|
||||
{ date: '2024-01-02', closeTs: makeUtcMs('2024-01-02', 21) },
|
||||
{ date: '2024-01-03', closeTs: makeUtcMs('2024-01-03', 21) },
|
||||
],
|
||||
},
|
||||
listingCalendarId: { [nyseId]: 'XNYS' },
|
||||
},
|
||||
}),
|
||||
'invalid_option'
|
||||
);
|
||||
|
||||
// Calendar omits a mid-history bar session (2023-12-29) -> must throw invalid_option.
|
||||
expectEventPanelError(
|
||||
() =>
|
||||
buildEventStudyInput({
|
||||
bars: dec2023AndJan2024Bars,
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
exchangeCalendars: {
|
||||
calendars: {
|
||||
XNYS: [
|
||||
{ date: '2023-12-28', closeTs: makeUtcMs('2023-12-28', 21) },
|
||||
{ date: '2024-01-02', closeTs: makeUtcMs('2024-01-02', 21) },
|
||||
{ date: '2024-01-03', closeTs: makeUtcMs('2024-01-03', 21) },
|
||||
],
|
||||
},
|
||||
listingCalendarId: { [nyseId]: 'XNYS' },
|
||||
},
|
||||
}),
|
||||
'invalid_option'
|
||||
);
|
||||
});
|
||||
|
||||
it('uses exchange-calendar adjacency so a missing open session for all listings yields NaN on the next bar', () => {
|
||||
// Exchange is open Mon, Tue, Wed, Thu, but bars exist only on Mon, Wed, Thu (Tue missing for ALL listings).
|
||||
const mon = '2024-01-08';
|
||||
const tue = '2024-01-09';
|
||||
const wed = '2024-01-10';
|
||||
const thu = '2024-01-11';
|
||||
const bars: BarRecord[] = [
|
||||
makeBar(10, mon, 100),
|
||||
makeBar(10, wed, 110),
|
||||
makeBar(10, thu, 121),
|
||||
makeBar(20, mon, 50),
|
||||
makeBar(20, wed, 55),
|
||||
makeBar(20, thu, 60.5),
|
||||
];
|
||||
|
||||
const res = buildEventStudyInput({
|
||||
bars,
|
||||
events: [],
|
||||
vaultCutoffDate: '2024-02-01',
|
||||
benchmark: 'equal_weight',
|
||||
exchangeCalendars: makeExchangeCalendars([10, 20], [mon, tue, wed, thu]),
|
||||
});
|
||||
|
||||
// sessionDates contains only observed bar dates [Mon, Wed, Thu].
|
||||
expect(res.input.sessionDates).toEqual([mon, wed, thu]);
|
||||
|
||||
const r10 = res.input.entityReturns.get(10)!;
|
||||
const r20 = res.input.entityReturns.get(20)!;
|
||||
const bench = res.input.benchmarkReturns!;
|
||||
|
||||
// Mon (index 0): NaN (first bar)
|
||||
expect(Number.isNaN(r10[0]!)).toBe(true);
|
||||
expect(Number.isNaN(r20[0]!)).toBe(true);
|
||||
expect(Number.isNaN(bench[0]!)).toBe(true);
|
||||
|
||||
// Wed (index 1): MUST be NaN because Tue was an open exchange session (not consecutive).
|
||||
expect(Number.isNaN(r10[1]!)).toBe(true);
|
||||
expect(Number.isNaN(r20[1]!)).toBe(true);
|
||||
expect(Number.isNaN(bench[1]!)).toBe(true);
|
||||
|
||||
// Thu (index 2): consecutive with Wed -> valid +10% return.
|
||||
expect(r10[2]!).toBeCloseTo(0.1, 12);
|
||||
expect(r20[2]!).toBeCloseTo(0.1, 12);
|
||||
expect(bench[2]!).toBeCloseTo(0.1, 12);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue