Files
worldmonitor/scripts/seed-commodity-quotes.mjs
Elie Habib ee66b6b5c2 feat(gold): Gold Intelligence v2 — positioning depth, returns, drivers (#3034)
* feat(gold): richer Gold Intelligence panel with positioning, returns, drivers

* fix(gold): restore leveragedFunds fields and derive P/S netPct in legacy fallback

Review catch on PR #3034:

1. seed-cot.mjs stopped emitting leveragedFundsLong/Short during the v2
   refactor, which would zero out the Leveraged Funds bars in the existing
   CotPositioningPanel on the next seed run. Re-read lev_money_* from the
   TFF rows and keep the fields on the output (commodity rows don't have
   this breakdown, stay at 0).
2. get-gold-intelligence legacy fallback hardcoded producerSwap.netPct to 0,
   meaning a pre-v2 COT snapshot rendered a neutral 0% Producer/Swap bar
   on deploy until seed-cot reran. Derive netPct from dealerLong/dealerShort
   (same formula as the v2 seeder). OI share stays 0 because open_interest
   wasn't captured pre-migration; clearly documented now.

Tests: added two regression guards (leveragedFunds preserved for TFF,
commodity rows emit 0 for those fields).

* fix(gold): make enrichment layer monitored and honest about freshness

Review catch on PR #3034:

- seed-commodity-quotes now writes seed-meta:market:gold-extended via
  writeExtraKeyWithMeta on every successful run. Partial / failed fetches
  skip BOTH the data write and the meta bump, so health correctly reports
  STALE_SEED instead of masking a broken Yahoo fetch with a green check.
- Require both gold (core) AND at least one driver/silver before writing,
  so a half-successful run doesn't overwrite healthy prior data with a
  degraded payload.
- Handler no longer stamps updatedAt with new Date() when the enrichment
  key is missing. Emits empty string so the panel's freshness indicator
  shows "Updated —" with a dim dot, matching reality — enrichment is
  missing, not fresh.
- Health: goldExtended entry in STANDALONE_KEYS + SEED_META (maxStaleMin
  30, matching commodity quotes), and seed-health.js advertises the
  domain so upstream monitors pick it up.

The panel already gates session/returns/drivers sections on presence, so
legacy panels without the enrichment layer stay fully functional.
2026-04-12 22:53:32 +04:00

288 lines
11 KiB
JavaScript

#!/usr/bin/env node
import { loadEnvFile, loadSharedConfig, sleep, runSeed, parseYahooChart, writeExtraKey, writeExtraKeyWithMeta, CHROME_UA } from './_seed-utils.mjs';
import { AV_PHYSICAL_MAP, fetchAvPhysicalCommodity, fetchAvBulkQuotes } from './_shared-av.mjs';
const commodityConfig = loadSharedConfig('commodities.json');
loadEnvFile(import.meta.url);
const CANONICAL_KEY = 'market:commodities-bootstrap:v1';
const GOLD_EXTENDED_KEY = 'market:gold-extended:v1';
const CACHE_TTL = 1800;
const YAHOO_DELAY_MS = 200;
const GOLD_HISTORY_SYMBOLS = ['GC=F', 'SI=F'];
const GOLD_DRIVER_SYMBOLS = [
{ symbol: '^TNX', label: 'US 10Y Yield' },
{ symbol: 'DX-Y.NYB', label: 'DXY' },
];
async function fetchYahooChart1y(symbol) {
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${encodeURIComponent(symbol)}?range=1y&interval=1d`;
try {
const resp = await fetch(url, { headers: { 'User-Agent': CHROME_UA }, signal: AbortSignal.timeout(15_000) });
if (!resp.ok) return null;
const json = await resp.json();
const r = json?.chart?.result?.[0];
if (!r) return null;
const meta = r.meta;
const ts = r.timestamp || [];
const closes = r.indicators?.quote?.[0]?.close || [];
const history = ts.map((t, i) => ({ d: new Date(t * 1000).toISOString().slice(0, 10), c: closes[i] }))
.filter(p => p.c != null && Number.isFinite(p.c));
return {
symbol,
price: meta?.regularMarketPrice ?? null,
dayHigh: meta?.regularMarketDayHigh ?? null,
dayLow: meta?.regularMarketDayLow ?? null,
prevClose: meta?.chartPreviousClose ?? meta?.previousClose ?? null,
fiftyTwoWeekHigh: meta?.fiftyTwoWeekHigh ?? null,
fiftyTwoWeekLow: meta?.fiftyTwoWeekLow ?? null,
history,
};
} catch {
return null;
}
}
function computeReturns(history, currentPrice) {
if (!history.length || !Number.isFinite(currentPrice)) return { w1: 0, m1: 0, ytd: 0, y1: 0 };
const byAgo = (days) => {
const target = history[Math.max(0, history.length - 1 - days)];
return target?.c;
};
const firstOfYear = history.find(p => p.d.startsWith(new Date().getUTCFullYear().toString()))?.c
?? history[0].c;
const pct = (from) => from ? ((currentPrice - from) / from) * 100 : 0;
return {
w1: +pct(byAgo(5)).toFixed(2),
m1: +pct(byAgo(21)).toFixed(2),
ytd: +pct(firstOfYear).toFixed(2),
y1: +pct(history[0].c).toFixed(2),
};
}
function computeRange52w(history, currentPrice) {
if (!history.length) return { hi: 0, lo: 0, positionPct: 0 };
const closes = history.map(p => p.c);
const hi = Math.max(...closes);
const lo = Math.min(...closes);
const span = hi - lo;
const positionPct = span > 0 ? ((currentPrice - lo) / span) * 100 : 50;
return { hi: +hi.toFixed(2), lo: +lo.toFixed(2), positionPct: +positionPct.toFixed(1) };
}
// Pearson correlation over the last N aligned daily returns
function pearsonCorrelation(aReturns, bReturns) {
const n = Math.min(aReturns.length, bReturns.length);
if (n < 5) return 0;
const a = aReturns.slice(-n);
const b = bReturns.slice(-n);
const meanA = a.reduce((s, v) => s + v, 0) / n;
const meanB = b.reduce((s, v) => s + v, 0) / n;
let num = 0, denA = 0, denB = 0;
for (let i = 0; i < n; i++) {
const da = a[i] - meanA;
const db = b[i] - meanB;
num += da * db;
denA += da * da;
denB += db * db;
}
const denom = Math.sqrt(denA * denB);
return denom > 0 ? +(num / denom).toFixed(3) : 0;
}
function dailyReturns(history) {
const out = [];
for (let i = 1; i < history.length; i++) {
const prev = history[i - 1].c;
if (prev > 0) out.push((history[i].c - prev) / prev);
}
return out;
}
async function fetchGoldExtended() {
const goldHistory = {};
for (const sym of GOLD_HISTORY_SYMBOLS) {
await sleep(YAHOO_DELAY_MS);
const chart = await fetchYahooChart1y(sym);
if (chart) goldHistory[sym] = chart;
}
const drivers = [];
const goldReturns = goldHistory['GC=F'] ? dailyReturns(goldHistory['GC=F'].history) : [];
for (const cfg of GOLD_DRIVER_SYMBOLS) {
await sleep(YAHOO_DELAY_MS);
const chart = await fetchYahooChart1y(cfg.symbol);
if (!chart || chart.price == null) continue;
const changePct = chart.prevClose ? ((chart.price - chart.prevClose) / chart.prevClose) * 100 : 0;
const driverReturns = dailyReturns(chart.history).slice(-30);
const goldLast30 = goldReturns.slice(-30);
const correlation = pearsonCorrelation(goldLast30, driverReturns);
drivers.push({
symbol: cfg.symbol,
label: cfg.label,
value: +chart.price.toFixed(2),
changePct: +changePct.toFixed(2),
correlation30d: correlation,
});
}
const gold = goldHistory['GC=F'];
const silver = goldHistory['SI=F'];
const build = (chart) => {
if (!chart || chart.price == null) return null;
return {
price: chart.price,
dayHigh: chart.dayHigh ?? 0,
dayLow: chart.dayLow ?? 0,
prevClose: chart.prevClose ?? 0,
returns: computeReturns(chart.history, chart.price),
range52w: computeRange52w(chart.history, chart.price),
};
};
return {
updatedAt: new Date().toISOString(),
gold: build(gold),
silver: build(silver),
drivers,
};
}
async function fetchYahooWithRetry(url, label, maxAttempts = 4) {
for (let i = 0; i < maxAttempts; i++) {
const resp = await fetch(url, {
headers: { 'User-Agent': CHROME_UA },
signal: AbortSignal.timeout(10_000),
});
if (resp.status === 429) {
const wait = 5000 * (i + 1);
console.warn(` [Yahoo] ${label} 429 — waiting ${wait / 1000}s (attempt ${i + 1}/${maxAttempts})`);
await sleep(wait);
continue;
}
if (!resp.ok) {
console.warn(` [Yahoo] ${label} HTTP ${resp.status}`);
return null;
}
return resp;
}
console.warn(` [Yahoo] ${label} rate limited after ${maxAttempts} attempts`);
return null;
}
const COMMODITY_SYMBOLS = commodityConfig.commodities.map(c => c.symbol);
async function fetchCommodityQuotes() {
const quotes = [];
let misses = 0;
const avKey = process.env.ALPHA_VANTAGE_API_KEY;
// --- Primary: Alpha Vantage ---
if (avKey) {
// Physical commodity functions for WTI, BRENT, NATURAL_GAS, COPPER, ALUMINUM
const physicalSymbols = COMMODITY_SYMBOLS.filter(s => AV_PHYSICAL_MAP[s]);
for (const sym of physicalSymbols) {
const q = await fetchAvPhysicalCommodity(sym, avKey);
if (q) {
const meta = commodityConfig.commodities.find(c => c.symbol === sym);
quotes.push({ symbol: sym, name: meta?.name || sym, display: meta?.display || sym, ...q });
console.log(` [AV:physical] ${sym}: $${q.price} (${q.change > 0 ? '+' : ''}${q.change.toFixed(2)}%)`);
}
}
// REALTIME_BULK_QUOTES for ETF-style symbols (URA, LIT)
const bulkCandidates = COMMODITY_SYMBOLS.filter(s => !AV_PHYSICAL_MAP[s] && !quotes.some(q => q.symbol === s) && !s.includes('=F') && !s.startsWith('^'));
const bulkResults = await fetchAvBulkQuotes(bulkCandidates, avKey);
for (const [sym, q] of bulkResults) {
const meta = commodityConfig.commodities.find(c => c.symbol === sym);
quotes.push({ symbol: sym, name: meta?.name || sym, display: meta?.display || sym, price: q.price, change: q.change, sparkline: [] });
console.log(` [AV:bulk] ${sym}: $${q.price} (${q.change > 0 ? '+' : ''}${q.change.toFixed(2)}%)`);
}
}
const covered = new Set(quotes.map(q => q.symbol));
// --- Fallback: Yahoo (for remaining symbols: futures not covered by AV, ^VIX, Indian markets) ---
let yahooIdx = 0;
for (let i = 0; i < COMMODITY_SYMBOLS.length; i++) {
const symbol = COMMODITY_SYMBOLS[i];
if (covered.has(symbol)) continue;
if (yahooIdx > 0) await sleep(YAHOO_DELAY_MS);
yahooIdx++;
try {
const url = `https://query1.finance.yahoo.com/v8/finance/chart/${encodeURIComponent(symbol)}`;
const resp = await fetchYahooWithRetry(url, symbol);
if (!resp) { misses++; continue; }
const parsed = parseYahooChart(await resp.json(), symbol);
if (parsed) {
quotes.push(parsed);
covered.add(symbol);
console.log(` [Yahoo] ${symbol}: $${parsed.price} (${parsed.change > 0 ? '+' : ''}${parsed.change}%)`);
} else {
misses++;
}
} catch (err) {
console.warn(` [Yahoo] ${symbol} error: ${err.message}`);
misses++;
}
}
if (quotes.length === 0) {
throw new Error(`All commodity fetches failed (${misses} misses)`);
}
return { quotes };
}
function validate(data) {
return Array.isArray(data?.quotes) && data.quotes.length >= 1;
}
let seedData = null;
async function fetchAndStash() {
seedData = await fetchCommodityQuotes();
return seedData;
}
runSeed('market', 'commodities', CANONICAL_KEY, fetchAndStash, {
validateFn: validate,
ttlSeconds: CACHE_TTL,
sourceVersion: 'alphavantage+yahoo-chart',
}).then(async (result) => {
if (result?.skipped || !seedData) return;
const commodityKey = `market:commodities:v1:${[...COMMODITY_SYMBOLS].sort().join(',')}`;
const quotesKey = `market:quotes:v1:${[...COMMODITY_SYMBOLS].sort().join(',')}`;
const quotesPayload = { ...seedData, finnhubSkipped: false, skipReason: '', rateLimited: false };
await writeExtraKey(commodityKey, seedData, CACHE_TTL);
await writeExtraKey(quotesKey, quotesPayload, CACHE_TTL);
try {
const extended = await fetchGoldExtended();
// Require gold (the core metal) AND at least one driver or silver. Writing a
// partial payload would overwrite a healthy prior key with degraded data and
// stamp seed-meta as fresh, masking a broken Yahoo fetch in health checks.
const hasCore = extended.gold != null;
const hasContext = extended.silver != null || extended.drivers.length > 0;
if (hasCore && hasContext) {
const recordCount = (extended.gold ? 1 : 0) + (extended.silver ? 1 : 0) + extended.drivers.length;
await writeExtraKeyWithMeta(GOLD_EXTENDED_KEY, extended, CACHE_TTL, recordCount, 'seed-meta:market:gold-extended');
console.log(` [Gold] extended: gold=${!!extended.gold} silver=${!!extended.silver} drivers=${extended.drivers.length}`);
} else {
// Preserve prior key (if any) and do NOT bump seed-meta — health will flag stale.
console.warn(` [Gold] extended: incomplete (gold=${!!extended.gold} silver=${!!extended.silver} drivers=${extended.drivers.length}) — skipping write, letting seed-meta go stale`);
}
} catch (e) {
console.warn(` [Gold] extended fetch error: ${e?.message || e} — skipping write, letting seed-meta go stale`);
}
}).catch((err) => {
const _cause = err.cause ? ` (cause: ${err.cause.message || err.cause.code || err.cause})` : ''; console.error('FATAL:', (err.message || err) + _cause);
process.exit(1);
});