Intelligence, Advanced Charting & Trading Platform
Phase 1: implement ingestion and derived-gold pipeline
* test: define ingestion and derived-gold pipeline behavior * feat: implement ingestion and derived-gold pipeline
davoodmehraban89 committed
Oct 5, 2026 at 16:21 UTC
745e7f26f8e018598040aff7bc738616a9e88bb1
2 files changed
+194
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
+}
packages/market-domain/test/pipeline.test.mjs
new
+70
@@ -0,0 +1,70 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import { normalizeProviderObservation, deriveGoldMetrics } from '../src/pipeline.mjs';
4
+
5
+test('normalizes an Iranian rial provider observation and preserves audit input', () => {
6
+ const result = normalizeProviderObservation({
7
+ providerCode: 'navasan',
8
+ canonicalSymbol: 'USD_IRT_FREE',
9
+ providerTimestamp: new Date('2026-10-05T12:00:00Z'),
10
+ receivedAt: new Date('2026-10-05T12:00:02Z'),
11
+ price: 1_000_000,
12
+ rawUnit: 'IRR_PER_USD',
13
+ rawPayload: { value: '1000000' },
14
+ referencePrice: 100_100,
15
+ maxDivergencePct: 1,
16
+ maxAgeMs: 60_000,
17
+ });
18
+
19
+ assert.equal(result.tick.price, 100_000);
20
+ assert.equal(result.tick.quality.state, 'GOOD');
21
+ assert.equal(result.raw.price, 1_000_000);
22
+ assert.equal(result.raw.rawUnit, 'IRR_PER_USD');
23
+ assert.deepEqual(result.raw.rawPayload, { value: '1000000' });
24
+});
25
+
26
+test('derives theoretical gold and premium with complete source provenance', () => {
27
+ const target = new Date('2026-10-05T12:00:00Z');
28
+ const result = deriveGoldMetrics({
29
+ target,
30
+ xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
31
+ usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:59:45Z') },
32
+ marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T11:59:40Z') },
33
+ maxAgeMs: 60_000,
34
+ formulaVersion: 'gold18-v1',
35
+ });
36
+
37
+ assert.equal(result.quality, 'GOOD');
38
+ assert.equal(result.formulaVersion, 'gold18-v1');
39
+ assert.equal(result.sourceSnapshot.targetTimestamp, '2026-10-05T12:00:00.000Z');
40
+ assert.equal(result.sourceSnapshot.xau.timestamp, '2026-10-05T11:59:50.000Z');
41
+ assert.equal(result.sourceSnapshot.usdIrt.timestamp, '2026-10-05T11:59:45.000Z');
42
+ assert.equal(result.sourceSnapshot.marketGold.timestamp, '2026-10-05T11:59:40.000Z');
43
+ assert.ok(result.theoreticalIrtPerGram > 0);
44
+ assert.ok(Number.isFinite(result.premium.absoluteIrt));
45
+ assert.ok(Number.isFinite(result.premium.percent));
46
+});
47
+
48
+test('marks derived gold metrics stale when any required source is stale', () => {
49
+ const target = new Date('2026-10-05T12:00:00Z');
50
+ const result = deriveGoldMetrics({
51
+ target,
52
+ xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
53
+ usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:55:00Z') },
54
+ marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T11:59:40Z') },
55
+ maxAgeMs: 60_000,
56
+ formulaVersion: 'gold18-v1',
57
+ });
58
+ assert.equal(result.quality, 'STALE');
59
+});
60
+
61
+test('rejects a market-gold observation from the future', () => {
62
+ assert.throws(() => deriveGoldMetrics({
63
+ target: new Date('2026-10-05T12:00:00Z'),
64
+ xau: { value: 4_000, timestamp: new Date('2026-10-05T11:59:50Z') },
65
+ usdIrt: { value: 100_000, timestamp: new Date('2026-10-05T11:59:45Z') },
66
+ marketGold: { value: 10_000_000, timestamp: new Date('2026-10-05T12:00:01Z') },
67
+ maxAgeMs: 60_000,
68
+ formulaVersion: 'gold18-v1',
69
+ }), /after target/);
70
+});