Intelligence, Advanced Charting & Trading Platform
feature/chartnova-multimarket-chart-ws-foundation
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 | // ChartNova canonical transport contracts. No provider keys, network calls or sample market prices. |
| 2 | export const DATA_KIND = Object.freeze({ |
| 3 | REFERENCE: "reference_point", |
| 4 | CANDLE: "source_ohlc", |
| 5 | }); |
| 6 | export const QUALITY = Object.freeze(["GOOD", "DEGRADED", "STALE", "INVALID"]); |
| 7 | export const TIMEFRAMES = Object.freeze(["1m","5m","15m","30m","1h","4h","1d"]); |
| 8 | const assert = (cond, message) => { if (!cond) throw new Error(message); }; |
| 9 | const decimals = (value, field, allowZero=false) => { |
| 10 | const raw = typeof value === "number" ? String(value) : value; |
| 11 | assert(typeof raw === "string" && /^(?:0|[1-9]\d{0,19})(?:\.\d{1,12})?$/.test(raw), field+": invalid decimal"); |
| 12 | const number = Number(raw); |
| 13 | assert(Number.isFinite(number) && (allowZero ? number>=0 : number>0),field+": invalid value"); |
| 14 | return raw; |
| 15 | }; |
| 16 | const ms = (time,field) => { |
| 17 | assert(typeof time === "string" && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?(?:Z|[+-]\d\d:\d\d)$/.test(time),field+": expected timezone-qualified ISO8601"); |
| 18 | const n=Date.parse(time);assert(Number.isFinite(n),field+": invalid time");return n; |
| 19 | }; |
| 20 | const symbol=(x)=>assert(typeof x==="string"&&/^[A-Z0-9_]{3,48}$/.test(x),"invalid canonical symbol"); |
| 21 | const source=(x)=>assert(typeof x==="string"&&/^[a-z][a-z0-9_]{1,39}$/.test(x),"invalid provider source"); |
| 22 | function common(o) { |
| 23 | assert(o && typeof o==="object"&&!Array.isArray(o),"invalid market record"); |
| 24 | symbol(o.symbol);source(o.source); |
| 25 | assert(typeof o.unit==="string"&&/^[A-Z0-9_]{3,64}$/.test(o.unit),"missing/invalid canonical unit"); |
| 26 | assert(QUALITY.includes(o.quality),"quality must be explicit"); |
| 27 | assert(typeof o.sourceTimestamp==="string","sourceTimestamp required"); |
| 28 | ms(o.sourceTimestamp,"sourceTimestamp"); |
| 29 | assert(typeof o.receivedAt==="string","receivedAt required"); |
| 30 | ms(o.receivedAt,"receivedAt"); |
| 31 | assert(Date.parse(o.sourceTimestamp)<=Date.parse(o.receivedAt)+5*60_000,"provider timestamp is in the future"); |
| 32 | assert(typeof o.licenseEvidence==="string"&&o.licenseEvidence.trim().length>=8,"licenseEvidence required"); |
| 33 | assert(["research_only","approved_public","pending_review"].includes(o.rights),"explicit redistribution rights required"); |
| 34 | } |
| 35 | export function canonicalReferencePoint(o) { |
| 36 | common(o);assert(o.dataKind===DATA_KIND.REFERENCE,"reference point kind required"); |
| 37 | return Object.freeze({ |
| 38 | dataKind:o.dataKind,symbol:o.symbol,source:o.source,unit:o.unit, |
| 39 | price:decimals(o.price,"price"),sourceTimestamp:new Date(o.sourceTimestamp).toISOString(), |
| 40 | receivedAt:new Date(o.receivedAt).toISOString(), |
| 41 | quality:o.quality,licenseEvidence:o.licenseEvidence,rights:o.rights, |
| 42 | venue:o.venue??null,quoteSide:o.quoteSide??"reference", |
| 43 | }); |
| 44 | } |
| 45 | export function canonicalSourceCandle(o) { |
| 46 | common(o);assert(o.dataKind===DATA_KIND.CANDLE,"source OHLC kind required"); |
| 47 | assert(TIMEFRAMES.includes(o.timeframe),"unsupported timeframe"); |
| 48 | const a=ms(o.openTime,"openTime"),b=ms(o.closeTime,"closeTime"); |
| 49 | assert(a<b,"candle interval invalid"); |
| 50 | assert(typeof o.timezone==="string"&&o.timezone.length>0,"timezone required"); |
| 51 | const fields=Object.fromEntries(["open","high","low","close"].map(k=>[k,decimals(o[k],k)])); |
| 52 | const n=(k)=>Number(fields[k]); |
| 53 | assert(n("low")<=n("open")&&n("open")<=n("high")&&n("low")<=n("close")&&n("close")<=n("high"),"OHLC ordering invalid"); |
| 54 | return Object.freeze({ |
| 55 | ...fields,dataKind:o.dataKind,symbol:o.symbol,source:o.source,unit:o.unit,timeframe:o.timeframe, |
| 56 | openTime:new Date(a).toISOString(),closeTime:new Date(b).toISOString(), |
| 57 | volume:o.volume===undefined||o.volume===null?null:decimals(o.volume,"volume",true), |
| 58 | sourceTimestamp:new Date(o.sourceTimestamp).toISOString(),receivedAt:new Date(o.receivedAt).toISOString(), |
| 59 | timezone:o.timezone,quality:o.quality,licenseEvidence:o.licenseEvidence,rights:o.rights, |
| 60 | venue:o.venue??null,quoteSide:o.quoteSide??"trade", |
| 61 | }); |
| 62 | } |
| 63 | export function mayPublish(record) { |
| 64 | return record?.rights==="approved_public"&&record?.quality!=="INVALID"; |
| 65 | } |
| 66 | export function transportFrame(record, sequence, publishedAt) { |
| 67 | assert(Number.isSafeInteger(sequence)&&sequence>0,"sequence must be positive"); |
| 68 | ms(publishedAt,"publishedAt"); |
| 69 | assert(mayPublish(record),"unlicensed or invalid market data must not be published"); |
| 70 | const {licenseEvidence,rights,receivedAt,...safe}=record; |
| 71 | return Object.freeze({ |
| 72 | type:"market.update",version:1,sequence,publishedAt:new Date(publishedAt).toISOString(), |
| 73 | payload:safe, |
| 74 | }); |
| 75 | } |