T-68: Bug sweep (5 bugs: T-58, T-59, T-61, T-62, T-63)
This commit is contained in:
parent
e440d8d5a0
commit
1692aba2fc
5 changed files with 800 additions and 30 deletions
|
|
@ -103,6 +103,7 @@ export {
|
|||
type StageBSource,
|
||||
type StageBSourceStats,
|
||||
type StageBStats,
|
||||
type StageBSymbolChangeInput,
|
||||
type StageBTickerSource,
|
||||
type StageBTickerSourceInput,
|
||||
type StageBUnresolvedReason,
|
||||
|
|
|
|||
|
|
@ -56,6 +56,13 @@ export interface StageBTickerSourceInput {
|
|||
span: SpanDateRange | null;
|
||||
}
|
||||
|
||||
export interface StageBSymbolChangeInput {
|
||||
readonly eodSourceId: string;
|
||||
readonly oldTicker: string;
|
||||
readonly effectiveDate: string;
|
||||
readonly validFrom?: string | null;
|
||||
}
|
||||
|
||||
export interface StageBInput {
|
||||
readonly instruments: readonly StageAInstrumentCandidate[];
|
||||
readonly eodListings:
|
||||
|
|
@ -65,6 +72,7 @@ export interface StageBInput {
|
|||
| readonly StageBIbContractInput[]
|
||||
| ReadonlyMap<string, Omit<StageBIbContractInput, 'sourceId'>>;
|
||||
readonly tickerSources: readonly StageBTickerSourceInput[];
|
||||
readonly symbolChanges?: readonly StageBSymbolChangeInput[];
|
||||
}
|
||||
|
||||
export interface MatchListingsStageBOptions {
|
||||
|
|
@ -81,7 +89,7 @@ export interface StageBListingCandidate extends ListingNaturalKey {
|
|||
export interface StageBProviderMappingCandidate {
|
||||
source: StageBSource;
|
||||
sourceId: string;
|
||||
listingKey: ListingNaturalKey;
|
||||
listingKey: Readonly<ListingNaturalKey>;
|
||||
method: StageBMatchMethod;
|
||||
validFrom: string | null;
|
||||
validTo: string | null;
|
||||
|
|
@ -125,7 +133,7 @@ export interface StageBConflictDetail {
|
|||
currency: string | null;
|
||||
ibConid: number | null;
|
||||
competingConids: number[];
|
||||
candidateListingKeys: ListingNaturalKey[];
|
||||
candidateListingKeys: Readonly<ListingNaturalKey>[];
|
||||
}
|
||||
|
||||
export interface StageBConflictRecord {
|
||||
|
|
@ -162,13 +170,26 @@ type EodMetadata = Omit<StageBEodListingInput, 'sourceId'>;
|
|||
type IbMetadata = Omit<StageBIbContractInput, 'sourceId'>;
|
||||
|
||||
interface ListingAccumulator {
|
||||
readonly key: ListingNaturalKey;
|
||||
readonly key: Readonly<ListingNaturalKey>;
|
||||
listedFrom: string | null;
|
||||
listedTo: string | null;
|
||||
ibConid: number | null;
|
||||
ibPrimaryExchange: string | null;
|
||||
}
|
||||
|
||||
interface TickerLookupCandidate {
|
||||
readonly acc: ListingAccumulator;
|
||||
readonly aliasSpan: SpanDateRange | null;
|
||||
}
|
||||
|
||||
interface ValidatedSymbolChange {
|
||||
readonly eodSourceId: string;
|
||||
readonly oldTickerRoot: string;
|
||||
readonly oldClassSuffix: string;
|
||||
readonly dayBeforeEffective: string;
|
||||
readonly validFrom: string | null;
|
||||
}
|
||||
|
||||
interface StagedIbCandidate {
|
||||
sourceId: string;
|
||||
primaryIsin: string;
|
||||
|
|
@ -328,7 +349,63 @@ function validateSpan(
|
|||
return { status: 'valid', span: { firstBarDate, lastBarDate } };
|
||||
}
|
||||
|
||||
function makeListingNaturalKeyId(key: ListingNaturalKey): string {
|
||||
function isLeapYear(year: number): boolean {
|
||||
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
|
||||
}
|
||||
|
||||
function daysInMonth(year: number, month: number): number {
|
||||
switch (month) {
|
||||
case 1:
|
||||
case 3:
|
||||
case 5:
|
||||
case 7:
|
||||
case 8:
|
||||
case 10:
|
||||
case 12:
|
||||
return 31;
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
return 30;
|
||||
case 2:
|
||||
return isLeapYear(year) ? 29 : 28;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure integer UTC calendar predecessor for exact `YYYY-MM-DD` strings.
|
||||
* Returns `null` for malformed dates or dates `<= '0001-01-01'`.
|
||||
*/
|
||||
function previousCalendarDate(ymd: string): string | null {
|
||||
if (!EXACT_YMD_PATTERN.test(ymd) || normalizeBarDate(ymd) !== ymd) {
|
||||
return null;
|
||||
}
|
||||
let year = Number(ymd.slice(0, 4));
|
||||
let month = Number(ymd.slice(5, 7));
|
||||
let day = Number(ymd.slice(8, 10));
|
||||
if (year < 1 || (year === 1 && month === 1 && day === 1)) {
|
||||
return null;
|
||||
}
|
||||
if (day > 1) {
|
||||
day -= 1;
|
||||
} else if (month > 1) {
|
||||
month -= 1;
|
||||
day = daysInMonth(year, month);
|
||||
} else {
|
||||
year -= 1;
|
||||
month = 12;
|
||||
day = 31;
|
||||
}
|
||||
const y = String(year).padStart(4, '0');
|
||||
const m = month < 10 ? `0${month}` : String(month);
|
||||
const d = day < 10 ? `0${day}` : String(day);
|
||||
return `${y}-${m}-${d}`;
|
||||
}
|
||||
|
||||
function makeListingNaturalKeyId(key: Readonly<ListingNaturalKey>): string {
|
||||
return `${key.primaryIsin}\u0000${key.mic}\u0000${key.tickerRoot}\u0000${key.classSuffix}\u0000${key.currency}`;
|
||||
}
|
||||
|
||||
|
|
@ -341,7 +418,10 @@ function makeTickerLookupKey(
|
|||
return `${mic}\u0000${currency}\u0000${tickerRoot}\u0000${classSuffix}`;
|
||||
}
|
||||
|
||||
function compareListingNaturalKey(a: ListingNaturalKey, b: ListingNaturalKey): number {
|
||||
function compareListingNaturalKey(
|
||||
a: Readonly<ListingNaturalKey>,
|
||||
b: Readonly<ListingNaturalKey>
|
||||
): number {
|
||||
return (
|
||||
compareAscii(a.primaryIsin, b.primaryIsin) ||
|
||||
compareAscii(a.mic, b.mic) ||
|
||||
|
|
@ -377,8 +457,8 @@ function compareNumberArrays(a: readonly number[], b: readonly number[]): number
|
|||
}
|
||||
|
||||
function compareListingKeyArrays(
|
||||
a: readonly ListingNaturalKey[],
|
||||
b: readonly ListingNaturalKey[]
|
||||
a: readonly Readonly<ListingNaturalKey>[],
|
||||
b: readonly Readonly<ListingNaturalKey>[]
|
||||
): number {
|
||||
const len = a.length < b.length ? a.length : b.length;
|
||||
for (let i = 0; i < len; i++) {
|
||||
|
|
@ -661,6 +741,69 @@ export function matchListingsStageB(
|
|||
counts.set(entry.sourceId, (counts.get(entry.sourceId) ?? 0) + 1);
|
||||
}
|
||||
|
||||
// Validate optional symbolChanges (historical EOD ticker aliases).
|
||||
const validatedSymbolChanges: ValidatedSymbolChange[] = [];
|
||||
if (input.symbolChanges !== undefined) {
|
||||
assertIsArray(input.symbolChanges, 'input.symbolChanges');
|
||||
for (let i = 0; i < input.symbolChanges.length; i++) {
|
||||
if (!(i in input.symbolChanges)) {
|
||||
throw new TypeError(`Invalid sparse slot at symbolChanges[${i}]: expected an object`);
|
||||
}
|
||||
const entry = input.symbolChanges[i];
|
||||
if (entry === undefined || entry === null || typeof entry !== 'object') {
|
||||
throw new TypeError(`Invalid symbolChanges entry at index ${i}: expected an object`);
|
||||
}
|
||||
if (typeof entry.eodSourceId !== 'string' || entry.eodSourceId.length === 0) {
|
||||
throw new Error(`Invalid empty or non-string eodSourceId at symbolChanges[${i}]`);
|
||||
}
|
||||
if (typeof entry.oldTicker !== 'string') {
|
||||
throw new TypeError(`Invalid non-string oldTicker at symbolChanges[${i}]`);
|
||||
}
|
||||
const parsedOldTicker = classifyTicker(entry.oldTicker, 'eod');
|
||||
if (!parsedOldTicker.ok) {
|
||||
throw new Error(
|
||||
`Invalid oldTicker "${entry.oldTicker}" (${parsedOldTicker.reason}) at symbolChanges[${i}]`
|
||||
);
|
||||
}
|
||||
if (typeof entry.effectiveDate !== 'string') {
|
||||
throw new TypeError(`Invalid non-string effectiveDate at symbolChanges[${i}]`);
|
||||
}
|
||||
const dayBeforeEffective = previousCalendarDate(entry.effectiveDate);
|
||||
if (dayBeforeEffective === null) {
|
||||
throw new RangeError(
|
||||
`Invalid effectiveDate "${entry.effectiveDate}" at symbolChanges[${i}]: expected exact valid YYYY-MM-DD > 0001-01-01`
|
||||
);
|
||||
}
|
||||
let validFrom: string | null = null;
|
||||
if (entry.validFrom !== undefined && entry.validFrom !== null) {
|
||||
if (typeof entry.validFrom !== 'string') {
|
||||
throw new TypeError(`Invalid non-string validFrom at symbolChanges[${i}]`);
|
||||
}
|
||||
if (
|
||||
!EXACT_YMD_PATTERN.test(entry.validFrom) ||
|
||||
normalizeBarDate(entry.validFrom) !== entry.validFrom
|
||||
) {
|
||||
throw new RangeError(
|
||||
`Invalid validFrom "${entry.validFrom}" at symbolChanges[${i}]: expected exact valid YYYY-MM-DD`
|
||||
);
|
||||
}
|
||||
if (entry.validFrom >= entry.effectiveDate) {
|
||||
throw new RangeError(
|
||||
`Invalid validFrom "${entry.validFrom}" at symbolChanges[${i}]: must be strictly before effectiveDate "${entry.effectiveDate}"`
|
||||
);
|
||||
}
|
||||
validFrom = entry.validFrom;
|
||||
}
|
||||
validatedSymbolChanges.push({
|
||||
eodSourceId: entry.eodSourceId,
|
||||
oldTickerRoot: parsedOldTicker.tickerRoot,
|
||||
oldClassSuffix: parsedOldTicker.classSuffix,
|
||||
dayBeforeEffective,
|
||||
validFrom,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const statsBySource: Record<StageBSource, StageBSourceStats> = {
|
||||
eod: createEmptySourceStats(),
|
||||
ib: createEmptySourceStats(),
|
||||
|
|
@ -671,7 +814,8 @@ export function matchListingsStageB(
|
|||
|
||||
const listingsByNaturalKey = new Map<string, ListingAccumulator>();
|
||||
const listingsByInstrumentCurrency = new Map<string, Map<string, ListingAccumulator[]>>();
|
||||
const listingsByTickerKey = new Map<string, ListingAccumulator[]>();
|
||||
const listingsByTickerKey = new Map<string, TickerLookupCandidate[]>();
|
||||
const listingByEodSourceId = new Map<string, ListingAccumulator>();
|
||||
|
||||
const providerMappings: StageBProviderMappingCandidate[] = [];
|
||||
const unresolved: StageBUnresolvedRecord[] = [];
|
||||
|
|
@ -769,13 +913,13 @@ export function matchListingsStageB(
|
|||
}
|
||||
const validSpan = spanCheck.status === 'valid' ? spanCheck.span : null;
|
||||
|
||||
const key: ListingNaturalKey = {
|
||||
const key: Readonly<ListingNaturalKey> = Object.freeze<ListingNaturalKey>({
|
||||
primaryIsin: inst.primaryIsin,
|
||||
mic: venue.mic,
|
||||
tickerRoot: ticker.tickerRoot,
|
||||
classSuffix: ticker.classSuffix,
|
||||
currency,
|
||||
};
|
||||
});
|
||||
const keyId = makeListingNaturalKeyId(key);
|
||||
let acc = listingsByNaturalKey.get(keyId);
|
||||
if (acc === undefined) {
|
||||
|
|
@ -811,7 +955,7 @@ export function matchListingsStageB(
|
|||
tickerList = [];
|
||||
listingsByTickerKey.set(tickerKey, tickerList);
|
||||
}
|
||||
tickerList.push(acc);
|
||||
tickerList.push({ acc, aliasSpan: null });
|
||||
} else if (validSpan !== null) {
|
||||
// Associative & commutative min/max widening ensures shuffle invariance.
|
||||
if (acc.listedFrom === null || validSpan.firstBarDate < acc.listedFrom) {
|
||||
|
|
@ -822,6 +966,8 @@ export function matchListingsStageB(
|
|||
}
|
||||
}
|
||||
|
||||
listingByEodSourceId.set(member.sourceId, acc);
|
||||
|
||||
providerMappings.push({
|
||||
source: 'eod',
|
||||
sourceId: member.sourceId,
|
||||
|
|
@ -834,6 +980,49 @@ export function matchListingsStageB(
|
|||
}
|
||||
}
|
||||
|
||||
// Register historical EOD symbol-change aliases after Step 1 so listedFrom/listedTo widening is finalized.
|
||||
for (let i = 0; i < validatedSymbolChanges.length; i++) {
|
||||
const change = validatedSymbolChanges[i];
|
||||
if (change === undefined) {
|
||||
continue;
|
||||
}
|
||||
const acc = listingByEodSourceId.get(change.eodSourceId);
|
||||
if (acc === undefined) {
|
||||
continue;
|
||||
}
|
||||
const aliasTickerKey = makeTickerLookupKey(
|
||||
acc.key.mic,
|
||||
acc.key.currency,
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [sourceId, count] of eodMetadataCounts.entries()) {
|
||||
if (count > 1 && (memberCountsBySource.eod.get(sourceId) ?? 0) === 0) {
|
||||
conflicts.push({
|
||||
|
|
@ -1119,29 +1308,34 @@ export function matchListingsStageB(
|
|||
continue;
|
||||
}
|
||||
|
||||
const overlapping: ListingAccumulator[] = [];
|
||||
let missingSpanCount = 0;
|
||||
const overlappingSet = new Set<ListingAccumulator>();
|
||||
const missingSpanSet = new Set<ListingAccumulator>();
|
||||
|
||||
for (let k = 0; k < candidates.length; k++) {
|
||||
const cand = candidates[k];
|
||||
if (cand === undefined) {
|
||||
continue;
|
||||
}
|
||||
if (cand.listedFrom === null || cand.listedTo === null) {
|
||||
missingSpanCount += 1;
|
||||
} else if (
|
||||
spansOverlap(
|
||||
spanCheck.span,
|
||||
{ firstBarDate: cand.listedFrom, lastBarDate: cand.listedTo },
|
||||
overlapOptions
|
||||
)
|
||||
) {
|
||||
overlapping.push(cand);
|
||||
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 (candSpan === null) {
|
||||
missingSpanSet.add(cand.acc);
|
||||
} else if (spansOverlap(spanCheck.span, candSpan, overlapOptions)) {
|
||||
overlappingSet.add(cand.acc);
|
||||
}
|
||||
}
|
||||
|
||||
if (overlapping.length > 1) {
|
||||
const candidateListingKeys = overlapping.map(c => c.key).sort(compareListingNaturalKey);
|
||||
if (overlappingSet.size > 1) {
|
||||
const candidateListingKeys = Array.from(overlappingSet, c => c.key).sort(
|
||||
compareListingNaturalKey
|
||||
);
|
||||
conflicts.push({
|
||||
source,
|
||||
sourceId: record.sourceId,
|
||||
|
|
@ -1155,7 +1349,7 @@ export function matchListingsStageB(
|
|||
},
|
||||
});
|
||||
sourceStats.conflict += 1;
|
||||
} else if (missingSpanCount > 0) {
|
||||
} 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,
|
||||
|
|
@ -1164,8 +1358,8 @@ export function matchListingsStageB(
|
|||
primaryIsin: null,
|
||||
});
|
||||
sourceStats.unresolved += 1;
|
||||
} else if (overlapping.length === 1) {
|
||||
const matchedListing = overlapping[0];
|
||||
} else if (overlappingSet.size === 1) {
|
||||
const matchedListing = overlappingSet.values().next().value;
|
||||
if (matchedListing !== undefined) {
|
||||
providerMappings.push({
|
||||
source,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue