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
|
|
@ -61,5 +61,5 @@ MONGODB_READ_URI="mongodb://studio_stock_bot_read:<password>@localhost:27017/sto
|
|||
- **Stratified date sampling (`selectSampleDates`):** Drops invalid/non-`YYYY-MM-DD` values, deduplicates, sorts ascending, and selects `count` dates (default `25`) by splitting the calendar into `count` contiguous near-equal strata and picking the middle date of each (returning all valid unique dates when `<= count`).
|
||||
- **Price ratio & FX rules (`crossCheckPrices`):** Compares raw sample closes over dates where both sides have a finite `close > 0` (conflicting duplicate closes or FX rates drop that date and are counted; missing FX dates on cross-currency pairs are skipped and never forward-filled). Expected `left/right` ratio is `1.0` for matching ISO currencies, `usdCad` (CAD per 1 USD) for `CAD`/`USD`, and `1 / usdCad` for `USD`/`CAD`; any other currency combination returns `unsupported_currency_pair`.
|
||||
- **Return correlation:** Computes two-pass centered Pearson correlation of consecutive log returns in a canonical common currency (converting CAD closes to USD via `cadClose / usdCad` for `CAD`/`USD` pairs so correlation is orientation-symmetric and unaffected by FX moves on flat USD prices), rounded to `4` decimal places (`provider_mapping.price_corr NUMERIC(5,4)`). Returns `null` when fewer than `3` returns exist or either series has zero variance.
|
||||
- **Strict verdict priority & defaults:** `unsupported_currency_pair` -> `insufficient_data` (`usableDates < 5` or `priceCorr === null`) -> `ratio_mismatch` (`|medianRatio - 1| > 0.02`) -> `low_correlation` (`priceCorr < 0.9`) -> `confirmed`.
|
||||
- **Strict verdict priority & defaults:** `unsupported_currency_pair` -> `insufficient_data` (`usableDates < 5` or `priceCorr === null`) -> `ratio_mismatch` (`medianRatio` outside inclusive `[1 - 0.02, 1 + 0.02]`) -> `low_correlation` (`priceCorr < 0.9`) -> `confirmed`. Malformed candidate pairs or endpoints never abort the batch; each malformed slot is counted in `stats.invalidPairs` and recorded with a deterministic non-confirming verdict so verdict counts always reconcile with `pairsIn`.
|
||||
- **Conflicts:** Emits `resolution_conflict`-shaped records only for `ratio_mismatch` (`price_ratio_mismatch`) and `low_correlation` (`price_low_correlation`), keyed to `left.source` and `left.sourceId` with a JSON-serializable `detail` payload.
|
||||
|
|
|
|||
|
|
@ -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