Intelligence, Advanced Charting & Trading Platform
phase-1/xau-provider-adapter
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 | |
| 5 | function validDate(value, name) { |
| 6 | if (!(value instanceof Date) || Number.isNaN(value.valueOf())) { |
| 7 | throw new TypeError(`${name} must be a valid Date`); |
| 8 | } |
| 9 | } |
| 10 | |
| 11 | function positiveFinite(value, name) { |
| 12 | if (!Number.isFinite(value) || value <= 0) { |
| 13 | throw new RangeError(`${name} must be positive and finite`); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | export function normalizeProviderObservation({ |
| 18 | providerCode, |
| 19 | canonicalSymbol, |
| 20 | providerTimestamp, |
| 21 | receivedAt, |
| 22 | price, |
| 23 | rawUnit, |
| 24 | rawPayload = null, |
| 25 | referencePrice = null, |
| 26 | maxDivergencePct = null, |
| 27 | maxAgeMs, |
| 28 | }) { |
| 29 | validDate(providerTimestamp, 'providerTimestamp'); |
| 30 | validDate(receivedAt, 'receivedAt'); |
| 31 | |
| 32 | const canonical = canonicalObservation({ |
| 33 | providerCode, |
| 34 | canonicalSymbol, |
| 35 | providerTimestamp, |
| 36 | receivedAt, |
| 37 | price, |
| 38 | rawUnit, |
| 39 | }); |
| 40 | |
| 41 | const ageMs = receivedAt.valueOf() - providerTimestamp.valueOf(); |
| 42 | const quality = evaluateObservationQuality({ |
| 43 | price: canonical.price, |
| 44 | ageMs, |
| 45 | maxAgeMs, |
| 46 | referencePrice, |
| 47 | maxDivergencePct, |
| 48 | }); |
| 49 | |
| 50 | return { |
| 51 | raw: { |
| 52 | providerCode, |
| 53 | canonicalSymbol, |
| 54 | providerTimestamp: providerTimestamp.toISOString(), |
| 55 | receivedAt: receivedAt.toISOString(), |
| 56 | price, |
| 57 | rawUnit, |
| 58 | rawPayload, |
| 59 | }, |
| 60 | tick: { |
| 61 | providerCode, |
| 62 | canonicalSymbol, |
| 63 | timestamp: canonical.providerTimestamp, |
| 64 | receivedAt: canonical.receivedAt, |
| 65 | price: canonical.price, |
| 66 | canonicalUnit: canonical.canonicalUnit, |
| 67 | quality, |
| 68 | }, |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | export function deriveGoldMetrics({ |
| 73 | target, |
| 74 | xau, |
| 75 | usdIrt, |
| 76 | marketGold, |
| 77 | maxAgeMs, |
| 78 | formulaVersion, |
| 79 | }) { |
| 80 | validDate(target, 'target'); |
| 81 | validDate(marketGold?.timestamp, 'marketGold.timestamp'); |
| 82 | positiveFinite(marketGold?.value, 'marketGold.value'); |
| 83 | if (!formulaVersion) throw new TypeError('formulaVersion is required'); |
| 84 | |
| 85 | const aligned = alignSources({ target, xau, usdIrt, maxAgeMs }); |
| 86 | |
| 87 | const targetMs = target.valueOf(); |
| 88 | const marketMs = marketGold.timestamp.valueOf(); |
| 89 | if (marketMs > targetMs) throw new RangeError('marketGold timestamp cannot be after target'); |
| 90 | |
| 91 | const marketAgeMs = targetMs - marketMs; |
| 92 | const theoreticalIrtPerGram = theoreticalGold18Irt({ |
| 93 | xauUsdPerTroyOunce: xau.value, |
| 94 | usdIrt: usdIrt.value, |
| 95 | }); |
| 96 | const premium = goldPremium({ |
| 97 | marketIrtPerGram: marketGold.value, |
| 98 | theoreticalIrtPerGram, |
| 99 | }); |
| 100 | |
| 101 | const quality = aligned.quality === 'STALE' || marketAgeMs > maxAgeMs ? 'STALE' : 'GOOD'; |
| 102 | |
| 103 | return { |
| 104 | targetTimestamp: target.toISOString(), |
| 105 | formulaVersion, |
| 106 | theoreticalIrtPerGram, |
| 107 | premium, |
| 108 | quality, |
| 109 | sourceSnapshot: { |
| 110 | targetTimestamp: target.toISOString(), |
| 111 | xau: aligned.xau, |
| 112 | usdIrt: aligned.usdIrt, |
| 113 | marketGold: { |
| 114 | value: marketGold.value, |
| 115 | timestamp: marketGold.timestamp.toISOString(), |
| 116 | }, |
| 117 | agesMs: { |
| 118 | xau: aligned.xauAgeMs, |
| 119 | usdIrt: aligned.usdIrtAgeMs, |
| 120 | marketGold: marketAgeMs, |
| 121 | }, |
| 122 | }, |
| 123 | }; |
| 124 | } |