Intelligence, Advanced Charting & Trading Platform
feat: implement ingestion and derived-gold pipeline
davoodmehraban89 committed
Oct 5, 2026 at 16:21 UTC
b66db5c9c596daebe1a0bd0443c32e8804fa8eb7
1 file changed
+124
packages/market-domain/src/pipeline.mjs
new
+124
@@ -0,0 +1,124 @@
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
+}