T-73: M1.1h — Pass 4: pure in-memory cross-sectional price cross-check (25 stratified sample dates, price ratio ≈ 1.0 or ≈ USD/CAD, return correlation)
This commit is contained in:
parent
8f76d99046
commit
db284d7760
3 changed files with 169 additions and 6 deletions
|
|
@ -99,6 +99,7 @@ export interface PriceCrossCheckVerdictCounts {
|
|||
|
||||
export interface PriceCrossCheckStats {
|
||||
pairsIn: number;
|
||||
invalidPairs: number;
|
||||
byVerdict: PriceCrossCheckVerdictCounts;
|
||||
conflictsEmitted: number;
|
||||
droppedInvalidCloses: number;
|
||||
|
|
@ -197,6 +198,25 @@ function cloneEndpoint(endpoint: PriceSeriesEndpoint): PriceSeriesEndpoint {
|
|||
};
|
||||
}
|
||||
|
||||
function sanitizeEndpoint(raw: unknown): { endpoint: PriceSeriesEndpoint; valid: boolean } {
|
||||
if (raw === null || typeof raw !== 'object') {
|
||||
return {
|
||||
endpoint: { source: '', sourceId: '', currency: '' },
|
||||
valid: false,
|
||||
};
|
||||
}
|
||||
const candidate = raw as Record<string, unknown>;
|
||||
const source = typeof candidate.source === 'string' ? candidate.source : '';
|
||||
const sourceId = typeof candidate.sourceId === 'string' ? candidate.sourceId : '';
|
||||
const currency = typeof candidate.currency === 'string' ? candidate.currency : '';
|
||||
const valid =
|
||||
source.length > 0 && sourceId.length > 0 && typeof candidate.currency === 'string';
|
||||
return {
|
||||
endpoint: { source, sourceId, currency },
|
||||
valid,
|
||||
};
|
||||
}
|
||||
|
||||
function compareEndpoint(a: PriceSeriesEndpoint, b: PriceSeriesEndpoint): number {
|
||||
return (
|
||||
compareAscii(a.source, b.source) ||
|
||||
|
|
@ -499,20 +519,53 @@ export function crossCheckPrices(
|
|||
low_correlation: 0,
|
||||
confirmed: 0,
|
||||
};
|
||||
const minAllowedRatio = 1 - ratioTolerance;
|
||||
const maxAllowedRatio = 1 + ratioTolerance;
|
||||
let invalidPairs = 0;
|
||||
let skippedMissingFxDates = 0;
|
||||
|
||||
const results: PriceCrossCheckRow[] = [];
|
||||
const conflicts: PriceCrossCheckConflictRecord[] = [];
|
||||
|
||||
for (let i = 0; i < input.pairs.length; i++) {
|
||||
const pair = input.pairs[i];
|
||||
if (pair === undefined) {
|
||||
const pair = input.pairs[i] as unknown;
|
||||
if (pair === null || typeof pair !== 'object') {
|
||||
invalidPairs += 1;
|
||||
byVerdict.unsupported_currency_pair += 1;
|
||||
results.push({
|
||||
left: { source: '', sourceId: '', currency: '' },
|
||||
right: { source: '', sourceId: '', currency: '' },
|
||||
verdict: 'unsupported_currency_pair',
|
||||
usableDates: 0,
|
||||
medianRatio: null,
|
||||
priceCorr: null,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
const left = cloneEndpoint(pair.left);
|
||||
const right = cloneEndpoint(pair.right);
|
||||
|
||||
const pairRecord = pair as Record<string, unknown>;
|
||||
const leftSanitized = sanitizeEndpoint(pairRecord.left);
|
||||
const rightSanitized = sanitizeEndpoint(pairRecord.right);
|
||||
const left = leftSanitized.endpoint;
|
||||
const right = rightSanitized.endpoint;
|
||||
const mode = classifyCurrencyPair(left.currency, right.currency);
|
||||
|
||||
if (!leftSanitized.valid || !rightSanitized.valid) {
|
||||
invalidPairs += 1;
|
||||
const malformedVerdict: PriceCrossCheckVerdict =
|
||||
mode === 'unsupported' ? 'unsupported_currency_pair' : 'insufficient_data';
|
||||
byVerdict[malformedVerdict] += 1;
|
||||
results.push({
|
||||
left,
|
||||
right,
|
||||
verdict: malformedVerdict,
|
||||
usableDates: 0,
|
||||
medianRatio: null,
|
||||
priceCorr: null,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mode === 'unsupported') {
|
||||
byVerdict.unsupported_currency_pair += 1;
|
||||
results.push({
|
||||
|
|
@ -584,7 +637,7 @@ export function crossCheckPrices(
|
|||
!Number.isFinite(medianRatio)
|
||||
) {
|
||||
verdict = 'insufficient_data';
|
||||
} else if (Math.abs(medianRatio - 1) > ratioTolerance) {
|
||||
} else if (medianRatio < minAllowedRatio || medianRatio > maxAllowedRatio) {
|
||||
verdict = 'ratio_mismatch';
|
||||
} else if (priceCorr < minCorrelation) {
|
||||
verdict = 'low_correlation';
|
||||
|
|
@ -635,6 +688,7 @@ export function crossCheckPrices(
|
|||
conflicts,
|
||||
stats: {
|
||||
pairsIn: input.pairs.length,
|
||||
invalidPairs,
|
||||
byVerdict,
|
||||
conflictsEmitted: conflicts.length,
|
||||
droppedInvalidCloses,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue