Intelligence, Advanced Charting & Trading Platform
Phase 1: implement XAU/USD provider adapter
* test: define Alpha Vantage XAU adapter contract * feat: implement Alpha Vantage XAU spot adapter * chore: document market provider secret contract
davoodmehraban89 committed
Oct 5, 2026 at 16:28 UTC
c19088581c5bd4e0d01cb665a7044d7c49228b47
3 files changed
+148
.env.example
+4
@@ -1,6 +1,10 @@
1
# Canonical PostgreSQL connection. Provider-agnostic by design.
2
DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/DATABASE?sslmode=require
3
4
+# Market data provider credentials. Never commit real keys.
5
+NAVASAN_API_KEY=
6
+ALPHAVANTAGE_API_KEY=
7
+
8
# Application defaults
9
APP_TIMEZONE=Asia/Tehran
10
MARKET_DATA_DEFAULT_QUALITY_THRESHOLD=70
packages/market-domain/src/adapters/alpha-vantage.mjs
new
+79
@@ -0,0 +1,79 @@
1
+import { canonicalObservation } from '../provider-contract.mjs';
2
+
3
+const ACCEPTED_UNITS = new Set([
4
+ 'usd per troy ounce',
5
+ 'usd/troy ounce',
6
+ 'usd per troy oz',
7
+ 'usd/troy oz',
8
+]);
9
+
10
+function validDate(value, name) {
11
+ if (!(value instanceof Date) || Number.isNaN(value.valueOf())) {
12
+ throw new TypeError(`${name} must be a valid Date`);
13
+ }
14
+}
15
+
16
+function parsePositiveNumeric(value, name) {
17
+ const numeric = typeof value === 'string' && value.trim() !== '' ? Number(value) : value;
18
+ if (!Number.isFinite(numeric) || numeric <= 0) {
19
+ throw new RangeError(`${name} must be numeric, positive, and finite`);
20
+ }
21
+ return numeric;
22
+}
23
+
24
+function parseProviderTimestamp(value) {
25
+ const date = value instanceof Date ? value : new Date(value);
26
+ if (Number.isNaN(date.valueOf())) throw new TypeError('timestamp must be a valid date/time');
27
+ return date;
28
+}
29
+
30
+export function buildAlphaVantageGoldSpotUrl(apiKey) {
31
+ if (typeof apiKey !== 'string' || apiKey.trim() === '') {
32
+ throw new TypeError('apiKey is required');
33
+ }
34
+ const url = new URL('https://www.alphavantage.co/query');
35
+ url.searchParams.set('function', 'GOLD_SILVER_SPOT');
36
+ url.searchParams.set('symbol', 'GOLD');
37
+ url.searchParams.set('apikey', apiKey);
38
+ return url.toString();
39
+}
40
+
41
+export function parseAlphaVantageGoldSpot({ payload, receivedAt }) {
42
+ validDate(receivedAt, 'receivedAt');
43
+ if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
44
+ throw new TypeError('payload must be an object');
45
+ }
46
+
47
+ const unit = String(payload.unit ?? '').trim().toLowerCase();
48
+ if (!ACCEPTED_UNITS.has(unit)) {
49
+ throw new RangeError(`unsupported Alpha Vantage gold unit: ${payload.unit ?? '<missing>'}`);
50
+ }
51
+
52
+ const price = parsePositiveNumeric(payload.price, 'price');
53
+ const providerTimestamp = parseProviderTimestamp(payload.timestamp);
54
+
55
+ const canonical = canonicalObservation({
56
+ providerCode: 'alpha_vantage',
57
+ canonicalSymbol: 'XAU_USD',
58
+ providerTimestamp,
59
+ receivedAt,
60
+ price,
61
+ rawUnit: 'USD_PER_TROY_OUNCE',
62
+ });
63
+
64
+ return {
65
+ providerCode: canonical.providerCode,
66
+ canonicalSymbol: canonical.canonicalSymbol,
67
+ providerTimestamp: canonical.providerTimestamp,
68
+ receivedAt: canonical.receivedAt,
69
+ price: canonical.price,
70
+ canonicalUnit: canonical.canonicalUnit,
71
+ rawUnit: 'USD_PER_TROY_OUNCE',
72
+ raw: {
73
+ price: payload.price,
74
+ unit: payload.unit,
75
+ timestamp: payload.timestamp,
76
+ symbol: payload.symbol ?? payload.name ?? null,
77
+ },
78
+ };
79
+}
packages/market-domain/test/alpha-vantage-adapter.test.mjs
new
+65
@@ -0,0 +1,65 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+import {
4
+ buildAlphaVantageGoldSpotUrl,
5
+ parseAlphaVantageGoldSpot,
6
+} from '../src/adapters/alpha-vantage.mjs';
7
+
8
+test('builds Alpha Vantage GOLD_SILVER_SPOT request without mutating key', () => {
9
+ const url = buildAlphaVantageGoldSpotUrl('secret-key');
10
+ assert.equal(
11
+ url,
12
+ 'https://www.alphavantage.co/query?function=GOLD_SILVER_SPOT&symbol=GOLD&apikey=secret-key',
13
+ );
14
+});
15
+
16
+test('parses Alpha Vantage gold spot payload into canonical XAU_USD observation', () => {
17
+ const result = parseAlphaVantageGoldSpot({
18
+ payload: {
19
+ price: '4001.25',
20
+ unit: 'USD per troy ounce',
21
+ timestamp: '2026-10-05T12:00:00Z',
22
+ },
23
+ receivedAt: new Date('2026-10-05T12:00:02Z'),
24
+ });
25
+
26
+ assert.equal(result.providerCode, 'alpha_vantage');
27
+ assert.equal(result.canonicalSymbol, 'XAU_USD');
28
+ assert.equal(result.price, 4001.25);
29
+ assert.equal(result.canonicalUnit, 'USD_PER_TROY_OUNCE');
30
+ assert.equal(result.providerTimestamp, '2026-10-05T12:00:00.000Z');
31
+});
32
+
33
+test('accepts equivalent documented unit spelling', () => {
34
+ const result = parseAlphaVantageGoldSpot({
35
+ payload: {
36
+ price: 3999.5,
37
+ unit: 'USD/troy ounce',
38
+ timestamp: '2026-10-05T12:00:00Z',
39
+ },
40
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
41
+ });
42
+ assert.equal(result.price, 3999.5);
43
+});
44
+
45
+test('rejects non-USD-per-troy-ounce units', () => {
46
+ assert.throws(() => parseAlphaVantageGoldSpot({
47
+ payload: {
48
+ price: '4000',
49
+ unit: 'USD per gram',
50
+ timestamp: '2026-10-05T12:00:00Z',
51
+ },
52
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
53
+ }), /unit/i);
54
+});
55
+
56
+test('rejects provider timestamps after receive time', () => {
57
+ assert.throws(() => parseAlphaVantageGoldSpot({
58
+ payload: {
59
+ price: '4000',
60
+ unit: 'USD per troy ounce',
61
+ timestamp: '2026-10-05T12:00:03Z',
62
+ },
63
+ receivedAt: new Date('2026-10-05T12:00:01Z'),
64
+ }), /before providerTimestamp/i);
65
+});