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') {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue