diff --git a/apps/stock/data-pipeline/src/security-master/delisting-chains.ts b/apps/stock/data-pipeline/src/security-master/delisting-chains.ts index 8342334e..afcefa0b 100644 --- a/apps/stock/data-pipeline/src/security-master/delisting-chains.ts +++ b/apps/stock/data-pipeline/src/security-master/delisting-chains.ts @@ -576,6 +576,10 @@ export function linkDelistingChains( : `${group.missingFromOtcs.length} OTC listings have null listedFrom; cannot establish transfer window or effectiveDate`, }, }); + // Rule 5 ("never guess"): an undated OTC row in the same (primaryIsin, currency) group + // could be the true continuation, so suppress all continuity linking for this group + // until listedFrom is resolved. + continue; } if (group.eligibleOtcs.length === 0) { diff --git a/test/unit/security-master-delisting-chains.test.ts b/test/unit/security-master-delisting-chains.test.ts index 81bfb487..249f81c2 100644 --- a/test/unit/security-master-delisting-chains.test.ts +++ b/test/unit/security-master-delisting-chains.test.ts @@ -797,6 +797,342 @@ describe('security master Pass 6 OTC delisting chain linker', () => { assertReconciliationAndContracts(result, listings.length); }); + test('suppresses continuity linking when a (primaryIsin, currency) group contains an undated OTC listing (T-66)', () => { + const listings: StageBListingCandidate[] = [ + // Group 1 (ended undated OTC): 1 ended primary + 1 dated OTC in window + 1 undated ended OTC. + makeListing({ + primaryIsin: 'US4500000001', + mic: 'XNAS', + tickerRoot: 'MIXA', + listedFrom: '2018-01-02', + listedTo: '2023-03-10', + }), + makeListing({ + primaryIsin: 'US4500000001', + mic: 'OTCM', + tickerRoot: 'MIXAQ', + listedFrom: '2023-03-15', + listedTo: '2024-01-01', + }), + makeListing({ + primaryIsin: 'US4500000001', + mic: 'OTCM', + tickerRoot: 'MIXAU', + listedFrom: null, + listedTo: '2024-01-01', + }), + + // Group 2 (open-ended undated OTC): 1 ended primary + 1 dated OTC in window + 1 undated open-ended OTC. + makeListing({ + primaryIsin: 'US4500000002', + mic: 'XNYS', + tickerRoot: 'MIXB', + listedFrom: '2018-01-02', + listedTo: '2023-03-10', + }), + makeListing({ + primaryIsin: 'US4500000002', + mic: 'OTCM', + tickerRoot: 'MIXBQ', + listedFrom: '2023-03-15', + listedTo: '2024-01-01', + }), + makeListing({ + primaryIsin: 'US4500000002', + mic: 'OTCM', + tickerRoot: 'MIXBU', + listedFrom: null, + listedTo: null, + }), + ]; + + const result = linkDelistingChains( + { listings, otcMics: DEFAULT_OTC_MICS }, + { asOfDate: '2025-11-27' } + ); + + expect(result.continuities).toEqual([]); + expect(result.conflicts).toHaveLength(2); + for (const conflict of result.conflicts) { + expect(conflict.conflictKind).toBe('missing_otc_listed_from'); + expect(conflict.detail.listedFrom).toBeNull(); + } + expect(result.conflicts[0]?.detail).toMatchObject({ + primaryIsin: 'US4500000001', + currency: 'USD', + listedFrom: null, + listedTo: '2024-01-01', + }); + expect(result.conflicts[1]?.detail).toMatchObject({ + primaryIsin: 'US4500000002', + currency: 'USD', + listedFrom: null, + listedTo: null, + }); + + const byTicker = new Map(result.listingOutcomes.map(o => [o.listingKey.tickerRoot, o])); + // Ended primaries fall back conservatively to unknown_involuntary / -0.3 (never moved_to_otc). + expect(byTicker.get('MIXA')).toMatchObject({ + delistReason: 'unknown_involuntary', + terminalReturn: -0.3, + }); + expect(byTicker.get('MIXB')).toMatchObject({ + delistReason: 'unknown_involuntary', + terminalReturn: -0.3, + }); + // Dated and undated OTC rows receive normal Pass 3 classification. + expect(byTicker.get('MIXAQ')).toMatchObject({ + delistReason: 'bankruptcy_cancelled', + terminalReturn: -1.0, + }); + expect(byTicker.get('MIXAU')).toMatchObject({ + delistReason: 'unknown_involuntary', + terminalReturn: -0.3, + }); + expect(byTicker.get('MIXBQ')).toMatchObject({ + delistReason: 'bankruptcy_cancelled', + terminalReturn: -1.0, + }); + expect(byTicker.get('MIXBU')).toMatchObject({ + delistReason: 'active', + terminalReturn: null, + }); + assertReconciliationAndContracts(result, listings.length); + + // Point-in-time asOfDate invariant: within activeGraceDays of primary's listedTo, + // emit neither conflict nor continuity and keep active / null outcomes. + const graceWindowResult = linkDelistingChains( + { listings, otcMics: DEFAULT_OTC_MICS }, + { asOfDate: '2023-03-15' } + ); + expect(graceWindowResult.continuities).toEqual([]); + expect(graceWindowResult.conflicts).toEqual([]); + for (const outcome of graceWindowResult.listingOutcomes) { + expect(outcome.delistReason).toBe('active'); + expect(outcome.terminalReturn).toBeNull(); + } + assertReconciliationAndContracts(graceWindowResult, listings.length); + + // Shuffle determinism and unique cloned ListingNaturalKey references on mixed dated+undated groups. + const baselineJson = JSON.stringify(result); + const inputRefs = new Set(listings); + const seenKeyRefs = new Set(); + const registerKey = (key: ListingNaturalKey): void => { + expect(inputRefs.has(key)).toBe(false); + expect(seenKeyRefs.has(key)).toBe(false); + seenKeyRefs.add(key); + }; + for (const o of result.listingOutcomes) { + registerKey(o.listingKey); + } + for (const conflict of result.conflicts) { + for (const k of conflict.detail.fromListingKeys) { + registerKey(k); + } + for (const k of conflict.detail.toListingKeys) { + registerKey(k); + } + for (const k of conflict.detail.candidateListingKeys) { + registerKey(k); + } + } + for (const seed of [7, 42, 1337, 65535]) { + const shuffled = shuffleWithSeed(listings, seed); + const shuffledResult = linkDelistingChains( + { listings: shuffled, otcMics: DEFAULT_OTC_MICS }, + { asOfDate: '2025-11-27' } + ); + expect(JSON.stringify(shuffledResult)).toBe(baselineJson); + assertReconciliationAndContracts(shuffledResult, listings.length); + } + }); + + test('isolates T-66 suppression by (primaryIsin, currency) and suppresses multi-cycle / multi-candidate groups cleanly', () => { + const listings: StageBListingCandidate[] = [ + // Clean SIVB -> SIVBQ in (US78486Q1013, USD) must still link cleanly... + makeListing({ + primaryIsin: 'US78486Q1013', + mic: 'XNAS', + tickerRoot: 'SIVB', + currency: 'USD', + listedFrom: '1987-10-26', + listedTo: '2023-03-10', + }), + makeListing({ + primaryIsin: 'US78486Q1013', + mic: 'OTCM', + tickerRoot: 'SIVBQ', + currency: 'USD', + listedFrom: '2023-03-28', + listedTo: '2024-11-07', + }), + // ...even when the same ISIN has an undated OTC row in a different currency (CAD). + makeListing({ + primaryIsin: 'US78486Q1013', + mic: 'OTCM', + tickerRoot: 'SIVBC', + currency: 'CAD', + listedFrom: null, + listedTo: null, + }), + + // Separate suppressed USD group in the same batch. + makeListing({ + primaryIsin: 'US4500000010', + mic: 'XNAS', + tickerRoot: 'SUPP', + currency: 'USD', + listedFrom: '2018-01-02', + listedTo: '2023-03-10', + }), + makeListing({ + primaryIsin: 'US4500000010', + mic: 'OTCM', + tickerRoot: 'SUPPQ', + currency: 'USD', + listedFrom: '2023-03-15', + listedTo: '2024-01-01', + }), + makeListing({ + primaryIsin: 'US4500000010', + mic: 'OTCM', + tickerRoot: 'SUPPU', + currency: 'USD', + listedFrom: null, + listedTo: '2024-01-01', + }), + + // Two disjoint valid primary->OTC cycles in one (primaryIsin, currency) group plus 1 undated OTC -> both suppressed. + makeListing({ + primaryIsin: 'US4500000011', + mic: 'XNAS', + tickerRoot: 'DCY1', + currency: 'USD', + listedFrom: '2015-01-02', + listedTo: '2018-06-01', + }), + makeListing({ + primaryIsin: 'US4500000011', + mic: 'OTCM', + tickerRoot: 'DCYAQ', + currency: 'USD', + listedFrom: '2018-06-10', + listedTo: '2019-12-31', + }), + makeListing({ + primaryIsin: 'US4500000011', + mic: 'XNYS', + tickerRoot: 'DCY2', + currency: 'USD', + listedFrom: '2020-01-02', + listedTo: '2023-06-01', + }), + makeListing({ + primaryIsin: 'US4500000011', + mic: 'OTCM', + tickerRoot: 'DCYBQ', + currency: 'USD', + listedFrom: '2023-06-12', + listedTo: '2024-06-01', + }), + makeListing({ + primaryIsin: 'US4500000011', + mic: 'OTCM', + tickerRoot: 'DCYUN', + currency: 'USD', + listedFrom: null, + listedTo: null, + }), + + // 1 ended primary + 2 dated OTCs + 1 undated OTC -> emits only 1 missing_otc_listed_from (no spurious ambiguous_otc_continuation). + makeListing({ + primaryIsin: 'US4500000012', + mic: 'XNAS', + tickerRoot: 'AMBU', + currency: 'USD', + listedFrom: '2018-01-02', + listedTo: '2023-03-10', + }), + makeListing({ + primaryIsin: 'US4500000012', + mic: 'OTCM', + tickerRoot: 'AMBUQ', + currency: 'USD', + listedFrom: '2023-03-15', + listedTo: '2024-01-01', + }), + makeListing({ + primaryIsin: 'US4500000012', + mic: 'OTCM', + tickerRoot: 'AMBUA', + currency: 'USD', + listedFrom: '2023-03-20', + listedTo: '2024-01-01', + }), + makeListing({ + primaryIsin: 'US4500000012', + mic: 'OTCM', + tickerRoot: 'AMBUX', + currency: 'USD', + listedFrom: null, + listedTo: '2024-01-01', + }), + ]; + + const result = linkDelistingChains( + { listings, otcMics: DEFAULT_OTC_MICS }, + { asOfDate: '2025-11-27' } + ); + + // Only SIVB -> SIVBQ links. + expect(result.continuities).toHaveLength(1); + expect(result.continuities[0]).toMatchObject({ + fromListingKey: { + primaryIsin: 'US78486Q1013', + mic: 'XNAS', + tickerRoot: 'SIVB', + currency: 'USD', + }, + toListingKey: { + primaryIsin: 'US78486Q1013', + mic: 'OTCM', + tickerRoot: 'SIVBQ', + currency: 'USD', + }, + effectiveDate: '2023-03-28', + kind: 'otc_delist_transfer', + }); + + // Exactly 3 missing_otc_listed_from conflicts (one per suppressed group) and 0 ambiguous_otc_continuation conflicts. + expect(result.conflicts).toHaveLength(3); + expect(result.conflicts.map(c => [c.conflictKind, c.detail.primaryIsin])).toEqual([ + ['missing_otc_listed_from', 'US4500000010'], + ['missing_otc_listed_from', 'US4500000011'], + ['missing_otc_listed_from', 'US4500000012'], + ]); + + const byTicker = new Map(result.listingOutcomes.map(o => [o.listingKey.tickerRoot, o])); + expect(byTicker.get('SIVB')).toMatchObject({ + delistReason: 'moved_to_otc', + terminalReturn: null, + }); + expect(byTicker.get('SIVBQ')).toMatchObject({ + delistReason: 'bankruptcy_cancelled', + terminalReturn: -1.0, + }); + expect(byTicker.get('SIVBC')).toMatchObject({ + delistReason: 'active', + terminalReturn: null, + }); + for (const suppressedPrimary of ['SUPP', 'DCY1', 'DCY2', 'AMBU']) { + expect(byTicker.get(suppressedPrimary)).toMatchObject({ + delistReason: 'unknown_involuntary', + terminalReturn: -0.3, + }); + } + assertReconciliationAndContracts(result, listings.length); + }); + test('throws TypeError or RangeError on invalid options or container arguments', () => { const emptyInput = { listings: [], otcMics: DEFAULT_OTC_MICS }; const validOpts = { asOfDate: '2025-11-27' };