Intelligence, Advanced Charting & Trading Platform
phase-1/postgres-ci
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | import { canonicalObservation } from './provider-contract.mjs'; |
| 2 | import { evaluateObservationQuality } from './data-quality.mjs'; |
| 3 | import { alignSources, theoreticalGold18Irt, goldPremium } from './gold-pricing.mjs'; |
| 4 | import { qualityScore } from './quality-score.mjs'; |
| 5 | |
| 6 | function validDate(value, name) { |
| 7 | if (!(value instanceof Date) || Number.isNaN(value.valueOf())) { |
| 8 | throw new TypeError(`${name} must be a valid Date`); |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | function positiveFinite(value, name) { |
| 13 | if (!Number.isFinite(value) || value <= 0) { |
| 14 | throw new RangeError(`${name} must be positive and finite`); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | function sourceQuality(source, fallbackState) { |
| 19 | const state = source?.quality?.state ?? fallbackState; |
| 20 | const score = source?.quality?.score; |
| 21 | return { |
| 22 | state, |
| 23 | score: Number.isFinite(score) ? Math.max(0, Math.min(100, score)) : qualityScore({ state }), |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | function weakestQuality(qualities) { |
| 28 | const order = { GOOD: 0, DEGRADED: 1, STALE: 2, INVALID: 3 }; |
| 29 | return qualities.reduce((weakest, item) => order[item.state] > order[weakest.state] ? item : weakest); |
| 30 | } |
| 31 | |
| 32 | export function normalizeProviderObservation({ |
| 33 | providerCode, |
| 34 | canonicalSymbol, |
| 35 | providerTimestamp, |
| 36 | receivedAt, |
| 37 | price, |
| 38 | rawUnit, |
| 39 | rawPayload = null, |
| 40 | referencePrice = null, |
| 41 | maxDivergencePct = null, |
| 42 | maxAgeMs, |
| 43 | }) { |
| 44 | validDate(providerTimestamp, 'providerTimestamp'); |
| 45 | validDate(receivedAt, 'receivedAt'); |
| 46 | |
| 47 | const canonical = canonicalObservation({ |
| 48 | providerCode, |
| 49 | canonicalSymbol, |
| 50 | providerTimestamp, |
| 51 | receivedAt, |
| 52 | price, |
| 53 | rawUnit, |
| 54 | }); |
| 55 | |
| 56 | const ageMs = receivedAt.valueOf() - providerTimestamp.valueOf(); |
| 57 | const quality = evaluateObservationQuality({ |
| 58 | price: canonical.price, |
| 59 | ageMs, |
| 60 | maxAgeMs, |
| 61 | referencePrice, |
| 62 | maxDivergencePct, |
| 63 | }); |
| 64 | quality.score = qualityScore({ |
| 65 | state: quality.state, |
| 66 | flags: quality.flags, |
| 67 | ageRatio: maxAgeMs > 0 ? ageMs / maxAgeMs : 0, |
| 68 | }); |
| 69 | |
| 70 | return { |
| 71 | raw: { |
| 72 | providerCode, |
| 73 | canonicalSymbol, |
| 74 | providerTimestamp: providerTimestamp.toISOString(), |
| 75 | receivedAt: receivedAt.toISOString(), |
| 76 | price, |
| 77 | rawUnit, |
| 78 | rawPayload, |
| 79 | }, |
| 80 | tick: { |
| 81 | providerCode, |
| 82 | canonicalSymbol, |
| 83 | timestamp: canonical.providerTimestamp, |
| 84 | receivedAt: canonical.receivedAt, |
| 85 | price: canonical.price, |
| 86 | canonicalUnit: canonical.canonicalUnit, |
| 87 | quality, |
| 88 | }, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | export function deriveGoldMetrics({ |
| 93 | target, |
| 94 | xau, |
| 95 | usdIrt, |
| 96 | marketGold, |
| 97 | maxAgeMs, |
| 98 | formulaVersion, |
| 99 | }) { |
| 100 | validDate(target, 'target'); |
| 101 | validDate(marketGold?.timestamp, 'marketGold.timestamp'); |
| 102 | positiveFinite(marketGold?.value, 'marketGold.value'); |
| 103 | if (!formulaVersion) throw new TypeError('formulaVersion is required'); |
| 104 | |
| 105 | const aligned = alignSources({ target, xau, usdIrt, maxAgeMs }); |
| 106 | |
| 107 | const targetMs = target.valueOf(); |
| 108 | const marketMs = marketGold.timestamp.valueOf(); |
| 109 | if (marketMs > targetMs) throw new RangeError('marketGold timestamp cannot be after target'); |
| 110 | |
| 111 | const marketAgeMs = targetMs - marketMs; |
| 112 | const theoreticalIrtPerGram = theoreticalGold18Irt({ |
| 113 | xauUsdPerTroyOunce: xau.value, |
| 114 | usdIrt: usdIrt.value, |
| 115 | }); |
| 116 | const premium = goldPremium({ |
| 117 | marketIrtPerGram: marketGold.value, |
| 118 | theoreticalIrtPerGram, |
| 119 | }); |
| 120 | |
| 121 | const xauQuality = sourceQuality(xau, aligned.xauAgeMs > maxAgeMs ? 'STALE' : 'GOOD'); |
| 122 | const usdQuality = sourceQuality(usdIrt, aligned.usdIrtAgeMs > maxAgeMs ? 'STALE' : 'GOOD'); |
| 123 | const marketQuality = sourceQuality(marketGold, marketAgeMs > maxAgeMs ? 'STALE' : 'GOOD'); |
| 124 | const weakest = weakestQuality([xauQuality, usdQuality, marketQuality]); |
| 125 | const ageStale = aligned.quality === 'STALE' || marketAgeMs > maxAgeMs; |
| 126 | const quality = ageStale ? 'STALE' : weakest.state; |
| 127 | const propagatedScore = Math.min(xauQuality.score, usdQuality.score, marketQuality.score); |
| 128 | const derivedScore = ageStale ? Math.min(30, propagatedScore) : propagatedScore; |
| 129 | |
| 130 | return { |
| 131 | targetTimestamp: target.toISOString(), |
| 132 | formulaVersion, |
| 133 | theoreticalIrtPerGram, |
| 134 | premium, |
| 135 | quality, |
| 136 | qualityScore: derivedScore, |
| 137 | sourceSnapshot: { |
| 138 | targetTimestamp: target.toISOString(), |
| 139 | xau: { ...aligned.xau, quality: xauQuality }, |
| 140 | usdIrt: { ...aligned.usdIrt, quality: usdQuality }, |
| 141 | marketGold: { |
| 142 | value: marketGold.value, |
| 143 | timestamp: marketGold.timestamp.toISOString(), |
| 144 | quality: marketQuality, |
| 145 | }, |
| 146 | agesMs: { |
| 147 | xau: aligned.xauAgeMs, |
| 148 | usdIrt: aligned.usdIrtAgeMs, |
| 149 | marketGold: marketAgeMs, |
| 150 | }, |
| 151 | }, |
| 152 | }; |
| 153 | } |