feat: Add 1W, 1M, 1Y returns to macro dashboard
Build Institutional Trader / build-and-deploy (push) Successful in 2m9s
Details
Build Institutional Trader / build-and-deploy (push) Successful in 2m9s
Details
This commit is contained in:
parent
e43361fadd
commit
7907fa30f2
|
|
@ -30,3 +30,5 @@ EXPOSE 3010
|
||||||
CMD ["node", "src/server.js"]
|
CMD ["node", "src/server.js"]
|
||||||
|
|
||||||
# Cache bust 20260625222919
|
# Cache bust 20260625222919
|
||||||
|
|
||||||
|
# Cache bust 20260625223615
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import NodeCache from 'node-cache';
|
import NodeCache from 'node-cache';
|
||||||
import { UNIVERSE, getSymbolMeta, classifyCapTier } from '../services/stockUniverseService.js';
|
import { UNIVERSE, getSymbolMeta, classifyCapTier } from '../services/stockUniverseService.js';
|
||||||
import { fetchBasicQuote, fetchQuoteSummary, fetchTechnicals, fetchFullAnalysis } from '../services/fundamentalsService.js';
|
import { fetchBasicQuote, fetchQuoteSummary, fetchTechnicals, fetchFullAnalysis, fetchMacroPerformance } from '../services/fundamentalsService.js';
|
||||||
import { fetchInstitutionalHolders, fetchInsiderTransactions } from '../services/institutionalHoldersService.js';
|
import { fetchInstitutionalHolders, fetchInsiderTransactions } from '../services/institutionalHoldersService.js';
|
||||||
import { fetchNewsForSymbol, fetchMarketNews } from '../services/newsService.js';
|
import { fetchNewsForSymbol, fetchMarketNews } from '../services/newsService.js';
|
||||||
import { scoreSuitability } from '../services/scoringEngine.js';
|
import { scoreSuitability } from '../services/scoringEngine.js';
|
||||||
|
|
@ -304,7 +304,7 @@ router.get('/macro', async (req, res) => {
|
||||||
|
|
||||||
const results = await Promise.allSettled(
|
const results = await Promise.allSettled(
|
||||||
MACRO_SYMBOLS.map(async (entry) => {
|
MACRO_SYMBOLS.map(async (entry) => {
|
||||||
const q = await fetchBasicQuote(entry.symbol);
|
const q = await fetchMacroPerformance(entry.symbol);
|
||||||
return q ? { symbol: entry.symbol, name: entry.name, ...q } : null;
|
return q ? { symbol: entry.symbol, name: entry.name, ...q } : null;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -81,6 +81,51 @@ export async function fetchBasicQuote(symbol) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch macro performance (1D, 1W, 1M, 1Y)
|
||||||
|
*/
|
||||||
|
export async function fetchMacroPerformance(symbol) {
|
||||||
|
const cacheKey = `macro_perf_${symbol}`;
|
||||||
|
const cached = fundamentalsCache.get(cacheKey);
|
||||||
|
if (cached) return cached;
|
||||||
|
|
||||||
|
const url = `${YF_BASE}/v8/finance/chart/${symbol}?interval=1d&range=1y`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await yfFetchAuth(url);
|
||||||
|
const result = data?.chart?.result?.[0];
|
||||||
|
if (!result) return null;
|
||||||
|
|
||||||
|
const closes = result.indicators?.quote?.[0]?.close?.filter(v => v != null) || [];
|
||||||
|
if (closes.length === 0) return null;
|
||||||
|
|
||||||
|
const currentPrice = closes[closes.length - 1];
|
||||||
|
|
||||||
|
// Calculate returns safely based on available data length
|
||||||
|
const getRet = (daysBack) => {
|
||||||
|
if (closes.length <= daysBack) return null;
|
||||||
|
const oldPrice = closes[closes.length - 1 - daysBack];
|
||||||
|
if (!oldPrice) return null;
|
||||||
|
return ((currentPrice - oldPrice) / oldPrice) * 100;
|
||||||
|
};
|
||||||
|
|
||||||
|
const q = {
|
||||||
|
symbol: symbol.toUpperCase(),
|
||||||
|
price: currentPrice,
|
||||||
|
change_pct: getRet(1),
|
||||||
|
change_1w: getRet(5),
|
||||||
|
change_1m: getRet(21),
|
||||||
|
change_1y: getRet(closes.length - 1),
|
||||||
|
};
|
||||||
|
|
||||||
|
fundamentalsCache.set(cacheKey, q);
|
||||||
|
return q;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(`[fundamentalsService] macro perf failed for ${symbol}:`, err.message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch full fundamentals via quoteSummary v10
|
* Fetch full fundamentals via quoteSummary v10
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -47,30 +47,51 @@ export default function MacroIndicatorsPanel() {
|
||||||
|
|
||||||
if (data.length === 0) return null;
|
if (data.length === 0) return null;
|
||||||
|
|
||||||
|
function fmtPct(n) {
|
||||||
|
if (n == null) return '—';
|
||||||
|
return (n >= 0 ? '+' : '') + n.toFixed(2) + '%';
|
||||||
|
}
|
||||||
|
|
||||||
|
function getColor(n) {
|
||||||
|
if (n == null) return 'text-slate-500';
|
||||||
|
return n >= 0 ? 'text-emerald-400' : 'text-red-400';
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mb-6">
|
<div className="mb-6">
|
||||||
<div className="text-xs font-bold text-slate-500 mb-3 uppercase tracking-wider pl-1">Global Macro Context</div>
|
<div className="text-xs font-bold text-slate-500 mb-3 uppercase tracking-wider pl-1">Global Macro Context</div>
|
||||||
<div className="flex gap-3 overflow-x-auto pb-4 scrollbar-thin scrollbar-thumb-slate-700 scrollbar-track-transparent">
|
<div className="flex gap-3 overflow-x-auto pb-4 scrollbar-thin scrollbar-thumb-slate-700 scrollbar-track-transparent">
|
||||||
{data.map((item) => {
|
{data.map((item) => (
|
||||||
const isPositive = (item.change_pct ?? 0) >= 0;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div
|
<div
|
||||||
key={item.symbol}
|
key={item.symbol}
|
||||||
className="bg-slate-900 border border-slate-700/50 rounded-xl p-3 min-w-[150px] flex-shrink-0 flex flex-col justify-between hover:bg-slate-800 transition-colors shadow-sm"
|
className="bg-slate-900 border border-slate-700/50 rounded-xl p-3 min-w-[200px] flex-shrink-0 flex flex-col justify-between hover:bg-slate-800 transition-colors shadow-sm"
|
||||||
>
|
>
|
||||||
<div className="text-xs font-semibold text-slate-400 mb-1">{item.name}</div>
|
<div className="flex justify-between items-start mb-2">
|
||||||
<div className="flex items-end justify-between">
|
<div className="text-xs font-semibold text-slate-400">{item.name}</div>
|
||||||
<span className="text-white font-mono font-bold">
|
<span className="text-white font-mono font-bold text-sm">
|
||||||
{fmtPrice(item.price, item.symbol)}
|
{fmtPrice(item.price, item.symbol)}
|
||||||
</span>
|
</span>
|
||||||
<span className={`text-xs font-semibold tabular-nums ${isPositive ? 'text-emerald-400' : 'text-red-400'}`}>
|
</div>
|
||||||
{isPositive ? '+' : ''}{(item.change_pct ?? 0).toFixed(2)}%
|
<div className="grid grid-cols-4 gap-1 text-[10px] text-center">
|
||||||
</span>
|
<div className="flex flex-col bg-slate-800/50 rounded p-1">
|
||||||
|
<span className="text-slate-500 mb-0.5">1D</span>
|
||||||
|
<span className={`font-semibold tabular-nums ${getColor(item.change_pct)}`}>{fmtPct(item.change_pct)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col bg-slate-800/50 rounded p-1">
|
||||||
|
<span className="text-slate-500 mb-0.5">1W</span>
|
||||||
|
<span className={`font-semibold tabular-nums ${getColor(item.change_1w)}`}>{fmtPct(item.change_1w)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col bg-slate-800/50 rounded p-1">
|
||||||
|
<span className="text-slate-500 mb-0.5">1M</span>
|
||||||
|
<span className={`font-semibold tabular-nums ${getColor(item.change_1m)}`}>{fmtPct(item.change_1m)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col bg-slate-800/50 rounded p-1">
|
||||||
|
<span className="text-slate-500 mb-0.5">1Y</span>
|
||||||
|
<span className={`font-semibold tabular-nums ${getColor(item.change_1y)}`}>{fmtPct(item.change_1y)}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
</div>
|
||||||
})}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue