Intelligence, Advanced Charting & Trading Platform
feat(chart): normalize reference lines separately from licensed OHLC candlesticks
davoodmehraban89 committed
Oct 12, 2026 at 00:19 UTC
8e476f9a11e1e5c7fd3aa95321966c28d1f60f7a
1 file changed
+48
packages/market-feed/src/chart-adapter.mjs
new
+48
@@ -0,0 +1,48 @@
1
+// UI data adapter: protects candlestick rendering from point-only sources.
2
+export const REFERENCE_KIND="daily_reference_point_not_ohlc";
3
+export function normalizeHistoryResponse(payload, expectedSymbol){
4
+ if(!payload||payload.ok!==true||payload.symbol!==expectedSymbol)throw Error("history_not_available");
5
+ const kind=payload.dataKind;
6
+ if(!Array.isArray(payload.series)||payload.series.length===0)throw Error("empty_history");
7
+ if(kind===REFERENCE_KIND||kind==="reference_point"){
8
+ let previous="";
9
+ const data=payload.series.map((row)=>{
10
+ if(!/^\d{4}-\d{2}-\d{2}$/.test(row.date||"")||row.date<=previous)throw Error("reference_history_order");
11
+ const value=typeof row.close==="number"?row.close:Number(row.close);
12
+ if(!Number.isFinite(value)||value<=0)throw Error("invalid_reference_rate");
13
+ previous=row.date;return {time:row.date,value};
14
+ });
15
+ return {kind:"reference",data,source:payload.source,unit:payload.quoteCurrency??payload.unit??"unknown",storage:payload.storage??"unspecified",attribution:payload.attribution??"Source: ECB"};
16
+ }
17
+ if(kind!=="source_ohlc"||payload.rights!=="approved_public")throw Error("unlicensed_or_unknown_history");
18
+ let previous=-Infinity;
19
+ const data=payload.series.map(row=>{
20
+ const date=row.openTime??row.time;
21
+ const time=typeof date==="string"&&/^\d{4}-\d{2}-\d{2}$/.test(date)?date:
22
+ typeof date==="string"&&Number.isFinite(Date.parse(date))?Math.floor(Date.parse(date)/1000):date;
23
+ const sortable=typeof time==="string"?Date.parse(time+"T00:00:00Z"):time*1000;
24
+ if(!Number.isFinite(sortable)||sortable<=previous)throw Error("invalid_candle_time");
25
+ const [open,high,low,close]=["open","high","low","close"].map(k=>Number(row[k]));
26
+ if(![open,high,low,close].every(Number.isFinite)||low<=0||low>open||low>close||high<open||high<close)throw Error("invalid_candle_prices");
27
+ previous=sortable;return {time,open,high,low,close};
28
+ });
29
+ return {kind:"candle",data,source:payload.source,unit:payload.unit,storage:payload.storage??"unspecified",attribution:payload.attribution??"Source attribution required"};
30
+}
31
+export function normalizeStreamUpdate(event,symbol,kind){
32
+ if(event?.version!==1||event.type!=="market.update"||event.payload?.symbol!==symbol)return null;
33
+ const row=event.payload;
34
+ if(kind==="reference"&&row.dataKind==="reference_point"){
35
+ const timestamp=Date.parse(row.sourceTimestamp);
36
+ const value=Number(row.price);
37
+ if(!Number.isFinite(timestamp)||!Number.isFinite(value)||value<=0)return null;
38
+ return {time:new Date(timestamp).toISOString().slice(0,10),value};
39
+ }
40
+ if(kind==="candle"&&row.dataKind==="source_ohlc"){
41
+ const n=["open","high","low","close"].map(k=>Number(row[k]));
42
+ if(!n.every(Number.isFinite)||n[2]<=0||n[2]>n[0]||n[2]>n[3]||n[1]<n[0]||n[1]<n[3])return null;
43
+ const time=row.timeframe==="1d"?new Date(row.openTime).toISOString().slice(0,10):Math.floor(Date.parse(row.openTime)/1000);
44
+ if(typeof time==="number"&&!Number.isFinite(time))return null;
45
+ return {time,open:n[0],high:n[1],low:n[2],close:n[3]};
46
+ }
47
+ return null;
48
+}