diff --git a/apps/stock/data-pipeline/src/security-master/listing-stage-b.ts b/apps/stock/data-pipeline/src/security-master/listing-stage-b.ts index a2c1e77b..c7af082a 100644 --- a/apps/stock/data-pipeline/src/security-master/listing-stage-b.ts +++ b/apps/stock/data-pipeline/src/security-master/listing-stage-b.ts @@ -177,9 +177,14 @@ interface ListingAccumulator { ibPrimaryExchange: string | null; } +interface AliasBounds { + readonly validFrom: string | null; + readonly validTo: string; +} + interface TickerLookupCandidate { readonly acc: ListingAccumulator; - readonly aliasSpan: SpanDateRange | null; + readonly aliasBounds: AliasBounds | null; } interface ValidatedSymbolChange { @@ -955,7 +960,7 @@ export function matchListingsStageB( tickerList = []; listingsByTickerKey.set(tickerKey, tickerList); } - tickerList.push({ acc, aliasSpan: null }); + tickerList.push({ acc, aliasBounds: null }); } else if (validSpan !== null) { // Associative & commutative min/max widening ensures shuffle invariance. if (acc.listedFrom === null || validSpan.firstBarDate < acc.listedFrom) { @@ -996,31 +1001,18 @@ export function matchListingsStageB( change.oldTickerRoot, change.oldClassSuffix ); - let candidate: TickerLookupCandidate | null = null; - if (acc.listedFrom === null || acc.listedTo === null) { - candidate = { acc, aliasSpan: null }; - } else { - const aliasFrom = - change.validFrom !== null && change.validFrom > acc.listedFrom - ? change.validFrom - : acc.listedFrom; - const aliasTo = - change.dayBeforeEffective < acc.listedTo ? change.dayBeforeEffective : acc.listedTo; - if (aliasFrom <= aliasTo) { - candidate = { - acc, - aliasSpan: { firstBarDate: aliasFrom, lastBarDate: aliasTo }, - }; - } - } - if (candidate !== null) { - let tickerList = listingsByTickerKey.get(aliasTickerKey); - if (tickerList === undefined) { - tickerList = []; - listingsByTickerKey.set(aliasTickerKey, tickerList); - } - tickerList.push(candidate); + let tickerList = listingsByTickerKey.get(aliasTickerKey); + if (tickerList === undefined) { + tickerList = []; + listingsByTickerKey.set(aliasTickerKey, tickerList); } + tickerList.push({ + acc, + aliasBounds: { + validFrom: change.validFrom, + validTo: change.dayBeforeEffective, + }, + }); } for (const [sourceId, count] of eodMetadataCounts.entries()) { @@ -1308,7 +1300,7 @@ export function matchListingsStageB( continue; } - const overlappingSet = new Set(); + const matchedSpanByAcc = new Map(); const missingSpanSet = new Set(); for (let k = 0; k < candidates.length; k++) { @@ -1316,24 +1308,70 @@ export function matchListingsStageB( if (cand === undefined) { continue; } - let candSpan: SpanDateRange | null; - if (cand.aliasSpan !== null) { - candSpan = cand.aliasSpan; - } else if (cand.acc.listedFrom !== null && cand.acc.listedTo !== null) { - candSpan = { firstBarDate: cand.acc.listedFrom, lastBarDate: cand.acc.listedTo }; - } else { - candSpan = null; + + if (cand.aliasBounds === null) { + if (cand.acc.listedFrom === null || cand.acc.listedTo === null) { + missingSpanSet.add(cand.acc); + } else { + const listingSpan: SpanDateRange = { + firstBarDate: cand.acc.listedFrom, + lastBarDate: cand.acc.listedTo, + }; + if (spansOverlap(spanCheck.span, listingSpan, overlapOptions)) { + matchedSpanByAcc.set(cand.acc, spanCheck.span); + } + } + continue; } - if (candSpan === null) { + // Explicit symbol-change alias bounds are hard limits independent of spanOverlapToleranceDays. + const intFrom = + cand.aliasBounds.validFrom !== null && + cand.aliasBounds.validFrom > spanCheck.span.firstBarDate + ? cand.aliasBounds.validFrom + : spanCheck.span.firstBarDate; + const intTo = + cand.aliasBounds.validTo < spanCheck.span.lastBarDate + ? cand.aliasBounds.validTo + : spanCheck.span.lastBarDate; + if (intFrom > intTo) { + continue; + } + + if (cand.acc.listedFrom === null || cand.acc.listedTo === null) { missingSpanSet.add(cand.acc); - } else if (spansOverlap(spanCheck.span, candSpan, overlapOptions)) { - overlappingSet.add(cand.acc); + continue; + } + + const effFrom = + cand.aliasBounds.validFrom !== null && cand.aliasBounds.validFrom > cand.acc.listedFrom + ? cand.aliasBounds.validFrom + : cand.acc.listedFrom; + const effTo = + cand.aliasBounds.validTo < cand.acc.listedTo + ? cand.aliasBounds.validTo + : cand.acc.listedTo; + if (effFrom > effTo) { + continue; + } + + const clampedProviderSpan: SpanDateRange = { firstBarDate: intFrom, lastBarDate: intTo }; + const aliasListingSpan: SpanDateRange = { firstBarDate: effFrom, lastBarDate: effTo }; + if (spansOverlap(clampedProviderSpan, aliasListingSpan, overlapOptions)) { + const existing = matchedSpanByAcc.get(cand.acc); + if (existing === undefined) { + matchedSpanByAcc.set(cand.acc, clampedProviderSpan); + } else { + matchedSpanByAcc.set(cand.acc, { + firstBarDate: intFrom < existing.firstBarDate ? intFrom : existing.firstBarDate, + lastBarDate: intTo > existing.lastBarDate ? intTo : existing.lastBarDate, + }); + } } } - if (overlappingSet.size > 1) { - const candidateListingKeys = Array.from(overlappingSet, c => c.key).sort( + if (matchedSpanByAcc.size > 1) { + const candidateListingKeys = Array.from(matchedSpanByAcc.keys(), c => c.key).sort( compareListingNaturalKey ); conflicts.push({ @@ -1358,16 +1396,17 @@ export function matchListingsStageB( primaryIsin: null, }); sourceStats.unresolved += 1; - } else if (overlappingSet.size === 1) { - const matchedListing = overlappingSet.values().next().value; - if (matchedListing !== undefined) { + } else if (matchedSpanByAcc.size === 1) { + const matchedEntry = matchedSpanByAcc.entries().next().value; + if (matchedEntry !== undefined) { + const [matchedListing, matchedSpan] = matchedEntry; providerMappings.push({ source, sourceId: record.sourceId, listingKey: matchedListing.key, method: 'tier3_ticker_span', - validFrom: spanCheck.span.firstBarDate, - validTo: spanCheck.span.lastBarDate, + validFrom: matchedSpan.firstBarDate, + validTo: matchedSpan.lastBarDate, }); sourceStats.matched += 1; } diff --git a/test/unit/security-master-listing-stage-b.test.ts b/test/unit/security-master-listing-stage-b.test.ts index 269d6414..18b9d6b3 100644 --- a/test/unit/security-master-listing-stage-b.test.ts +++ b/test/unit/security-master-listing-stage-b.test.ts @@ -1934,6 +1934,309 @@ describe('security master Pass 3 Stage B listing matcher', () => { ).toThrow(RangeError); }); + test('enforces hard symbolChanges alias bounds across crossing spans, toleranceDays, and undated listings (T-59)', () => { + const instruments: StageAInstrumentCandidate[] = [ + { + primaryIsin: 'US30303M1027', + primaryCusip: '30303M102', + issuerCountry: 'US', + canonicalName: 'Meta Platforms Inc.', + members: [{ source: 'eod', sourceId: 'META.US', currency: 'USD', method: 'tier1_isin' }], + }, + { + primaryIsin: 'US0000000026', + primaryCusip: null, + issuerCountry: 'US', + canonicalName: 'FB Reused Ticker 2025', + members: [{ source: 'eod', sourceId: 'FB.US', currency: 'USD', method: 'tier1_isin' }], + }, + { + primaryIsin: 'US5555555555', + primaryCusip: null, + issuerCountry: 'US', + canonicalName: 'Chained Rename Corp', + members: [{ source: 'eod', sourceId: 'CUR.US', currency: 'USD', method: 'tier1_isin' }], + }, + ]; + + const datedEodListings: StageBEodListingInput[] = [ + { + sourceId: 'META.US', + ticker: 'META', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2012-05-18', lastBarDate: '2025-11-26' }, + }, + { + sourceId: 'FB.US', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2025-01-02', lastBarDate: '2025-11-26' }, + }, + { + sourceId: 'CUR.US', + ticker: 'CUR', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2010-01-04', lastBarDate: '2025-11-26' }, + }, + ]; + + const symbolChanges: StageBSymbolChangeInput[] = [ + { eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }, + { + eodSourceId: 'CUR.US', + oldTicker: 'OLD2', + validFrom: '2015-01-01', + effectiveDate: '2020-03-01', + }, + ]; + + // 1. Default options: a provider span crossing the rename date (June 8–10, 2022) must bound + // emitted validity to proven alias dates only (June 8), never assigning June 9–10 to Meta. + const crossingResult = matchListingsStageB({ + instruments, + eodListings: datedEodListings, + ibContracts: [], + tickerSources: [ + { + source: 'qm', + sourceId: 'QM_FB_CROSS_RENAME', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2022-06-08', lastBarDate: '2022-06-10' }, + }, + { + source: 'qm', + sourceId: 'QM_OLD2_CROSS_BOTH_BOUNDS', + ticker: 'OLD2', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2014-12-30', lastBarDate: '2020-03-05' }, + }, + ], + symbolChanges, + }); + + expect(crossingResult.unresolved).toEqual([]); + expect(crossingResult.conflicts).toEqual([]); + expect(crossingResult.providerMappings.filter(m => m.source === 'qm')).toEqual([ + { + source: 'qm', + sourceId: 'QM_FB_CROSS_RENAME', + listingKey: { + primaryIsin: 'US30303M1027', + mic: 'XNAS', + tickerRoot: 'META', + classSuffix: '', + currency: 'USD', + }, + method: 'tier3_ticker_span', + validFrom: '2022-06-08', + validTo: '2022-06-08', + }, + { + source: 'qm', + sourceId: 'QM_OLD2_CROSS_BOTH_BOUNDS', + listingKey: { + primaryIsin: 'US5555555555', + mic: 'XNAS', + tickerRoot: 'CUR', + classSuffix: '', + currency: 'USD', + }, + method: 'tier3_ticker_span', + validFrom: '2015-01-01', + validTo: '2020-02-29', + }, + ]); + assertReconciliationAndContracts(crossingResult); + + // 2. spanOverlapToleranceDays = 1: explicit alias bounds remain hard limits, while ordinary + // primary listing matches still honor toleranceDays. + const toleranceResult = matchListingsStageB( + { + instruments, + eodListings: datedEodListings, + ibContracts: [], + tickerSources: [ + { + source: 'qm', + sourceId: 'QM_FB_EFFECTIVE_DATE', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2022-06-09', lastBarDate: '2022-06-09' }, + }, + { + source: 'qm', + sourceId: 'QM_OLD2_BEFORE_VALID_FROM', + ticker: 'OLD2', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2014-12-31', lastBarDate: '2014-12-31' }, + }, + { + source: 'qm', + sourceId: 'QM_FB_2025_PRIMARY_TOLERANCE', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2025-01-01', lastBarDate: '2025-01-01' }, + }, + ], + symbolChanges, + }, + { spanOverlapToleranceDays: 1 } + ); + + expect(toleranceResult.conflicts).toEqual([]); + expect(toleranceResult.unresolved).toEqual([ + { + source: 'qm', + sourceId: 'QM_FB_EFFECTIVE_DATE', + reason: 'no_span_overlap', + primaryIsin: null, + }, + { + source: 'qm', + sourceId: 'QM_OLD2_BEFORE_VALID_FROM', + reason: 'no_span_overlap', + primaryIsin: null, + }, + ]); + expect(toleranceResult.providerMappings.filter(m => m.source === 'qm')).toEqual([ + { + source: 'qm', + sourceId: 'QM_FB_2025_PRIMARY_TOLERANCE', + listingKey: { + primaryIsin: 'US0000000026', + mic: 'XNAS', + tickerRoot: 'FB', + classSuffix: '', + currency: 'USD', + }, + method: 'tier3_ticker_span', + validFrom: '2025-01-01', + validTo: '2025-01-01', + }, + ]); + assertReconciliationAndContracts(toleranceResult); + + // 3. Undated META.US (span: null): historical FB intersecting the alias window hits missing_span, + // while 2025 FB reuse (after rename date) resolves cleanly to 2025 FB.US. + const undatedMetaResult = matchListingsStageB({ + instruments: instruments.slice(0, 2), + eodListings: [ + { + sourceId: 'META.US', + ticker: 'META', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: null, + }, + { + sourceId: 'FB.US', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2025-01-02', lastBarDate: '2025-11-26' }, + }, + ], + ibContracts: [], + tickerSources: [ + { + source: 'qm', + sourceId: 'QM_FB_2015_UNDATED_META', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2015-01-02', lastBarDate: '2015-12-31' }, + }, + { + source: 'qm', + sourceId: 'QM_FB_2023_GAP_UNDATED_META', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2023-01-03', lastBarDate: '2023-12-29' }, + }, + { + source: 'qm', + sourceId: 'QM_FB_2025_REUSE_UNDATED_META', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2025-02-03', lastBarDate: '2025-10-31' }, + }, + { + source: 'qm', + sourceId: 'QM_FB_CROSS_UNDATED_META', + ticker: 'FB', + mic: 'XNAS', + role: 'primary_venue', + currency: 'USD', + span: { firstBarDate: '2022-06-08', lastBarDate: '2025-06-01' }, + }, + ], + symbolChanges: [{ eodSourceId: 'META.US', oldTicker: 'FB', effectiveDate: '2022-06-09' }], + }); + + expect(undatedMetaResult.conflicts).toEqual([]); + expect(undatedMetaResult.providerMappings.filter(m => m.source === 'qm')).toEqual([ + { + source: 'qm', + sourceId: 'QM_FB_2025_REUSE_UNDATED_META', + listingKey: { + primaryIsin: 'US0000000026', + mic: 'XNAS', + tickerRoot: 'FB', + classSuffix: '', + currency: 'USD', + }, + method: 'tier3_ticker_span', + validFrom: '2025-02-03', + validTo: '2025-10-31', + }, + ]); + expect(undatedMetaResult.unresolved).toEqual([ + { + source: 'qm', + sourceId: 'QM_FB_2015_UNDATED_META', + reason: 'missing_span', + primaryIsin: null, + }, + { + source: 'qm', + sourceId: 'QM_FB_2023_GAP_UNDATED_META', + reason: 'no_span_overlap', + primaryIsin: null, + }, + { + source: 'qm', + sourceId: 'QM_FB_CROSS_UNDATED_META', + reason: 'missing_span', + primaryIsin: null, + }, + ]); + assertReconciliationAndContracts(undatedMetaResult); + }); + test('local scale benchmark: ~20k instruments, ~60k EOD, ~18k IB, ~40k QM/CEO complete under 5000 ms', () => { const NUM_INSTRUMENTS = 20_000; const instruments: StageAInstrumentCandidate[] = new Array(