benchmarks

This commit is contained in:
Kazuma
2026-06-02 02:42:41 -04:00
parent 74e6797dcc
commit b9c5dbb7c7
17 changed files with 512 additions and 166 deletions
+45
View File
@@ -0,0 +1,45 @@
import { YahooClient } from './YahooClient.js';
export class BenchmarkProvider {
constructor() {
this.client = new YahooClient();
this.cache = { data: null, expiresAt: 0 };
this.TTL_MS = 60 * 60 * 1000; // Cache for 1 hour
}
async getMarketContext() {
// 1. Return cached data if still valid
if (this.cache.data && Date.now() < this.cache.expiresAt) {
return this.cache.data;
}
try {
const [sp500, tn10y] = await Promise.all([
this.client.fetchSummary('^GSPC'),
this.client.fetchSummary('^TNX'),
]);
const context = {
sp500Price: sp500.price?.regularMarketPrice ?? 0,
riskFreeRate: tn10y.price?.regularMarketPrice ?? 0,
timestamp: new Date().toISOString(),
};
// 2. Validate data sanity (prevent 0-value errors)
if (context.sp500Price === 0 || context.riskFreeRate === 0) {
throw new Error('Invalid market data received (zero values)');
}
// 3. Update cache
this.cache = { data: context, expiresAt: Date.now() + this.TTL_MS };
return context;
} catch (error) {
console.error(
'Market data fetch failed, using last known or empty state:',
error,
);
// If we have stale cache, use it even if expired, otherwise return safe defaults
return this.cache.data || { sp500Price: 4500, riskFreeRate: 4.0 };
}
}
}
+3 -2
View File
@@ -1,10 +1,11 @@
// src/api/YahooClient.js
import YahooFinance from 'yahoo-finance2';
export class YahooClient {
constructor() {
// Instantiate the client as required by v3
this.yf = new YahooFinance();
this.yf = new YahooFinance({
suppressNotices: ['yahooSurvey'],
});
}
async fetchSummary(ticker, retries = 3, backoff = 1000) {