Intelligence, Advanced Charting & Trading Platform
ops/chartnova-emergency-rollback-2026-10-15
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 | |
| 3 | export const NAVASAN_SYMBOLS = Object.freeze({ |
| 4 | usd_sell: Object.freeze({ canonicalSymbol: 'USD_IRT_FREE', rawUnit: 'IRR_PER_USD' }), |
| 5 | '18ayar': Object.freeze({ canonicalSymbol: 'GOLD18_IRT', rawUnit: 'IRR_PER_GRAM' }), |
| 6 | }); |
| 7 | |
| 8 | function validDate(value, name) { |
| 9 | if (!(value instanceof Date) || Number.isNaN(value.valueOf())) { |
| 10 | throw new TypeError(`${name} must be a valid Date`); |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | function parsePositiveNumeric(value, name) { |
| 15 | const numeric = typeof value === 'string' && value.trim() !== '' ? Number(value) : value; |
| 16 | if (!Number.isFinite(numeric) || numeric <= 0) { |
| 17 | throw new RangeError(`${name} must be numeric, positive, and finite`); |
| 18 | } |
| 19 | return numeric; |
| 20 | } |
| 21 | |
| 22 | function unixSecondsToDate(value, name) { |
| 23 | const numeric = Number(value); |
| 24 | if (!Number.isFinite(numeric) || numeric <= 0) { |
| 25 | throw new RangeError(`${name} must be a positive Unix timestamp`); |
| 26 | } |
| 27 | const date = new Date(numeric * 1000); |
| 28 | if (Number.isNaN(date.valueOf())) throw new RangeError(`${name} is invalid`); |
| 29 | return date; |
| 30 | } |
| 31 | |
| 32 | function toCanonicalPrice(rawPrice, rawUnit) { |
| 33 | if (rawUnit === 'IRR_PER_USD' || rawUnit === 'IRR_PER_GRAM') return rawPrice / 10; |
| 34 | return rawPrice; |
| 35 | } |
| 36 | |
| 37 | export function parseNavasanLatest({ payload, receivedAt }) { |
| 38 | validDate(receivedAt, 'receivedAt'); |
| 39 | if (!payload || typeof payload !== 'object' || Array.isArray(payload)) { |
| 40 | throw new TypeError('payload must be an object'); |
| 41 | } |
| 42 | |
| 43 | const observations = []; |
| 44 | |
| 45 | for (const [providerSymbol, mapping] of Object.entries(NAVASAN_SYMBOLS)) { |
| 46 | const item = payload[providerSymbol]; |
| 47 | if (item == null) continue; |
| 48 | if (typeof item !== 'object' || Array.isArray(item)) { |
| 49 | throw new TypeError(`Navasan ${providerSymbol} observation must be an object`); |
| 50 | } |
| 51 | |
| 52 | const rawPrice = parsePositiveNumeric(item.value, `${providerSymbol}.value`); |
| 53 | const providerTimestamp = unixSecondsToDate(item.timestamp, `${providerSymbol}.timestamp`); |
| 54 | |
| 55 | const canonical = canonicalObservation({ |
| 56 | providerCode: 'navasan', |
| 57 | canonicalSymbol: mapping.canonicalSymbol, |
| 58 | providerTimestamp, |
| 59 | receivedAt, |
| 60 | price: rawPrice, |
| 61 | rawUnit: mapping.rawUnit, |
| 62 | }); |
| 63 | |
| 64 | observations.push({ |
| 65 | providerCode: canonical.providerCode, |
| 66 | providerSymbol, |
| 67 | canonicalSymbol: canonical.canonicalSymbol, |
| 68 | providerTimestamp: canonical.providerTimestamp, |
| 69 | receivedAt: canonical.receivedAt, |
| 70 | price: canonical.price, |
| 71 | canonicalUnit: canonical.canonicalUnit, |
| 72 | rawUnit: mapping.rawUnit, |
| 73 | raw: { |
| 74 | value: item.value, |
| 75 | change: item.change ?? null, |
| 76 | timestamp: item.timestamp, |
| 77 | date: item.date ?? null, |
| 78 | }, |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | return observations; |
| 83 | } |
| 84 | |
| 85 | export function parseNavasanOhlc({ providerSymbol, payload }) { |
| 86 | const mapping = NAVASAN_SYMBOLS[providerSymbol]; |
| 87 | if (!mapping) throw new RangeError(`unsupported Navasan symbol: ${providerSymbol}`); |
| 88 | if (!Array.isArray(payload)) throw new TypeError('payload must be an array'); |
| 89 | |
| 90 | return payload.map((item, index) => { |
| 91 | if (!item || typeof item !== 'object' || Array.isArray(item)) { |
| 92 | throw new TypeError(`payload[${index}] must be an object`); |
| 93 | } |
| 94 | |
| 95 | const openRaw = parsePositiveNumeric(item.open, `payload[${index}].open`); |
| 96 | const highRaw = parsePositiveNumeric(item.high, `payload[${index}].high`); |
| 97 | const lowRaw = parsePositiveNumeric(item.low, `payload[${index}].low`); |
| 98 | const closeRaw = parsePositiveNumeric(item.close, `payload[${index}].close`); |
| 99 | const open = toCanonicalPrice(openRaw, mapping.rawUnit); |
| 100 | const high = toCanonicalPrice(highRaw, mapping.rawUnit); |
| 101 | const low = toCanonicalPrice(lowRaw, mapping.rawUnit); |
| 102 | const close = toCanonicalPrice(closeRaw, mapping.rawUnit); |
| 103 | |
| 104 | if (low > open || low > close || high < open || high < close || low > high) { |
| 105 | throw new RangeError(`invalid OHLC ordering at payload[${index}]`); |
| 106 | } |
| 107 | |
| 108 | return { |
| 109 | providerCode: 'navasan', |
| 110 | providerSymbol, |
| 111 | canonicalSymbol: mapping.canonicalSymbol, |
| 112 | timeframe: '1d', |
| 113 | openTime: unixSecondsToDate(item.timestamp, `payload[${index}].timestamp`).toISOString(), |
| 114 | open, |
| 115 | high, |
| 116 | low, |
| 117 | close, |
| 118 | canonicalUnit: mapping.canonicalSymbol === 'USD_IRT_FREE' ? 'IRT_PER_USD' : 'IRT_PER_GRAM', |
| 119 | rawUnit: mapping.rawUnit, |
| 120 | raw: { |
| 121 | timestamp: item.timestamp, |
| 122 | date: item.date ?? null, |
| 123 | open: item.open, |
| 124 | high: item.high, |
| 125 | low: item.low, |
| 126 | close: item.close, |
| 127 | }, |
| 128 | }; |
| 129 | }); |
| 130 | } |