T-68: Bug sweep (5 bugs: T-58, T-59, T-61, T-62, T-63)
This commit is contained in:
parent
dea894695a
commit
99ed9bc5a9
2 changed files with 365 additions and 27 deletions
|
|
@ -410,6 +410,51 @@ function previousCalendarDate(ymd: string): string | null {
|
|||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
|
||||
function compareSpanDateRange(a: SpanDateRange, b: SpanDateRange): number {
|
||||
return compareAscii(a.firstBarDate, b.firstBarDate) || compareAscii(a.lastBarDate, b.lastBarDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges matched date spans for a single listing when all spans are overlapping or
|
||||
* calendar-adjacent. Returns `null` if any gap of at least one calendar day separates
|
||||
* matched spans, so Stage B never bridges dates where the listing used another ticker.
|
||||
*/
|
||||
function mergeContiguousSpans(spans: readonly SpanDateRange[]): SpanDateRange | null {
|
||||
if (spans.length === 0) {
|
||||
return null;
|
||||
}
|
||||
const first = spans[0];
|
||||
if (first === undefined) {
|
||||
return null;
|
||||
}
|
||||
if (spans.length === 1) {
|
||||
return first;
|
||||
}
|
||||
const sorted = spans.slice().sort(compareSpanDateRange);
|
||||
const head = sorted[0];
|
||||
if (head === undefined) {
|
||||
return null;
|
||||
}
|
||||
const mergedFrom = head.firstBarDate;
|
||||
let mergedTo = head.lastBarDate;
|
||||
for (let i = 1; i < sorted.length; i++) {
|
||||
const next = sorted[i];
|
||||
if (next === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (next.firstBarDate > mergedTo) {
|
||||
const prevDay = previousCalendarDate(next.firstBarDate);
|
||||
if (prevDay === null || prevDay > mergedTo) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (next.lastBarDate > mergedTo) {
|
||||
mergedTo = next.lastBarDate;
|
||||
}
|
||||
}
|
||||
return { firstBarDate: mergedFrom, lastBarDate: mergedTo };
|
||||
}
|
||||
|
||||
function makeListingNaturalKeyId(key: Readonly<ListingNaturalKey>): string {
|
||||
return `${key.primaryIsin}\u0000${key.mic}\u0000${key.tickerRoot}\u0000${key.classSuffix}\u0000${key.currency}`;
|
||||
}
|
||||
|
|
@ -1300,9 +1345,18 @@ export function matchListingsStageB(
|
|||
continue;
|
||||
}
|
||||
|
||||
const matchedSpanByAcc = new Map<ListingAccumulator, SpanDateRange>();
|
||||
const matchedSpansByAcc = new Map<ListingAccumulator, SpanDateRange[]>();
|
||||
const missingSpanSet = new Set<ListingAccumulator>();
|
||||
|
||||
const recordMatchedSpan = (acc: ListingAccumulator, matchedSpan: SpanDateRange): void => {
|
||||
const existing = matchedSpansByAcc.get(acc);
|
||||
if (existing === undefined) {
|
||||
matchedSpansByAcc.set(acc, [matchedSpan]);
|
||||
} else {
|
||||
existing.push(matchedSpan);
|
||||
}
|
||||
};
|
||||
|
||||
for (let k = 0; k < candidates.length; k++) {
|
||||
const cand = candidates[k];
|
||||
if (cand === undefined) {
|
||||
|
|
@ -1318,7 +1372,7 @@ export function matchListingsStageB(
|
|||
lastBarDate: cand.acc.listedTo,
|
||||
};
|
||||
if (spansOverlap(spanCheck.span, listingSpan, overlapOptions)) {
|
||||
matchedSpanByAcc.set(cand.acc, spanCheck.span);
|
||||
recordMatchedSpan(cand.acc, spanCheck.span);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
|
|
@ -1358,20 +1412,12 @@ export function matchListingsStageB(
|
|||
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,
|
||||
});
|
||||
}
|
||||
recordMatchedSpan(cand.acc, clampedProviderSpan);
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedSpanByAcc.size > 1) {
|
||||
const candidateListingKeys = Array.from(matchedSpanByAcc.keys(), c => c.key).sort(
|
||||
if (matchedSpansByAcc.size > 1) {
|
||||
const candidateListingKeys = Array.from(matchedSpansByAcc.keys(), c => c.key).sort(
|
||||
compareListingNaturalKey
|
||||
);
|
||||
conflicts.push({
|
||||
|
|
@ -1387,6 +1433,47 @@ export function matchListingsStageB(
|
|||
},
|
||||
});
|
||||
sourceStats.conflict += 1;
|
||||
} else if (matchedSpansByAcc.size === 1) {
|
||||
const matchedEntry = matchedSpansByAcc.entries().next().value;
|
||||
const mergedSpan =
|
||||
matchedEntry !== undefined ? mergeContiguousSpans(matchedEntry[1]) : null;
|
||||
if (matchedEntry !== undefined && mergedSpan === null) {
|
||||
// Matched disjoint alias windows for one listing; a single mapping cannot represent both
|
||||
// without falsely claiming intervening dates under another ticker.
|
||||
conflicts.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
conflictKind: 'ambiguous_ticker_span',
|
||||
detail: {
|
||||
primaryIsin: null,
|
||||
currency,
|
||||
ibConid: null,
|
||||
competingConids: [],
|
||||
candidateListingKeys: [matchedEntry[0].key],
|
||||
},
|
||||
});
|
||||
sourceStats.conflict += 1;
|
||||
} else if (missingSpanSet.size > 0) {
|
||||
// Never match when a same-key listing has an unknown span (could hide a reused-ticker collision).
|
||||
unresolved.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
reason: 'missing_span',
|
||||
primaryIsin: null,
|
||||
});
|
||||
sourceStats.unresolved += 1;
|
||||
} else if (matchedEntry !== undefined && mergedSpan !== null) {
|
||||
const [matchedListing] = matchedEntry;
|
||||
providerMappings.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
listingKey: matchedListing.key,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: mergedSpan.firstBarDate,
|
||||
validTo: mergedSpan.lastBarDate,
|
||||
});
|
||||
sourceStats.matched += 1;
|
||||
}
|
||||
} else if (missingSpanSet.size > 0) {
|
||||
// Never match when a same-key listing has an unknown span (could hide a reused-ticker collision).
|
||||
unresolved.push({
|
||||
|
|
@ -1396,20 +1483,6 @@ export function matchListingsStageB(
|
|||
primaryIsin: null,
|
||||
});
|
||||
sourceStats.unresolved += 1;
|
||||
} 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: matchedSpan.firstBarDate,
|
||||
validTo: matchedSpan.lastBarDate,
|
||||
});
|
||||
sourceStats.matched += 1;
|
||||
}
|
||||
} else {
|
||||
unresolved.push({
|
||||
source,
|
||||
|
|
|
|||
|
|
@ -2237,6 +2237,271 @@ describe('security master Pass 3 Stage B listing matcher', () => {
|
|||
assertReconciliationAndContracts(undatedMetaResult);
|
||||
});
|
||||
|
||||
test('quarantines disjoint returning-ticker alias windows while merging adjacent or overlapping windows (T-59)', () => {
|
||||
const instruments: StageAInstrumentCandidate[] = [
|
||||
{
|
||||
primaryIsin: 'US7777777777',
|
||||
primaryCusip: null,
|
||||
issuerCountry: 'US',
|
||||
canonicalName: 'Returning Ticker Corp',
|
||||
members: [{ source: 'eod', sourceId: 'CUR.US', currency: 'USD', method: 'tier1_isin' }],
|
||||
},
|
||||
];
|
||||
|
||||
const eodListings: StageBEodListingInput[] = [
|
||||
{
|
||||
sourceId: 'CUR.US',
|
||||
ticker: 'CUR',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2010-01-04', lastBarDate: '2025-11-26' },
|
||||
},
|
||||
];
|
||||
|
||||
// Listing used OLD through 2015, changed to MID for 2016-2019, returned to OLD for 2020-2021,
|
||||
// then changed to CUR on 2022-01-01. Also includes adjacent, overlapping, and 1-day-gap aliases.
|
||||
const symbolChanges: StageBSymbolChangeInput[] = [
|
||||
{ eodSourceId: 'CUR.US', oldTicker: 'OLD', effectiveDate: '2016-01-01' },
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'MID',
|
||||
validFrom: '2016-01-01',
|
||||
effectiveDate: '2020-01-01',
|
||||
},
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'OLD',
|
||||
validFrom: '2020-01-01',
|
||||
effectiveDate: '2022-01-01',
|
||||
},
|
||||
// Adjacent windows: 2014-01-01..2015-12-31 and 2016-01-01..2017-12-31
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'ADJ',
|
||||
validFrom: '2014-01-01',
|
||||
effectiveDate: '2016-01-01',
|
||||
},
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'ADJ',
|
||||
validFrom: '2016-01-01',
|
||||
effectiveDate: '2018-01-01',
|
||||
},
|
||||
// Overlapping windows: 2014-01-01..2016-06-30 and 2016-01-01..2017-12-31
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'OVL',
|
||||
validFrom: '2014-01-01',
|
||||
effectiveDate: '2016-07-01',
|
||||
},
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'OVL',
|
||||
validFrom: '2016-01-01',
|
||||
effectiveDate: '2018-01-01',
|
||||
},
|
||||
// 1-day gap: 2014-01-01..2015-12-31 and 2016-01-02..2017-12-31 (2016-01-01 missing)
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'GAP1',
|
||||
validFrom: '2014-01-01',
|
||||
effectiveDate: '2016-01-01',
|
||||
},
|
||||
{
|
||||
eodSourceId: 'CUR.US',
|
||||
oldTicker: 'GAP1',
|
||||
validFrom: '2016-01-02',
|
||||
effectiveDate: '2018-01-01',
|
||||
},
|
||||
];
|
||||
|
||||
const tickerSources: StageBTickerSourceInput[] = [
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_SPANS_BOTH_WINDOWS',
|
||||
ticker: 'OLD',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2015-01-02', lastBarDate: '2021-12-31' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_WINDOW1_ONLY',
|
||||
ticker: 'OLD',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2015-01-02', lastBarDate: '2015-12-31' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_WINDOW2_ONLY',
|
||||
ticker: 'OLD',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2020-01-02', lastBarDate: '2021-12-31' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_IN_MID_GAP',
|
||||
ticker: 'OLD',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2016-01-04', lastBarDate: '2019-12-31' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_MID_2016_2019',
|
||||
ticker: 'MID',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2016-01-04', lastBarDate: '2019-12-31' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_ADJ_MERGES',
|
||||
ticker: 'ADJ',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2015-06-01', lastBarDate: '2016-06-30' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OVL_MERGES',
|
||||
ticker: 'OVL',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2015-06-01', lastBarDate: '2016-09-30' },
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_GAP1_QUARANTINED',
|
||||
ticker: 'GAP1',
|
||||
mic: 'XNAS',
|
||||
role: 'primary_venue',
|
||||
currency: 'USD',
|
||||
span: { firstBarDate: '2015-06-01', lastBarDate: '2016-06-30' },
|
||||
},
|
||||
];
|
||||
|
||||
const baseline = matchListingsStageB({
|
||||
instruments,
|
||||
eodListings,
|
||||
ibContracts: [],
|
||||
tickerSources,
|
||||
symbolChanges,
|
||||
});
|
||||
|
||||
const curKey = {
|
||||
primaryIsin: 'US7777777777',
|
||||
mic: 'XNAS',
|
||||
tickerRoot: 'CUR',
|
||||
classSuffix: '',
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
expect(baseline.conflicts).toEqual([
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_GAP1_QUARANTINED',
|
||||
conflictKind: 'ambiguous_ticker_span',
|
||||
detail: {
|
||||
primaryIsin: null,
|
||||
currency: 'USD',
|
||||
ibConid: null,
|
||||
competingConids: [],
|
||||
candidateListingKeys: [curKey],
|
||||
},
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_SPANS_BOTH_WINDOWS',
|
||||
conflictKind: 'ambiguous_ticker_span',
|
||||
detail: {
|
||||
primaryIsin: null,
|
||||
currency: 'USD',
|
||||
ibConid: null,
|
||||
competingConids: [],
|
||||
candidateListingKeys: [curKey],
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
expect(baseline.unresolved).toEqual([
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_IN_MID_GAP',
|
||||
reason: 'no_span_overlap',
|
||||
primaryIsin: null,
|
||||
},
|
||||
]);
|
||||
|
||||
expect(baseline.providerMappings.filter(m => m.source === 'qm')).toEqual([
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_ADJ_MERGES',
|
||||
listingKey: curKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2015-06-01',
|
||||
validTo: '2016-06-30',
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_MID_2016_2019',
|
||||
listingKey: curKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2016-01-04',
|
||||
validTo: '2019-12-31',
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_WINDOW1_ONLY',
|
||||
listingKey: curKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2015-01-02',
|
||||
validTo: '2015-12-31',
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OLD_WINDOW2_ONLY',
|
||||
listingKey: curKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2020-01-02',
|
||||
validTo: '2021-12-31',
|
||||
},
|
||||
{
|
||||
source: 'qm',
|
||||
sourceId: 'QM_OVL_MERGES',
|
||||
listingKey: curKey,
|
||||
method: 'tier3_ticker_span',
|
||||
validFrom: '2015-06-01',
|
||||
validTo: '2016-09-30',
|
||||
},
|
||||
]);
|
||||
|
||||
assertReconciliationAndContracts(baseline);
|
||||
|
||||
// Shuffle invariance across symbolChanges and tickerSources.
|
||||
const baselineJson = JSON.stringify(baseline);
|
||||
for (const seed of [7, 42, 999]) {
|
||||
const shuffled = matchListingsStageB({
|
||||
instruments,
|
||||
eodListings,
|
||||
ibContracts: [],
|
||||
tickerSources: shuffleWithSeed(tickerSources, seed),
|
||||
symbolChanges: shuffleWithSeed(symbolChanges, seed + 1),
|
||||
});
|
||||
expect(JSON.stringify(shuffled)).toBe(baselineJson);
|
||||
}
|
||||
});
|
||||
|
||||
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<StageAInstrumentCandidate>(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue