Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d49a738880 | |||
| 6273faa2f9 | |||
| d33c7e0f52 | |||
| 79930c40ac | |||
| 448278243e | |||
| ec3e4dde75 |
34
changelog.md
34
changelog.md
@@ -1,5 +1,39 @@
|
||||
# Changelog
|
||||
|
||||
## 2025-11-01 - 3.2.0 - feat(StockDataService)
|
||||
Add unified StockDataService and BaseProviderService with new stockdata interfaces, provider integrations, tests and README updates
|
||||
|
||||
- Introduce StockDataService: unified API to fetch prices and fundamentals with automatic enrichment and caching
|
||||
- Add IStockData and IStockDataServiceConfig interfaces to define combined price+fundamentals payloads and configuration
|
||||
- Implement BaseProviderService abstraction to share provider registration, health, stats and caching logic
|
||||
- Add classes.stockdataservice.ts implementing batch/single fetch, enrichment, caching, health checks and provider stats
|
||||
- Export new stockdata module and classes from ts/stocks/index.ts
|
||||
- Add comprehensive tests: test/test.stockdata.service.node.ts to cover setup, provider registration, fetching, caching, enrichment, health and error handling
|
||||
- Update README with Unified Stock Data API examples, usage, and documentation reflecting new unified service
|
||||
- Minor infra: add .claude/settings.local.json permissions for local tooling and web fetch domains
|
||||
|
||||
## 2025-11-01 - 3.1.0 - feat(fundamentals)
|
||||
Add FundamentalsService and SEC EDGAR provider with caching, rate-limiting, tests, and docs updates
|
||||
|
||||
- Introduce FundamentalsService to manage fundamentals providers, caching, retry logic and provider statistics
|
||||
- Add SecEdgarProvider to fetch SEC EDGAR company facts (CIK lookup, company facts parsing) with rate limiting and local caches
|
||||
- Expose fundamentals interfaces and services from ts/stocks (exports updated)
|
||||
- Add comprehensive tests for FundamentalsService and SecEdgarProvider (new test files)
|
||||
- Update README with new Fundamentals module documentation, usage examples, and configuration guidance
|
||||
- Implement caching and TTL handling for fundamentals data and provider-specific cache TTL support
|
||||
- Add .claude/settings.local.json (local permissions) and various test improvements
|
||||
|
||||
## 2025-10-31 - 3.0.0 - BREAKING CHANGE(stocks)
|
||||
Unify stock provider API to discriminated IStockDataRequest and add company name/fullname enrichment
|
||||
|
||||
- Replace legacy provider methods (fetchPrice/fetchPrices) with a single fetchData(request: IStockDataRequest) on IStockProvider — providers must be migrated to the new signature.
|
||||
- Migrate StockPriceService to the unified getData(request: IStockDataRequest) API. Convenience helpers getPrice/getPrices now wrap getData.
|
||||
- Add companyName and companyFullName fields to IStockPrice and populate them in provider mappings (Marketstack mapping updated; Yahoo provider updated to support the unified API).
|
||||
- MarketstackProvider: added buildCompanyFullName helper and improved mapping to include company identification fields and full name formatting.
|
||||
- YahooFinanceProvider: updated to implement fetchData and to route current/batch requests through the new unified request types; historical/intraday throw explicit errors.
|
||||
- Updated tests to exercise the new unified API, company-name enrichment, caching behavior, and provider direct methods.
|
||||
- Note: This is a breaking change for external providers and integrations that implemented the old fetchPrice/fetchPrices API. Bump major version.
|
||||
|
||||
## 2025-10-31 - 2.1.0 - feat(stocks)
|
||||
Add unified stock data API (getData) with historical/OHLCV support, smart caching and provider enhancements
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@fin.cx/opendata",
|
||||
"version": "2.1.0",
|
||||
"version": "3.2.0",
|
||||
"private": false,
|
||||
"description": "A comprehensive TypeScript library for accessing business data and real-time financial information. Features include German company data management with MongoDB integration, JSONL bulk processing, automated Handelsregister interactions, and real-time stock market data from multiple providers.",
|
||||
"main": "dist_ts/index.js",
|
||||
|
||||
287
test/test.fundamentals.service.node.ts
Normal file
287
test/test.fundamentals.service.node.ts
Normal file
@@ -0,0 +1,287 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as opendata from '../ts/index.js';
|
||||
|
||||
const TEST_USER_AGENT = 'fin.cx test@fin.cx';
|
||||
|
||||
tap.test('FundamentalsService - Provider Registration', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
await tap.test('should register provider', async () => {
|
||||
service.register(provider);
|
||||
|
||||
const registered = service.getProvider('SEC EDGAR');
|
||||
expect(registered).toBeDefined();
|
||||
expect(registered?.name).toEqual('SEC EDGAR');
|
||||
});
|
||||
|
||||
await tap.test('should get all providers', async () => {
|
||||
const providers = service.getAllProviders();
|
||||
expect(providers.length).toBeGreaterThan(0);
|
||||
expect(providers[0].name).toEqual('SEC EDGAR');
|
||||
});
|
||||
|
||||
await tap.test('should get enabled providers', async () => {
|
||||
const providers = service.getEnabledProviders();
|
||||
expect(providers.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
await tap.test('should unregister provider', async () => {
|
||||
service.unregister('SEC EDGAR');
|
||||
|
||||
const registered = service.getProvider('SEC EDGAR');
|
||||
expect(registered).toBeUndefined();
|
||||
|
||||
// Re-register for other tests
|
||||
service.register(provider);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Fetch Fundamentals', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should fetch fundamentals for single ticker', async () => {
|
||||
const fundamentals = await service.getFundamentals('AAPL');
|
||||
|
||||
expect(fundamentals).toBeDefined();
|
||||
expect(fundamentals.ticker).toEqual('AAPL');
|
||||
expect(fundamentals.companyName).toEqual('Apple Inc.');
|
||||
expect(fundamentals.provider).toEqual('SEC EDGAR');
|
||||
expect(fundamentals.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
expect(fundamentals.sharesOutstanding).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n📊 Fetched via Service:');
|
||||
console.log(` ${fundamentals.ticker}: ${fundamentals.companyName}`);
|
||||
console.log(` EPS: $${fundamentals.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
console.log(` Shares: ${(fundamentals.sharesOutstanding! / 1_000_000_000).toFixed(2)}B`);
|
||||
});
|
||||
|
||||
await tap.test('should fetch fundamentals for multiple tickers', async () => {
|
||||
const fundamentalsList = await service.getBatchFundamentals(['AAPL', 'MSFT']);
|
||||
|
||||
expect(fundamentalsList).toBeInstanceOf(Array);
|
||||
expect(fundamentalsList.length).toEqual(2);
|
||||
|
||||
const apple = fundamentalsList.find(f => f.ticker === 'AAPL');
|
||||
const msft = fundamentalsList.find(f => f.ticker === 'MSFT');
|
||||
|
||||
expect(apple).toBeDefined();
|
||||
expect(msft).toBeDefined();
|
||||
expect(apple!.companyName).toEqual('Apple Inc.');
|
||||
expect(msft!.companyName).toContain('Microsoft');
|
||||
|
||||
console.log('\n📊 Batch Fetch via Service:');
|
||||
fundamentalsList.forEach(f => {
|
||||
console.log(` ${f.ticker}: ${f.companyName} - EPS: $${f.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Caching', async () => {
|
||||
const service = new opendata.FundamentalsService({
|
||||
ttl: 60000, // 60 seconds for testing
|
||||
maxEntries: 100
|
||||
});
|
||||
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should cache fundamentals data', async () => {
|
||||
// Clear cache first
|
||||
service.clearCache();
|
||||
|
||||
let stats = service.getCacheStats();
|
||||
expect(stats.size).toEqual(0);
|
||||
|
||||
// First fetch (should hit API)
|
||||
const start1 = Date.now();
|
||||
await service.getFundamentals('AAPL');
|
||||
const duration1 = Date.now() - start1;
|
||||
|
||||
stats = service.getCacheStats();
|
||||
expect(stats.size).toEqual(1);
|
||||
|
||||
// Second fetch (should hit cache - much faster)
|
||||
const start2 = Date.now();
|
||||
await service.getFundamentals('AAPL');
|
||||
const duration2 = Date.now() - start2;
|
||||
|
||||
expect(duration2).toBeLessThan(duration1);
|
||||
|
||||
console.log('\n⚡ Cache Performance:');
|
||||
console.log(` First fetch: ${duration1}ms`);
|
||||
console.log(` Cached fetch: ${duration2}ms`);
|
||||
console.log(` Speedup: ${Math.round(duration1 / duration2)}x`);
|
||||
});
|
||||
|
||||
await tap.test('should respect cache TTL', async () => {
|
||||
// Set very short TTL
|
||||
service.setCacheTTL(100); // 100ms
|
||||
|
||||
// Fetch and cache
|
||||
await service.getFundamentals('MSFT');
|
||||
|
||||
// Wait for TTL to expire
|
||||
await new Promise(resolve => setTimeout(resolve, 150));
|
||||
|
||||
// This should fetch again (cache expired)
|
||||
const stats = service.getCacheStats();
|
||||
console.log(`\n⏱️ Cache TTL: ${stats.ttl}ms`);
|
||||
});
|
||||
|
||||
await tap.test('should clear cache', async () => {
|
||||
service.clearCache();
|
||||
|
||||
const stats = service.getCacheStats();
|
||||
expect(stats.size).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Price Enrichment', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should enrich fundamentals with price to calculate market cap', async () => {
|
||||
const fundamentals = await service.getFundamentals('AAPL');
|
||||
|
||||
// Simulate current price
|
||||
const currentPrice = 270.37;
|
||||
|
||||
const enriched = await service.enrichWithPrice(fundamentals, currentPrice);
|
||||
|
||||
expect(enriched.marketCap).toBeDefined();
|
||||
expect(enriched.priceToEarnings).toBeDefined();
|
||||
expect(enriched.priceToBook).toBeDefined();
|
||||
|
||||
expect(enriched.marketCap).toBeGreaterThan(0);
|
||||
expect(enriched.priceToEarnings).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n💰 Enriched with Price ($270.37):');
|
||||
console.log(` Market Cap: $${(enriched.marketCap! / 1_000_000_000_000).toFixed(2)}T`);
|
||||
console.log(` P/E Ratio: ${enriched.priceToEarnings!.toFixed(2)}`);
|
||||
console.log(` P/B Ratio: ${enriched.priceToBook?.toFixed(2) || 'N/A'}`);
|
||||
|
||||
// Verify calculations
|
||||
const expectedMarketCap = fundamentals.sharesOutstanding! * currentPrice;
|
||||
expect(Math.abs(enriched.marketCap! - expectedMarketCap)).toBeLessThan(1); // Allow for rounding
|
||||
|
||||
const expectedPE = currentPrice / fundamentals.earningsPerShareDiluted!;
|
||||
expect(Math.abs(enriched.priceToEarnings! - expectedPE)).toBeLessThan(0.01);
|
||||
});
|
||||
|
||||
await tap.test('should enrich batch fundamentals with prices', async () => {
|
||||
const fundamentalsList = await service.getBatchFundamentals(['AAPL', 'MSFT']);
|
||||
|
||||
const priceMap = new Map<string, number>([
|
||||
['AAPL', 270.37],
|
||||
['MSFT', 425.50]
|
||||
]);
|
||||
|
||||
const enriched = await service.enrichBatchWithPrices(fundamentalsList, priceMap);
|
||||
|
||||
expect(enriched.length).toEqual(2);
|
||||
|
||||
const apple = enriched.find(f => f.ticker === 'AAPL')!;
|
||||
const msft = enriched.find(f => f.ticker === 'MSFT')!;
|
||||
|
||||
expect(apple.marketCap).toBeGreaterThan(0);
|
||||
expect(msft.marketCap).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n💰 Batch Enrichment:');
|
||||
console.log(` AAPL: Market Cap $${(apple.marketCap! / 1_000_000_000_000).toFixed(2)}T, P/E ${apple.priceToEarnings!.toFixed(2)}`);
|
||||
console.log(` MSFT: Market Cap $${(msft.marketCap! / 1_000_000_000_000).toFixed(2)}T, P/E ${msft.priceToEarnings!.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Provider Health', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should check provider health', async () => {
|
||||
const health = await service.checkProvidersHealth();
|
||||
|
||||
expect(health.size).toEqual(1);
|
||||
expect(health.get('SEC EDGAR')).toBe(true);
|
||||
|
||||
console.log('\n💚 Provider Health:');
|
||||
health.forEach((isHealthy, name) => {
|
||||
console.log(` ${name}: ${isHealthy ? '✅ Healthy' : '❌ Unhealthy'}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Provider Statistics', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should track provider statistics', async () => {
|
||||
// Make some requests
|
||||
await service.getFundamentals('AAPL');
|
||||
await service.getFundamentals('MSFT');
|
||||
|
||||
const stats = service.getProviderStats();
|
||||
|
||||
expect(stats.size).toEqual(1);
|
||||
|
||||
const secStats = stats.get('SEC EDGAR');
|
||||
expect(secStats).toBeDefined();
|
||||
expect(secStats!.successCount).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n📈 Provider Stats:');
|
||||
console.log(` Success Count: ${secStats!.successCount}`);
|
||||
console.log(` Error Count: ${secStats!.errorCount}`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('FundamentalsService - Error Handling', async () => {
|
||||
const service = new opendata.FundamentalsService();
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.register(provider);
|
||||
|
||||
await tap.test('should throw error for invalid ticker', async () => {
|
||||
try {
|
||||
await service.getFundamentals('INVALIDTICKER123456');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('CIK not found');
|
||||
}
|
||||
});
|
||||
|
||||
await tap.test('should throw error when no providers available', async () => {
|
||||
const emptyService = new opendata.FundamentalsService();
|
||||
|
||||
try {
|
||||
await emptyService.getFundamentals('AAPL');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('No fundamentals providers available');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -151,7 +151,7 @@ tap.test('should handle invalid ticker gracefully', async () => {
|
||||
await stockService.getPrice({ ticker: invalidTicker });
|
||||
throw new Error('Should have thrown an error for invalid ticker');
|
||||
} catch (error) {
|
||||
expect(error.message).toInclude('Failed to fetch price');
|
||||
expect(error.message).toInclude('Failed to fetch');
|
||||
console.log('✓ Invalid ticker handled correctly');
|
||||
}
|
||||
});
|
||||
@@ -215,19 +215,20 @@ tap.test('should test direct provider methods', async () => {
|
||||
expect(available).toEqual(true);
|
||||
console.log(' ✓ isAvailable() returned true');
|
||||
|
||||
// Test fetchPrice directly
|
||||
const price = await marketstackProvider.fetchPrice({ ticker: 'MSFT' });
|
||||
// Test fetchData for single ticker
|
||||
const price = await marketstackProvider.fetchData({ type: 'current', ticker: 'MSFT' }) as opendata.IStockPrice;
|
||||
expect(price.ticker).toEqual('MSFT');
|
||||
expect(price.provider).toEqual('Marketstack');
|
||||
expect(price.price).toBeGreaterThan(0);
|
||||
console.log(` ✓ fetchPrice() for MSFT: $${price.price}`);
|
||||
console.log(` ✓ fetchData (current) for MSFT: $${price.price}`);
|
||||
|
||||
// Test fetchPrices directly
|
||||
const prices = await marketstackProvider.fetchPrices({
|
||||
// Test fetchData for batch
|
||||
const prices = await marketstackProvider.fetchData({
|
||||
type: 'batch',
|
||||
tickers: ['AAPL', 'GOOGL']
|
||||
});
|
||||
}) as opendata.IStockPrice[];
|
||||
expect(prices.length).toBeGreaterThan(0);
|
||||
console.log(` ✓ fetchPrices() returned ${prices.length} prices`);
|
||||
console.log(` ✓ fetchData (batch) returned ${prices.length} prices`);
|
||||
|
||||
for (const p of prices) {
|
||||
console.log(` ${p.ticker}: $${p.price}`);
|
||||
@@ -252,9 +253,10 @@ tap.test('should fetch sample EOD data', async () => {
|
||||
];
|
||||
|
||||
try {
|
||||
const prices = await marketstackProvider.fetchPrices({
|
||||
const prices = await marketstackProvider.fetchData({
|
||||
type: 'batch',
|
||||
tickers: sampleTickers.map(t => t.ticker)
|
||||
});
|
||||
}) as opendata.IStockPrice[];
|
||||
|
||||
const priceMap = new Map(prices.map(p => [p.ticker, p]));
|
||||
|
||||
@@ -452,4 +454,119 @@ tap.test('should verify smart caching with historical data', async () => {
|
||||
console.log(`✓ Speed improvement: ${Math.round((duration1 / duration2) * 10) / 10}x faster`);
|
||||
});
|
||||
|
||||
// Company Name Feature Tests
|
||||
|
||||
tap.test('should include company name in single price request', async () => {
|
||||
if (!marketstackProvider) {
|
||||
console.log('⚠️ Skipping - Marketstack provider not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n🏢 Testing Company Name Feature: Single Request');
|
||||
|
||||
const price = await stockService.getPrice({ ticker: 'AAPL' });
|
||||
|
||||
expect(price.companyName).not.toEqual(undefined);
|
||||
expect(typeof price.companyName).toEqual('string');
|
||||
expect(price.companyName).toInclude('Apple');
|
||||
|
||||
console.log(`✓ Company name retrieved: "${price.companyName}"`);
|
||||
console.log(` Ticker: ${price.ticker}`);
|
||||
console.log(` Price: $${price.price}`);
|
||||
console.log(` Company: ${price.companyName}`);
|
||||
});
|
||||
|
||||
tap.test('should include company names in batch price request', async () => {
|
||||
if (!marketstackProvider) {
|
||||
console.log('⚠️ Skipping - Marketstack provider not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n🏢 Testing Company Name Feature: Batch Request');
|
||||
|
||||
const prices = await stockService.getPrices({
|
||||
tickers: ['AAPL', 'MSFT', 'GOOGL']
|
||||
});
|
||||
|
||||
expect(prices).toBeArray();
|
||||
expect(prices.length).toBeGreaterThan(0);
|
||||
|
||||
console.log(`✓ Fetched ${prices.length} prices with company names:`);
|
||||
|
||||
for (const price of prices) {
|
||||
expect(price.companyName).not.toEqual(undefined);
|
||||
expect(typeof price.companyName).toEqual('string');
|
||||
console.log(` ${price.ticker.padEnd(6)} - ${price.companyName}`);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should include company name in historical data', async () => {
|
||||
if (!marketstackProvider) {
|
||||
console.log('⚠️ Skipping - Marketstack provider not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n🏢 Testing Company Name Feature: Historical Data');
|
||||
|
||||
const prices = await stockService.getData({
|
||||
type: 'historical',
|
||||
ticker: 'TSLA',
|
||||
from: new Date('2025-10-01'),
|
||||
to: new Date('2025-10-05')
|
||||
});
|
||||
|
||||
expect(prices).toBeArray();
|
||||
const historicalPrices = prices as opendata.IStockPrice[];
|
||||
expect(historicalPrices.length).toBeGreaterThan(0);
|
||||
|
||||
// All historical records should have the same company name
|
||||
for (const price of historicalPrices) {
|
||||
expect(price.companyName).not.toEqual(undefined);
|
||||
expect(typeof price.companyName).toEqual('string');
|
||||
}
|
||||
|
||||
const firstPrice = historicalPrices[0];
|
||||
console.log(`✓ Historical records include company name: "${firstPrice.companyName}"`);
|
||||
console.log(` Ticker: ${firstPrice.ticker}`);
|
||||
console.log(` Records: ${historicalPrices.length}`);
|
||||
console.log(` Date range: ${historicalPrices[historicalPrices.length - 1].timestamp.toISOString().split('T')[0]} to ${firstPrice.timestamp.toISOString().split('T')[0]}`);
|
||||
});
|
||||
|
||||
tap.test('should verify company name is included with zero extra API calls', async () => {
|
||||
if (!marketstackProvider) {
|
||||
console.log('⚠️ Skipping - Marketstack provider not initialized');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('\n⚡ Testing Company Name Efficiency: Zero Extra API Calls');
|
||||
|
||||
// Clear cache to ensure we're making fresh API calls
|
||||
stockService.clearCache();
|
||||
|
||||
// Single request timing
|
||||
const start1 = Date.now();
|
||||
const singlePrice = await stockService.getPrice({ ticker: 'AMZN' });
|
||||
const duration1 = Date.now() - start1;
|
||||
|
||||
expect(singlePrice.companyName).not.toEqual(undefined);
|
||||
|
||||
// Batch request timing
|
||||
stockService.clearCache();
|
||||
const start2 = Date.now();
|
||||
const batchPrices = await stockService.getPrices({ tickers: ['NVDA', 'AMD', 'INTC'] });
|
||||
const duration2 = Date.now() - start2;
|
||||
|
||||
for (const price of batchPrices) {
|
||||
expect(price.companyName).not.toEqual(undefined);
|
||||
}
|
||||
|
||||
console.log(`✓ Single request (with company name): ${duration1}ms`);
|
||||
console.log(`✓ Batch request (with company names): ${duration2}ms`);
|
||||
console.log(`✓ Company names included in standard EOD response - zero extra calls!`);
|
||||
console.log(` Single: ${singlePrice.ticker} - "${singlePrice.companyName}"`);
|
||||
for (const price of batchPrices) {
|
||||
console.log(` Batch: ${price.ticker} - "${price.companyName}"`);
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
|
||||
261
test/test.secedgar.provider.node.ts
Normal file
261
test/test.secedgar.provider.node.ts
Normal file
@@ -0,0 +1,261 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as opendata from '../ts/index.js';
|
||||
|
||||
// Test configuration
|
||||
const TEST_USER_AGENT = 'fin.cx test@fin.cx';
|
||||
const TEST_TICKER = 'AAPL'; // Apple Inc - well-known test case
|
||||
const RATE_LIMIT_DELAY = 150; // 150ms between requests (< 10 req/sec)
|
||||
|
||||
tap.test('SEC EDGAR Provider - Constructor', async () => {
|
||||
await tap.test('should create provider with valid User-Agent', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
expect(provider.name).toEqual('SEC EDGAR');
|
||||
expect(provider.priority).toEqual(100);
|
||||
expect(provider.requiresAuth).toBe(false);
|
||||
expect(provider.rateLimit?.requestsPerMinute).toEqual(600);
|
||||
});
|
||||
|
||||
await tap.test('should throw error if User-Agent is missing', async () => {
|
||||
expect(() => {
|
||||
new opendata.SecEdgarProvider({
|
||||
userAgent: ''
|
||||
});
|
||||
}).toThrow('User-Agent is required');
|
||||
});
|
||||
|
||||
await tap.test('should throw error if User-Agent format is invalid', async () => {
|
||||
expect(() => {
|
||||
new opendata.SecEdgarProvider({
|
||||
userAgent: 'InvalidFormat'
|
||||
});
|
||||
}).toThrow('Invalid User-Agent format');
|
||||
});
|
||||
|
||||
await tap.test('should accept valid User-Agent with space and email', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: 'MyCompany contact@example.com'
|
||||
});
|
||||
|
||||
expect(provider).toBeInstanceOf(opendata.SecEdgarProvider);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - Availability', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
await tap.test('should report as available', async () => {
|
||||
const isAvailable = await provider.isAvailable();
|
||||
expect(isAvailable).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - Fetch Fundamentals', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT,
|
||||
timeout: 30000
|
||||
});
|
||||
|
||||
await tap.test('should fetch fundamentals for Apple (AAPL)', async () => {
|
||||
const fundamentals = await provider.fetchData({
|
||||
type: 'fundamentals-current',
|
||||
ticker: TEST_TICKER
|
||||
});
|
||||
|
||||
// Verify structure
|
||||
expect(fundamentals).toBeDefined();
|
||||
expect(fundamentals).not.toBeInstanceOf(Array);
|
||||
|
||||
const data = fundamentals as opendata.IStockFundamentals;
|
||||
|
||||
// Basic fields
|
||||
expect(data.ticker).toEqual('AAPL');
|
||||
expect(data.cik).toBeDefined();
|
||||
expect(data.companyName).toEqual('Apple Inc.');
|
||||
expect(data.provider).toEqual('SEC EDGAR');
|
||||
expect(data.timestamp).toBeInstanceOf(Date);
|
||||
expect(data.fetchedAt).toBeInstanceOf(Date);
|
||||
|
||||
// Financial metrics (Apple should have all of these)
|
||||
expect(data.earningsPerShareDiluted).toBeDefined();
|
||||
expect(data.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
|
||||
expect(data.sharesOutstanding).toBeDefined();
|
||||
expect(data.sharesOutstanding).toBeGreaterThan(0);
|
||||
|
||||
expect(data.revenue).toBeDefined();
|
||||
expect(data.revenue).toBeGreaterThan(0);
|
||||
|
||||
expect(data.netIncome).toBeDefined();
|
||||
expect(data.assets).toBeDefined();
|
||||
expect(data.assets).toBeGreaterThan(0);
|
||||
|
||||
expect(data.liabilities).toBeDefined();
|
||||
expect(data.stockholdersEquity).toBeDefined();
|
||||
|
||||
// Metadata
|
||||
expect(data.fiscalYear).toBeDefined();
|
||||
|
||||
console.log('\n📊 Sample Apple Fundamentals:');
|
||||
console.log(` Company: ${data.companyName} (CIK: ${data.cik})`);
|
||||
console.log(` EPS (Diluted): $${data.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
console.log(` Shares Outstanding: ${(data.sharesOutstanding! / 1_000_000_000).toFixed(2)}B`);
|
||||
console.log(` Revenue: $${(data.revenue! / 1_000_000_000).toFixed(2)}B`);
|
||||
console.log(` Net Income: $${(data.netIncome! / 1_000_000_000).toFixed(2)}B`);
|
||||
console.log(` Assets: $${(data.assets! / 1_000_000_000).toFixed(2)}B`);
|
||||
console.log(` Fiscal Year: ${data.fiscalYear}`);
|
||||
});
|
||||
|
||||
await tap.test('should throw error for invalid ticker', async () => {
|
||||
try {
|
||||
await provider.fetchData({
|
||||
type: 'fundamentals-current',
|
||||
ticker: 'INVALIDTICKER123456'
|
||||
});
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('CIK not found');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - Batch Fetch', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT,
|
||||
timeout: 30000
|
||||
});
|
||||
|
||||
await tap.test('should fetch fundamentals for multiple tickers', async () => {
|
||||
const result = await provider.fetchData({
|
||||
type: 'fundamentals-batch',
|
||||
tickers: ['AAPL', 'MSFT']
|
||||
});
|
||||
|
||||
expect(result).toBeInstanceOf(Array);
|
||||
|
||||
const fundamentalsList = result as opendata.IStockFundamentals[];
|
||||
expect(fundamentalsList.length).toEqual(2);
|
||||
|
||||
// Check Apple
|
||||
const apple = fundamentalsList.find(f => f.ticker === 'AAPL');
|
||||
expect(apple).toBeDefined();
|
||||
expect(apple!.companyName).toEqual('Apple Inc.');
|
||||
expect(apple!.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
|
||||
// Check Microsoft
|
||||
const microsoft = fundamentalsList.find(f => f.ticker === 'MSFT');
|
||||
expect(microsoft).toBeDefined();
|
||||
expect(microsoft!.companyName).toContain('Microsoft');
|
||||
expect(microsoft!.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n📊 Batch Fetch Results:');
|
||||
fundamentalsList.forEach(f => {
|
||||
console.log(` ${f.ticker}: ${f.companyName} - EPS: $${f.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - CIK Caching', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
await tap.test('should cache CIK lookups', async () => {
|
||||
// Clear cache first
|
||||
provider.clearCache();
|
||||
|
||||
let stats = provider.getCacheStats();
|
||||
expect(stats.cikCacheSize).toEqual(0);
|
||||
|
||||
// First fetch (should populate cache)
|
||||
await provider.fetchData({
|
||||
type: 'fundamentals-current',
|
||||
ticker: 'AAPL'
|
||||
});
|
||||
|
||||
stats = provider.getCacheStats();
|
||||
expect(stats.cikCacheSize).toBeGreaterThan(0);
|
||||
|
||||
console.log(`\n💾 CIK Cache: ${stats.cikCacheSize} entries`);
|
||||
});
|
||||
|
||||
await tap.test('should clear cache', async () => {
|
||||
provider.clearCache();
|
||||
|
||||
const stats = provider.getCacheStats();
|
||||
expect(stats.cikCacheSize).toEqual(0);
|
||||
expect(stats.hasTickerList).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - Rate Limiting', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
await tap.test('should handle multiple rapid requests without exceeding rate limit', async () => {
|
||||
// Make 5 requests in succession
|
||||
// Rate limiter should ensure we don't exceed 10 req/sec
|
||||
const tickers = ['AAPL', 'MSFT', 'GOOGL', 'AMZN', 'META'];
|
||||
const startTime = Date.now();
|
||||
|
||||
const promises = tickers.map(ticker =>
|
||||
provider.fetchData({
|
||||
type: 'fundamentals-current',
|
||||
ticker
|
||||
})
|
||||
);
|
||||
|
||||
const results = await Promise.all(promises);
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
expect(results.length).toEqual(5);
|
||||
console.log(`\n⏱️ 5 requests completed in ${duration}ms (avg: ${Math.round(duration / 5)}ms/request)`);
|
||||
|
||||
// Verify all results are valid
|
||||
results.forEach((result, index) => {
|
||||
expect(result).toBeDefined();
|
||||
const data = result as opendata.IStockFundamentals;
|
||||
expect(data.ticker).toEqual(tickers[index]);
|
||||
expect(data.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('SEC EDGAR Provider - Market Cap Calculation', async () => {
|
||||
const provider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
await tap.test('should provide data needed for market cap calculation', async () => {
|
||||
const fundamentals = await provider.fetchData({
|
||||
type: 'fundamentals-current',
|
||||
ticker: 'AAPL'
|
||||
}) as opendata.IStockFundamentals;
|
||||
|
||||
expect(fundamentals.sharesOutstanding).toBeDefined();
|
||||
expect(fundamentals.earningsPerShareDiluted).toBeDefined();
|
||||
|
||||
// Simulate current price (in real usage, this comes from price provider)
|
||||
const simulatedPrice = 270.37;
|
||||
|
||||
// Calculate market cap
|
||||
const marketCap = fundamentals.sharesOutstanding! * simulatedPrice;
|
||||
const pe = simulatedPrice / fundamentals.earningsPerShareDiluted!;
|
||||
|
||||
console.log('\n💰 Calculated Metrics (with simulated price $270.37):');
|
||||
console.log(` Shares Outstanding: ${(fundamentals.sharesOutstanding! / 1_000_000_000).toFixed(2)}B`);
|
||||
console.log(` Market Cap: $${(marketCap / 1_000_000_000_000).toFixed(2)}T`);
|
||||
console.log(` EPS: $${fundamentals.earningsPerShareDiluted!.toFixed(2)}`);
|
||||
console.log(` P/E Ratio: ${pe.toFixed(2)}`);
|
||||
|
||||
expect(marketCap).toBeGreaterThan(0);
|
||||
expect(pe).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
418
test/test.stockdata.service.node.ts
Normal file
418
test/test.stockdata.service.node.ts
Normal file
@@ -0,0 +1,418 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as opendata from '../ts/index.js';
|
||||
|
||||
const TEST_USER_AGENT = 'fin.cx test@fin.cx';
|
||||
|
||||
tap.test('StockDataService - Basic Setup', async () => {
|
||||
await tap.test('should create StockDataService instance', async () => {
|
||||
const service = new opendata.StockDataService({
|
||||
cache: {
|
||||
priceTTL: 60000, // 1 minute for testing
|
||||
fundamentalsTTL: 120000, // 2 minutes for testing
|
||||
maxEntries: 100
|
||||
}
|
||||
});
|
||||
|
||||
expect(service).toBeInstanceOf(opendata.StockDataService);
|
||||
|
||||
const stats = service.getCacheStats();
|
||||
expect(stats.priceCache.ttl).toEqual(60000);
|
||||
expect(stats.fundamentalsCache.ttl).toEqual(120000);
|
||||
expect(stats.maxEntries).toEqual(100);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Provider Registration', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
await tap.test('should register price provider', async () => {
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
|
||||
const providers = service.getPriceProviders();
|
||||
expect(providers.length).toEqual(1);
|
||||
expect(providers[0].name).toEqual('Yahoo Finance');
|
||||
});
|
||||
|
||||
await tap.test('should register fundamentals provider', async () => {
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
const providers = service.getFundamentalsProviders();
|
||||
expect(providers.length).toEqual(1);
|
||||
expect(providers[0].name).toEqual('SEC EDGAR');
|
||||
});
|
||||
|
||||
await tap.test('should unregister providers', async () => {
|
||||
service.unregisterPriceProvider('Yahoo Finance');
|
||||
service.unregisterFundamentalsProvider('SEC EDGAR');
|
||||
|
||||
expect(service.getPriceProviders().length).toEqual(0);
|
||||
expect(service.getFundamentalsProviders().length).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Price Fetching', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
|
||||
await tap.test('should fetch single price', async () => {
|
||||
const price = await service.getPrice('AAPL');
|
||||
|
||||
expect(price).toBeDefined();
|
||||
expect(price.ticker).toEqual('AAPL');
|
||||
expect(price.price).toBeGreaterThan(0);
|
||||
expect(price.provider).toEqual('Yahoo Finance');
|
||||
expect(price.timestamp).toBeInstanceOf(Date);
|
||||
|
||||
console.log(`\n💵 Single Price: ${price.ticker} = $${price.price.toFixed(2)}`);
|
||||
});
|
||||
|
||||
await tap.test('should fetch batch prices', async () => {
|
||||
const prices = await service.getPrices(['AAPL', 'MSFT', 'GOOGL']);
|
||||
|
||||
expect(prices).toBeInstanceOf(Array);
|
||||
expect(prices.length).toBeGreaterThan(0);
|
||||
expect(prices.length).toBeLessThanOrEqual(3);
|
||||
|
||||
console.log('\n💵 Batch Prices:');
|
||||
prices.forEach(p => {
|
||||
console.log(` ${p.ticker}: $${p.price.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should cache prices', async () => {
|
||||
// Clear cache
|
||||
service.clearCache();
|
||||
|
||||
const stats1 = service.getCacheStats();
|
||||
expect(stats1.priceCache.size).toEqual(0);
|
||||
|
||||
// Fetch price (should hit API)
|
||||
const start1 = Date.now();
|
||||
await service.getPrice('AAPL');
|
||||
const duration1 = Date.now() - start1;
|
||||
|
||||
const stats2 = service.getCacheStats();
|
||||
expect(stats2.priceCache.size).toEqual(1);
|
||||
|
||||
// Fetch again (should hit cache - much faster)
|
||||
const start2 = Date.now();
|
||||
await service.getPrice('AAPL');
|
||||
const duration2 = Date.now() - start2;
|
||||
|
||||
expect(duration2).toBeLessThan(duration1);
|
||||
|
||||
console.log('\n⚡ Cache Performance:');
|
||||
console.log(` First fetch: ${duration1}ms`);
|
||||
console.log(` Cached fetch: ${duration2}ms`);
|
||||
console.log(` Speedup: ${Math.round(duration1 / duration2)}x`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Fundamentals Fetching', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
await tap.test('should fetch single fundamentals', async () => {
|
||||
const fundamentals = await service.getFundamentals('AAPL');
|
||||
|
||||
expect(fundamentals).toBeDefined();
|
||||
expect(fundamentals.ticker).toEqual('AAPL');
|
||||
expect(fundamentals.companyName).toEqual('Apple Inc.');
|
||||
expect(fundamentals.provider).toEqual('SEC EDGAR');
|
||||
expect(fundamentals.earningsPerShareDiluted).toBeGreaterThan(0);
|
||||
expect(fundamentals.sharesOutstanding).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n📊 Single Fundamentals:');
|
||||
console.log(` ${fundamentals.ticker}: ${fundamentals.companyName}`);
|
||||
console.log(` EPS: $${fundamentals.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
console.log(` Shares: ${(fundamentals.sharesOutstanding! / 1_000_000_000).toFixed(2)}B`);
|
||||
});
|
||||
|
||||
await tap.test('should fetch batch fundamentals', async () => {
|
||||
const fundamentals = await service.getBatchFundamentals(['AAPL', 'MSFT']);
|
||||
|
||||
expect(fundamentals).toBeInstanceOf(Array);
|
||||
expect(fundamentals.length).toEqual(2);
|
||||
|
||||
console.log('\n📊 Batch Fundamentals:');
|
||||
fundamentals.forEach(f => {
|
||||
console.log(` ${f.ticker}: ${f.companyName} - EPS: $${f.earningsPerShareDiluted?.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should cache fundamentals', async () => {
|
||||
// Clear cache
|
||||
service.clearCache();
|
||||
|
||||
const stats1 = service.getCacheStats();
|
||||
expect(stats1.fundamentalsCache.size).toEqual(0);
|
||||
|
||||
// Fetch fundamentals (should hit API)
|
||||
await service.getFundamentals('AAPL');
|
||||
|
||||
const stats2 = service.getCacheStats();
|
||||
expect(stats2.fundamentalsCache.size).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Complete Stock Data', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
// Register both providers
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
await tap.test('should fetch complete stock data with string', async () => {
|
||||
const data = await service.getStockData('AAPL');
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data.ticker).toEqual('AAPL');
|
||||
expect(data.price).toBeDefined();
|
||||
expect(data.price.ticker).toEqual('AAPL');
|
||||
expect(data.fundamentals).toBeDefined();
|
||||
expect(data.fundamentals?.ticker).toEqual('AAPL');
|
||||
expect(data.fetchedAt).toBeInstanceOf(Date);
|
||||
|
||||
// Check automatic enrichment
|
||||
expect(data.fundamentals?.marketCap).toBeDefined();
|
||||
expect(data.fundamentals?.priceToEarnings).toBeDefined();
|
||||
expect(data.fundamentals?.marketCap).toBeGreaterThan(0);
|
||||
expect(data.fundamentals?.priceToEarnings).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n✨ Complete Stock Data (Auto-Enriched):');
|
||||
console.log(` ${data.ticker}: ${data.fundamentals?.companyName}`);
|
||||
console.log(` Price: $${data.price.price.toFixed(2)}`);
|
||||
console.log(` Market Cap: $${(data.fundamentals!.marketCap! / 1_000_000_000_000).toFixed(2)}T`);
|
||||
console.log(` P/E Ratio: ${data.fundamentals!.priceToEarnings!.toFixed(2)}`);
|
||||
});
|
||||
|
||||
await tap.test('should fetch complete stock data with request object', async () => {
|
||||
const data = await service.getStockData({
|
||||
ticker: 'MSFT',
|
||||
includeFundamentals: true,
|
||||
enrichFundamentals: true
|
||||
});
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data.ticker).toEqual('MSFT');
|
||||
expect(data.price).toBeDefined();
|
||||
expect(data.fundamentals).toBeDefined();
|
||||
expect(data.fundamentals?.marketCap).toBeDefined();
|
||||
expect(data.fundamentals?.priceToEarnings).toBeDefined();
|
||||
});
|
||||
|
||||
await tap.test('should fetch complete stock data without fundamentals', async () => {
|
||||
const data = await service.getStockData({
|
||||
ticker: 'GOOGL',
|
||||
includeFundamentals: false
|
||||
});
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data.ticker).toEqual('GOOGL');
|
||||
expect(data.price).toBeDefined();
|
||||
expect(data.fundamentals).toBeUndefined();
|
||||
});
|
||||
|
||||
await tap.test('should handle fundamentals fetch failure gracefully', async () => {
|
||||
// Try a ticker that might not have fundamentals
|
||||
const data = await service.getStockData({
|
||||
ticker: 'BTC-USD', // Crypto - no SEC filings
|
||||
includeFundamentals: true
|
||||
});
|
||||
|
||||
expect(data).toBeDefined();
|
||||
expect(data.price).toBeDefined();
|
||||
// Fundamentals might be undefined due to error
|
||||
console.log(`\n⚠️ ${data.ticker} - Price available, Fundamentals: ${data.fundamentals ? 'Yes' : 'No'}`);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Batch Complete Stock Data', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
await tap.test('should fetch batch complete data with array', async () => {
|
||||
const data = await service.getBatchStockData(['AAPL', 'MSFT']);
|
||||
|
||||
expect(data).toBeInstanceOf(Array);
|
||||
expect(data.length).toEqual(2);
|
||||
|
||||
data.forEach(stock => {
|
||||
expect(stock.ticker).toBeDefined();
|
||||
expect(stock.price).toBeDefined();
|
||||
expect(stock.fundamentals).toBeDefined();
|
||||
expect(stock.fundamentals?.marketCap).toBeGreaterThan(0);
|
||||
expect(stock.fundamentals?.priceToEarnings).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
console.log('\n✨ Batch Complete Data:');
|
||||
data.forEach(stock => {
|
||||
console.log(` ${stock.ticker}: Price $${stock.price.price.toFixed(2)}, P/E ${stock.fundamentals!.priceToEarnings!.toFixed(2)}`);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should fetch batch complete data with request object', async () => {
|
||||
const data = await service.getBatchStockData({
|
||||
tickers: ['AAPL', 'GOOGL'],
|
||||
includeFundamentals: true,
|
||||
enrichFundamentals: true
|
||||
});
|
||||
|
||||
expect(data).toBeInstanceOf(Array);
|
||||
expect(data.length).toEqual(2);
|
||||
|
||||
data.forEach(stock => {
|
||||
expect(stock.fundamentals?.marketCap).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should fetch batch without enrichment', async () => {
|
||||
const data = await service.getBatchStockData({
|
||||
tickers: ['AAPL', 'MSFT'],
|
||||
includeFundamentals: true,
|
||||
enrichFundamentals: false
|
||||
});
|
||||
|
||||
expect(data).toBeInstanceOf(Array);
|
||||
|
||||
// Check that fundamentals exist but enrichment might not be complete
|
||||
data.forEach(stock => {
|
||||
if (stock.fundamentals) {
|
||||
expect(stock.fundamentals.ticker).toBeDefined();
|
||||
expect(stock.fundamentals.companyName).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Health & Statistics', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
await tap.test('should check providers health', async () => {
|
||||
const health = await service.checkProvidersHealth();
|
||||
|
||||
expect(health.size).toEqual(2);
|
||||
expect(health.get('Yahoo Finance (price)')).toBe(true);
|
||||
expect(health.get('SEC EDGAR (fundamentals)')).toBe(true);
|
||||
|
||||
console.log('\n💚 Provider Health:');
|
||||
health.forEach((isHealthy, name) => {
|
||||
console.log(` ${name}: ${isHealthy ? '✅ Healthy' : '❌ Unhealthy'}`);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should track provider statistics', async () => {
|
||||
// Make some requests to generate stats
|
||||
await service.getPrice('AAPL');
|
||||
await service.getFundamentals('AAPL');
|
||||
|
||||
const stats = service.getProviderStats();
|
||||
|
||||
expect(stats.size).toEqual(2);
|
||||
|
||||
const yahooStats = stats.get('Yahoo Finance');
|
||||
expect(yahooStats).toBeDefined();
|
||||
expect(yahooStats!.type).toEqual('price');
|
||||
expect(yahooStats!.successCount).toBeGreaterThan(0);
|
||||
|
||||
const secStats = stats.get('SEC EDGAR');
|
||||
expect(secStats).toBeDefined();
|
||||
expect(secStats!.type).toEqual('fundamentals');
|
||||
expect(secStats!.successCount).toBeGreaterThan(0);
|
||||
|
||||
console.log('\n📈 Provider Statistics:');
|
||||
stats.forEach((stat, name) => {
|
||||
console.log(` ${name} (${stat.type}): Success=${stat.successCount}, Errors=${stat.errorCount}`);
|
||||
});
|
||||
});
|
||||
|
||||
await tap.test('should clear all caches', async () => {
|
||||
service.clearCache();
|
||||
|
||||
const stats = service.getCacheStats();
|
||||
expect(stats.priceCache.size).toEqual(0);
|
||||
expect(stats.fundamentalsCache.size).toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
tap.test('StockDataService - Error Handling', async () => {
|
||||
await tap.test('should throw error when no price provider available', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
try {
|
||||
await service.getPrice('AAPL');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('No price providers available');
|
||||
}
|
||||
});
|
||||
|
||||
await tap.test('should throw error when no fundamentals provider available', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
|
||||
try {
|
||||
await service.getFundamentals('AAPL');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('No fundamentals providers available');
|
||||
}
|
||||
});
|
||||
|
||||
await tap.test('should handle invalid ticker for price', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
const yahooProvider = new opendata.YahooFinanceProvider();
|
||||
service.registerPriceProvider(yahooProvider);
|
||||
|
||||
try {
|
||||
await service.getPrice('INVALIDTICKER123456');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('Failed to fetch price');
|
||||
}
|
||||
});
|
||||
|
||||
await tap.test('should handle invalid ticker for fundamentals', async () => {
|
||||
const service = new opendata.StockDataService();
|
||||
const secProvider = new opendata.SecEdgarProvider({
|
||||
userAgent: TEST_USER_AGENT
|
||||
});
|
||||
service.registerFundamentalsProvider(secProvider);
|
||||
|
||||
try {
|
||||
await service.getFundamentals('INVALIDTICKER123456');
|
||||
throw new Error('Should have thrown error');
|
||||
} catch (error) {
|
||||
expect(error.message).toContain('CIK not found');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@fin.cx/opendata',
|
||||
version: '2.1.0',
|
||||
version: '3.2.0',
|
||||
description: 'A comprehensive TypeScript library for accessing business data and real-time financial information. Features include German company data management with MongoDB integration, JSONL bulk processing, automated Handelsregister interactions, and real-time stock market data from multiple providers.'
|
||||
}
|
||||
|
||||
296
ts/stocks/classes.baseproviderservice.ts
Normal file
296
ts/stocks/classes.baseproviderservice.ts
Normal file
@@ -0,0 +1,296 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
|
||||
/**
|
||||
* Base provider entry for tracking provider state
|
||||
*/
|
||||
export interface IBaseProviderEntry<TProvider> {
|
||||
provider: TProvider;
|
||||
config: IBaseProviderConfig;
|
||||
lastError?: Error;
|
||||
lastErrorTime?: Date;
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base provider configuration
|
||||
*/
|
||||
export interface IBaseProviderConfig {
|
||||
enabled: boolean;
|
||||
priority: number;
|
||||
timeout?: number;
|
||||
retryAttempts?: number;
|
||||
retryDelay?: number;
|
||||
cacheTTL?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base provider interface
|
||||
*/
|
||||
export interface IBaseProvider {
|
||||
name: string;
|
||||
priority: number;
|
||||
isAvailable(): Promise<boolean>;
|
||||
readonly requiresAuth: boolean;
|
||||
readonly rateLimit?: {
|
||||
requestsPerMinute: number;
|
||||
requestsPerDay?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache entry for any data type
|
||||
*/
|
||||
export interface IBaseCacheEntry<TData> {
|
||||
data: TData;
|
||||
timestamp: Date;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base service for managing data providers with caching
|
||||
* Shared logic extracted from StockPriceService and FundamentalsService
|
||||
*/
|
||||
export abstract class BaseProviderService<TProvider extends IBaseProvider, TData> {
|
||||
protected providers = new Map<string, IBaseProviderEntry<TProvider>>();
|
||||
protected cache = new Map<string, IBaseCacheEntry<TData>>();
|
||||
protected logger = console;
|
||||
|
||||
protected cacheConfig = {
|
||||
ttl: 60000, // Default 60 seconds
|
||||
maxEntries: 10000
|
||||
};
|
||||
|
||||
constructor(cacheConfig?: { ttl?: number; maxEntries?: number }) {
|
||||
if (cacheConfig) {
|
||||
this.cacheConfig = { ...this.cacheConfig, ...cacheConfig };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a provider
|
||||
*/
|
||||
public register(provider: TProvider, config?: Partial<IBaseProviderConfig>): void {
|
||||
const defaultConfig: IBaseProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: 30000,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000,
|
||||
cacheTTL: this.cacheConfig.ttl
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.providers.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a provider
|
||||
*/
|
||||
public unregister(providerName: string): void {
|
||||
this.providers.delete(providerName);
|
||||
console.log(`Unregistered provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific provider by name
|
||||
*/
|
||||
public getProvider(name: string): TProvider | undefined {
|
||||
return this.providers.get(name)?.provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered providers
|
||||
*/
|
||||
public getAllProviders(): TProvider[] {
|
||||
return Array.from(this.providers.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled providers sorted by priority
|
||||
*/
|
||||
public getEnabledProviders(): TProvider[] {
|
||||
return Array.from(this.providers.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check health of all providers
|
||||
*/
|
||||
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
|
||||
const health = new Map<string, boolean>();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(name, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(name, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(name, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider statistics
|
||||
*/
|
||||
public getProviderStats(): Map<
|
||||
string,
|
||||
{
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
lastError?: string;
|
||||
lastErrorTime?: Date;
|
||||
}
|
||||
> {
|
||||
const stats = new Map();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
stats.set(name, {
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached data
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.cache.clear();
|
||||
console.log('Cache cleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cache TTL
|
||||
*/
|
||||
public setCacheTTL(ttl: number): void {
|
||||
this.cacheConfig.ttl = ttl;
|
||||
console.log(`Cache TTL set to ${ttl}ms`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
size: number;
|
||||
maxEntries: number;
|
||||
ttl: number;
|
||||
} {
|
||||
return {
|
||||
size: this.cache.size,
|
||||
maxEntries: this.cacheConfig.maxEntries,
|
||||
ttl: this.cacheConfig.ttl
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch with retry logic
|
||||
*/
|
||||
protected async fetchWithRetry<T>(
|
||||
fetchFn: () => Promise<T>,
|
||||
config: IBaseProviderConfig
|
||||
): Promise<T> {
|
||||
const maxAttempts = config.retryAttempts || 1;
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fetchFn();
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
const delay = (config.retryDelay || 1000) * attempt;
|
||||
console.log(`Retry attempt ${attempt} after ${delay}ms`);
|
||||
await plugins.smartdelay.delayFor(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from cache if not expired
|
||||
*/
|
||||
protected getFromCache(key: string): TData | null {
|
||||
const entry = this.cache.get(key);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache entry has expired
|
||||
const age = Date.now() - entry.timestamp.getTime();
|
||||
if (entry.ttl !== Infinity && age > entry.ttl) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cache with TTL
|
||||
*/
|
||||
protected addToCache(key: string, data: TData, ttl?: number): void {
|
||||
// Enforce max entries limit
|
||||
if (this.cache.size >= this.cacheConfig.maxEntries) {
|
||||
// Remove oldest entry
|
||||
const oldestKey = this.cache.keys().next().value;
|
||||
if (oldestKey) {
|
||||
this.cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
|
||||
this.cache.set(key, {
|
||||
data,
|
||||
timestamp: new Date(),
|
||||
ttl: ttl || this.cacheConfig.ttl
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Track successful fetch for provider
|
||||
*/
|
||||
protected trackSuccess(providerName: string): void {
|
||||
const entry = this.providers.get(providerName);
|
||||
if (entry) {
|
||||
entry.successCount++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Track failed fetch for provider
|
||||
*/
|
||||
protected trackError(providerName: string, error: Error): void {
|
||||
const entry = this.providers.get(providerName);
|
||||
if (entry) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error;
|
||||
entry.lastErrorTime = new Date();
|
||||
}
|
||||
}
|
||||
}
|
||||
404
ts/stocks/classes.fundamentalsservice.ts
Normal file
404
ts/stocks/classes.fundamentalsservice.ts
Normal file
@@ -0,0 +1,404 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type {
|
||||
IFundamentalsProvider,
|
||||
IFundamentalsProviderConfig,
|
||||
IFundamentalsProviderRegistry,
|
||||
IStockFundamentals,
|
||||
IFundamentalsRequest
|
||||
} from './interfaces/fundamentals.js';
|
||||
|
||||
interface IProviderEntry {
|
||||
provider: IFundamentalsProvider;
|
||||
config: IFundamentalsProviderConfig;
|
||||
lastError?: Error;
|
||||
lastErrorTime?: Date;
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
}
|
||||
|
||||
interface ICacheEntry {
|
||||
fundamentals: IStockFundamentals | IStockFundamentals[];
|
||||
timestamp: Date;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service for managing fundamental data providers and caching
|
||||
* Parallel to StockPriceService but for fundamental data instead of prices
|
||||
*/
|
||||
export class FundamentalsService implements IFundamentalsProviderRegistry {
|
||||
private providers = new Map<string, IProviderEntry>();
|
||||
private cache = new Map<string, ICacheEntry>();
|
||||
private logger = console;
|
||||
|
||||
private cacheConfig = {
|
||||
ttl: 90 * 24 * 60 * 60 * 1000, // 90 days default (fundamentals change quarterly)
|
||||
maxEntries: 10000
|
||||
};
|
||||
|
||||
constructor(cacheConfig?: { ttl?: number; maxEntries?: number }) {
|
||||
if (cacheConfig) {
|
||||
this.cacheConfig = { ...this.cacheConfig, ...cacheConfig };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a fundamentals provider
|
||||
*/
|
||||
public register(provider: IFundamentalsProvider, config?: IFundamentalsProviderConfig): void {
|
||||
const defaultConfig: IFundamentalsProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: 30000, // Longer timeout for fundamental data
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000,
|
||||
cacheTTL: this.cacheConfig.ttl
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.providers.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered fundamentals provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a provider
|
||||
*/
|
||||
public unregister(providerName: string): void {
|
||||
this.providers.delete(providerName);
|
||||
console.log(`Unregistered fundamentals provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific provider by name
|
||||
*/
|
||||
public getProvider(name: string): IFundamentalsProvider | undefined {
|
||||
return this.providers.get(name)?.provider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered providers
|
||||
*/
|
||||
public getAllProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.providers.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled providers sorted by priority
|
||||
*/
|
||||
public getEnabledProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.providers.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamental data for a single ticker
|
||||
*/
|
||||
public async getFundamentals(ticker: string): Promise<IStockFundamentals> {
|
||||
const result = await this.getData({
|
||||
type: 'fundamentals-current',
|
||||
ticker
|
||||
});
|
||||
return result as IStockFundamentals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamental data for multiple tickers
|
||||
*/
|
||||
public async getBatchFundamentals(tickers: string[]): Promise<IStockFundamentals[]> {
|
||||
const result = await this.getData({
|
||||
type: 'fundamentals-batch',
|
||||
tickers
|
||||
});
|
||||
return result as IStockFundamentals[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified data fetching method
|
||||
*/
|
||||
public async getData(
|
||||
request: IFundamentalsRequest
|
||||
): Promise<IStockFundamentals | IStockFundamentals[]> {
|
||||
const cacheKey = this.getCacheKey(request);
|
||||
const cached = this.getFromCache(cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for ${this.getRequestDescription(request)}`);
|
||||
return cached;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No fundamentals providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.providers.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData(request),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
// Use provider-specific cache TTL or default
|
||||
const ttl = entry.config.cacheTTL || this.cacheConfig.ttl;
|
||||
this.addToCache(cacheKey, result, ttl);
|
||||
|
||||
console.log(`Successfully fetched ${this.getRequestDescription(request)} from ${provider.name}`);
|
||||
return result;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(
|
||||
`Provider ${provider.name} failed for ${this.getRequestDescription(request)}: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch ${this.getRequestDescription(request)} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enrich fundamentals with calculated metrics using current price
|
||||
*/
|
||||
public async enrichWithPrice(
|
||||
fundamentals: IStockFundamentals,
|
||||
price: number
|
||||
): Promise<IStockFundamentals> {
|
||||
const enriched = { ...fundamentals };
|
||||
|
||||
// Calculate market cap: price × shares outstanding
|
||||
if (fundamentals.sharesOutstanding) {
|
||||
enriched.marketCap = price * fundamentals.sharesOutstanding;
|
||||
}
|
||||
|
||||
// Calculate P/E ratio: price / EPS
|
||||
if (fundamentals.earningsPerShareDiluted && fundamentals.earningsPerShareDiluted > 0) {
|
||||
enriched.priceToEarnings = price / fundamentals.earningsPerShareDiluted;
|
||||
}
|
||||
|
||||
// Calculate price-to-book: market cap / stockholders equity
|
||||
if (enriched.marketCap && fundamentals.stockholdersEquity && fundamentals.stockholdersEquity > 0) {
|
||||
enriched.priceToBook = enriched.marketCap / fundamentals.stockholdersEquity;
|
||||
}
|
||||
|
||||
return enriched;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enrich batch fundamentals with prices
|
||||
*/
|
||||
public async enrichBatchWithPrices(
|
||||
fundamentalsList: IStockFundamentals[],
|
||||
priceMap: Map<string, number>
|
||||
): Promise<IStockFundamentals[]> {
|
||||
return Promise.all(
|
||||
fundamentalsList.map(fundamentals => {
|
||||
const price = priceMap.get(fundamentals.ticker);
|
||||
if (price) {
|
||||
return this.enrichWithPrice(fundamentals, price);
|
||||
}
|
||||
return Promise.resolve(fundamentals);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check health of all providers
|
||||
*/
|
||||
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
|
||||
const health = new Map<string, boolean>();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(name, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(name, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(name, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get provider statistics
|
||||
*/
|
||||
public getProviderStats(): Map<
|
||||
string,
|
||||
{
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
lastError?: string;
|
||||
lastErrorTime?: Date;
|
||||
}
|
||||
> {
|
||||
const stats = new Map();
|
||||
|
||||
for (const [name, entry] of this.providers) {
|
||||
stats.set(name, {
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all cached data
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.cache.clear();
|
||||
console.log('Fundamentals cache cleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set cache TTL
|
||||
*/
|
||||
public setCacheTTL(ttl: number): void {
|
||||
this.cacheConfig.ttl = ttl;
|
||||
console.log(`Fundamentals cache TTL set to ${ttl}ms`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
size: number;
|
||||
maxEntries: number;
|
||||
ttl: number;
|
||||
} {
|
||||
return {
|
||||
size: this.cache.size,
|
||||
maxEntries: this.cacheConfig.maxEntries,
|
||||
ttl: this.cacheConfig.ttl
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch with retry logic
|
||||
*/
|
||||
private async fetchWithRetry<T>(
|
||||
fetchFn: () => Promise<T>,
|
||||
config: IFundamentalsProviderConfig
|
||||
): Promise<T> {
|
||||
const maxAttempts = config.retryAttempts || 1;
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fetchFn();
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
const delay = (config.retryDelay || 1000) * attempt;
|
||||
console.log(`Retry attempt ${attempt} after ${delay}ms`);
|
||||
await plugins.smartdelay.delayFor(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate cache key for request
|
||||
*/
|
||||
private getCacheKey(request: IFundamentalsRequest): string {
|
||||
switch (request.type) {
|
||||
case 'fundamentals-current':
|
||||
return `fundamentals:${request.ticker}`;
|
||||
case 'fundamentals-batch':
|
||||
const tickers = request.tickers.sort().join(',');
|
||||
return `fundamentals-batch:${tickers}`;
|
||||
default:
|
||||
return `unknown:${JSON.stringify(request)}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from cache if not expired
|
||||
*/
|
||||
private getFromCache(key: string): IStockFundamentals | IStockFundamentals[] | null {
|
||||
const entry = this.cache.get(key);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache entry has expired
|
||||
const age = Date.now() - entry.timestamp.getTime();
|
||||
if (entry.ttl !== Infinity && age > entry.ttl) {
|
||||
this.cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.fundamentals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cache with TTL
|
||||
*/
|
||||
private addToCache(
|
||||
key: string,
|
||||
fundamentals: IStockFundamentals | IStockFundamentals[],
|
||||
ttl?: number
|
||||
): void {
|
||||
// Enforce max entries limit
|
||||
if (this.cache.size >= this.cacheConfig.maxEntries) {
|
||||
// Remove oldest entry
|
||||
const oldestKey = this.cache.keys().next().value;
|
||||
if (oldestKey) {
|
||||
this.cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
|
||||
this.cache.set(key, {
|
||||
fundamentals,
|
||||
timestamp: new Date(),
|
||||
ttl: ttl || this.cacheConfig.ttl
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get human-readable request description
|
||||
*/
|
||||
private getRequestDescription(request: IFundamentalsRequest): string {
|
||||
switch (request.type) {
|
||||
case 'fundamentals-current':
|
||||
return `fundamentals for ${request.ticker}`;
|
||||
case 'fundamentals-batch':
|
||||
return `fundamentals for ${request.tickers.length} tickers`;
|
||||
default:
|
||||
return 'fundamentals data';
|
||||
}
|
||||
}
|
||||
}
|
||||
647
ts/stocks/classes.stockdataservice.ts
Normal file
647
ts/stocks/classes.stockdataservice.ts
Normal file
@@ -0,0 +1,647 @@
|
||||
import * as plugins from '../plugins.js';
|
||||
import type { IStockProvider, IProviderConfig } from './interfaces/provider.js';
|
||||
import type { IFundamentalsProvider, IFundamentalsProviderConfig, IStockFundamentals } from './interfaces/fundamentals.js';
|
||||
import type { IStockPrice, IStockDataRequest as IPriceRequest } from './interfaces/stockprice.js';
|
||||
import type { IStockData, IStockDataServiceConfig, ICompleteStockDataRequest, ICompleteStockDataBatchRequest } from './interfaces/stockdata.js';
|
||||
|
||||
interface IProviderEntry<T> {
|
||||
provider: T;
|
||||
config: IProviderConfig | IFundamentalsProviderConfig;
|
||||
lastError?: Error;
|
||||
lastErrorTime?: Date;
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
}
|
||||
|
||||
interface ICacheEntry<T> {
|
||||
data: T;
|
||||
timestamp: Date;
|
||||
ttl: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified service for managing both stock prices and fundamentals
|
||||
* Provides automatic enrichment and convenient combined data access
|
||||
*/
|
||||
export class StockDataService {
|
||||
private priceProviders = new Map<string, IProviderEntry<IStockProvider>>();
|
||||
private fundamentalsProviders = new Map<string, IProviderEntry<IFundamentalsProvider>>();
|
||||
|
||||
private priceCache = new Map<string, ICacheEntry<IStockPrice | IStockPrice[]>>();
|
||||
private fundamentalsCache = new Map<string, ICacheEntry<IStockFundamentals | IStockFundamentals[]>>();
|
||||
|
||||
private logger = console;
|
||||
|
||||
private config: Required<IStockDataServiceConfig> = {
|
||||
cache: {
|
||||
priceTTL: 24 * 60 * 60 * 1000, // 24 hours
|
||||
fundamentalsTTL: 90 * 24 * 60 * 60 * 1000, // 90 days
|
||||
maxEntries: 10000
|
||||
},
|
||||
timeout: {
|
||||
price: 10000, // 10 seconds
|
||||
fundamentals: 30000 // 30 seconds
|
||||
}
|
||||
};
|
||||
|
||||
constructor(config?: IStockDataServiceConfig) {
|
||||
if (config) {
|
||||
this.config = {
|
||||
cache: { ...this.config.cache, ...config.cache },
|
||||
timeout: { ...this.config.timeout, ...config.timeout }
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Provider Management ==========
|
||||
|
||||
/**
|
||||
* Register a price provider
|
||||
*/
|
||||
public registerPriceProvider(provider: IStockProvider, config?: IProviderConfig): void {
|
||||
const defaultConfig: IProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: this.config.timeout.price,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.priceProviders.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered price provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a fundamentals provider
|
||||
*/
|
||||
public registerFundamentalsProvider(
|
||||
provider: IFundamentalsProvider,
|
||||
config?: IFundamentalsProviderConfig
|
||||
): void {
|
||||
const defaultConfig: IFundamentalsProviderConfig = {
|
||||
enabled: true,
|
||||
priority: provider.priority,
|
||||
timeout: this.config.timeout.fundamentals,
|
||||
retryAttempts: 2,
|
||||
retryDelay: 1000,
|
||||
cacheTTL: this.config.cache.fundamentalsTTL
|
||||
};
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config };
|
||||
|
||||
this.fundamentalsProviders.set(provider.name, {
|
||||
provider,
|
||||
config: mergedConfig,
|
||||
successCount: 0,
|
||||
errorCount: 0
|
||||
});
|
||||
|
||||
console.log(`Registered fundamentals provider: ${provider.name}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a price provider
|
||||
*/
|
||||
public unregisterPriceProvider(providerName: string): void {
|
||||
this.priceProviders.delete(providerName);
|
||||
console.log(`Unregistered price provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a fundamentals provider
|
||||
*/
|
||||
public unregisterFundamentalsProvider(providerName: string): void {
|
||||
this.fundamentalsProviders.delete(providerName);
|
||||
console.log(`Unregistered fundamentals provider: ${providerName}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered price providers
|
||||
*/
|
||||
public getPriceProviders(): IStockProvider[] {
|
||||
return Array.from(this.priceProviders.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered fundamentals providers
|
||||
*/
|
||||
public getFundamentalsProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.fundamentalsProviders.values()).map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled price providers sorted by priority
|
||||
*/
|
||||
private getEnabledPriceProviders(): IStockProvider[] {
|
||||
return Array.from(this.priceProviders.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enabled fundamentals providers sorted by priority
|
||||
*/
|
||||
private getEnabledFundamentalsProviders(): IFundamentalsProvider[] {
|
||||
return Array.from(this.fundamentalsProviders.values())
|
||||
.filter(entry => entry.config.enabled)
|
||||
.sort((a, b) => (b.config.priority || 0) - (a.config.priority || 0))
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
// ========== Data Fetching Methods ==========
|
||||
|
||||
/**
|
||||
* Get current price for a single ticker
|
||||
*/
|
||||
public async getPrice(ticker: string): Promise<IStockPrice> {
|
||||
const cacheKey = `price:${ticker}`;
|
||||
const cached = this.getFromCache(this.priceCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for price: ${ticker}`);
|
||||
return cached as IStockPrice;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledPriceProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.priceProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'current', ticker }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const price = result as IStockPrice;
|
||||
this.addToCache(this.priceCache, cacheKey, price, this.config.cache.priceTTL);
|
||||
|
||||
console.log(`Successfully fetched price for ${ticker} from ${provider.name}`);
|
||||
return price;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for ${ticker}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch price for ${ticker} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current prices for multiple tickers
|
||||
*/
|
||||
public async getPrices(tickers: string[]): Promise<IStockPrice[]> {
|
||||
const cacheKey = `prices:${tickers.sort().join(',')}`;
|
||||
const cached = this.getFromCache(this.priceCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for prices: ${tickers.length} tickers`);
|
||||
return cached as IStockPrice[];
|
||||
}
|
||||
|
||||
const providers = this.getEnabledPriceProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.priceProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'batch', tickers }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const prices = result as IStockPrice[];
|
||||
this.addToCache(this.priceCache, cacheKey, prices, this.config.cache.priceTTL);
|
||||
|
||||
console.log(`Successfully fetched ${prices.length} prices from ${provider.name}`);
|
||||
return prices;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for batch prices: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch prices for ${tickers.length} tickers from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamentals for a single ticker
|
||||
*/
|
||||
public async getFundamentals(ticker: string): Promise<IStockFundamentals> {
|
||||
const cacheKey = `fundamentals:${ticker}`;
|
||||
const cached = this.getFromCache(this.fundamentalsCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for fundamentals: ${ticker}`);
|
||||
return cached as IStockFundamentals;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledFundamentalsProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No fundamentals providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.fundamentalsProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'fundamentals-current', ticker }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const fundamentals = result as IStockFundamentals;
|
||||
const ttl = (entry.config as IFundamentalsProviderConfig).cacheTTL || this.config.cache.fundamentalsTTL;
|
||||
this.addToCache(this.fundamentalsCache, cacheKey, fundamentals, ttl);
|
||||
|
||||
console.log(`Successfully fetched fundamentals for ${ticker} from ${provider.name}`);
|
||||
return fundamentals;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for ${ticker} fundamentals: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch fundamentals for ${ticker} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get fundamentals for multiple tickers
|
||||
*/
|
||||
public async getBatchFundamentals(tickers: string[]): Promise<IStockFundamentals[]> {
|
||||
const cacheKey = `fundamentals-batch:${tickers.sort().join(',')}`;
|
||||
const cached = this.getFromCache(this.fundamentalsCache, cacheKey);
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for batch fundamentals: ${tickers.length} tickers`);
|
||||
return cached as IStockFundamentals[];
|
||||
}
|
||||
|
||||
const providers = this.getEnabledFundamentalsProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No fundamentals providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.fundamentalsProviders.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => provider.fetchData({ type: 'fundamentals-batch', tickers }),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
const fundamentals = result as IStockFundamentals[];
|
||||
const ttl = (entry.config as IFundamentalsProviderConfig).cacheTTL || this.config.cache.fundamentalsTTL;
|
||||
this.addToCache(this.fundamentalsCache, cacheKey, fundamentals, ttl);
|
||||
|
||||
console.log(`Successfully fetched ${fundamentals.length} fundamentals from ${provider.name}`);
|
||||
return fundamentals;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(`Provider ${provider.name} failed for batch fundamentals: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch fundamentals for ${tickers.length} tickers from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ✨ Get complete stock data (price + fundamentals) with automatic enrichment
|
||||
*/
|
||||
public async getStockData(request: string | ICompleteStockDataRequest): Promise<IStockData> {
|
||||
const normalizedRequest = typeof request === 'string'
|
||||
? { ticker: request, includeFundamentals: true, enrichFundamentals: true }
|
||||
: { includeFundamentals: true, enrichFundamentals: true, ...request };
|
||||
|
||||
const price = await this.getPrice(normalizedRequest.ticker);
|
||||
|
||||
let fundamentals: IStockFundamentals | undefined;
|
||||
|
||||
if (normalizedRequest.includeFundamentals) {
|
||||
try {
|
||||
fundamentals = await this.getFundamentals(normalizedRequest.ticker);
|
||||
|
||||
// Enrich fundamentals with price calculations
|
||||
if (normalizedRequest.enrichFundamentals && fundamentals) {
|
||||
fundamentals = this.enrichWithPrice(fundamentals, price.price);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch fundamentals for ${normalizedRequest.ticker}: ${error.message}`);
|
||||
// Continue without fundamentals
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ticker: normalizedRequest.ticker,
|
||||
price,
|
||||
fundamentals,
|
||||
fetchedAt: new Date()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* ✨ Get complete stock data for multiple tickers with automatic enrichment
|
||||
*/
|
||||
public async getBatchStockData(request: string[] | ICompleteStockDataBatchRequest): Promise<IStockData[]> {
|
||||
const normalizedRequest = Array.isArray(request)
|
||||
? { tickers: request, includeFundamentals: true, enrichFundamentals: true }
|
||||
: { includeFundamentals: true, enrichFundamentals: true, ...request };
|
||||
|
||||
const prices = await this.getPrices(normalizedRequest.tickers);
|
||||
const priceMap = new Map(prices.map(p => [p.ticker, p]));
|
||||
|
||||
let fundamentalsMap = new Map<string, IStockFundamentals>();
|
||||
|
||||
if (normalizedRequest.includeFundamentals) {
|
||||
try {
|
||||
const fundamentals = await this.getBatchFundamentals(normalizedRequest.tickers);
|
||||
|
||||
// Enrich with prices if requested
|
||||
if (normalizedRequest.enrichFundamentals) {
|
||||
for (const fund of fundamentals) {
|
||||
const price = priceMap.get(fund.ticker);
|
||||
if (price) {
|
||||
fundamentalsMap.set(fund.ticker, this.enrichWithPrice(fund, price.price));
|
||||
} else {
|
||||
fundamentalsMap.set(fund.ticker, fund);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
fundamentalsMap = new Map(fundamentals.map(f => [f.ticker, f]));
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch batch fundamentals: ${error.message}`);
|
||||
// Continue without fundamentals
|
||||
}
|
||||
}
|
||||
|
||||
return normalizedRequest.tickers.map(ticker => ({
|
||||
ticker,
|
||||
price: priceMap.get(ticker)!,
|
||||
fundamentals: fundamentalsMap.get(ticker),
|
||||
fetchedAt: new Date()
|
||||
}));
|
||||
}
|
||||
|
||||
// ========== Helper Methods ==========
|
||||
|
||||
/**
|
||||
* Enrich fundamentals with calculated metrics using current price
|
||||
*/
|
||||
private enrichWithPrice(fundamentals: IStockFundamentals, price: number): IStockFundamentals {
|
||||
const enriched = { ...fundamentals };
|
||||
|
||||
// Calculate market cap: price × shares outstanding
|
||||
if (fundamentals.sharesOutstanding) {
|
||||
enriched.marketCap = price * fundamentals.sharesOutstanding;
|
||||
}
|
||||
|
||||
// Calculate P/E ratio: price / EPS
|
||||
if (fundamentals.earningsPerShareDiluted && fundamentals.earningsPerShareDiluted > 0) {
|
||||
enriched.priceToEarnings = price / fundamentals.earningsPerShareDiluted;
|
||||
}
|
||||
|
||||
// Calculate price-to-book: market cap / stockholders equity
|
||||
if (enriched.marketCap && fundamentals.stockholdersEquity && fundamentals.stockholdersEquity > 0) {
|
||||
enriched.priceToBook = enriched.marketCap / fundamentals.stockholdersEquity;
|
||||
}
|
||||
|
||||
return enriched;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch with retry logic
|
||||
*/
|
||||
private async fetchWithRetry<T>(
|
||||
fetchFn: () => Promise<T>,
|
||||
config: IProviderConfig | IFundamentalsProviderConfig
|
||||
): Promise<T> {
|
||||
const maxAttempts = config.retryAttempts || 1;
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
return await fetchFn();
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
if (attempt < maxAttempts) {
|
||||
const delay = (config.retryDelay || 1000) * attempt;
|
||||
console.log(`Retry attempt ${attempt} after ${delay}ms`);
|
||||
await plugins.smartdelay.delayFor(delay);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get from cache if not expired
|
||||
*/
|
||||
private getFromCache<T>(cache: Map<string, ICacheEntry<T>>, key: string): T | null {
|
||||
const entry = cache.get(key);
|
||||
|
||||
if (!entry) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if cache entry has expired
|
||||
const age = Date.now() - entry.timestamp.getTime();
|
||||
if (entry.ttl !== Infinity && age > entry.ttl) {
|
||||
cache.delete(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
return entry.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add to cache with TTL
|
||||
*/
|
||||
private addToCache<T>(cache: Map<string, ICacheEntry<T>>, key: string, data: T, ttl: number): void {
|
||||
// Enforce max entries limit
|
||||
if (cache.size >= this.config.cache.maxEntries) {
|
||||
// Remove oldest entry
|
||||
const oldestKey = cache.keys().next().value;
|
||||
if (oldestKey) {
|
||||
cache.delete(oldestKey);
|
||||
}
|
||||
}
|
||||
|
||||
cache.set(key, {
|
||||
data,
|
||||
timestamp: new Date(),
|
||||
ttl
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Health & Statistics ==========
|
||||
|
||||
/**
|
||||
* Check health of all providers (both price and fundamentals)
|
||||
*/
|
||||
public async checkProvidersHealth(): Promise<Map<string, boolean>> {
|
||||
const health = new Map<string, boolean>();
|
||||
|
||||
// Check price providers
|
||||
for (const [name, entry] of this.priceProviders) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(`${name} (price)`, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(`${name} (price)`, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(`${name} (price)`, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// Check fundamentals providers
|
||||
for (const [name, entry] of this.fundamentalsProviders) {
|
||||
if (!entry.config.enabled) {
|
||||
health.set(`${name} (fundamentals)`, false);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const isAvailable = await entry.provider.isAvailable();
|
||||
health.set(`${name} (fundamentals)`, isAvailable);
|
||||
} catch (error) {
|
||||
health.set(`${name} (fundamentals)`, false);
|
||||
console.error(`Health check failed for ${name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return health;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get statistics for all providers
|
||||
*/
|
||||
public getProviderStats(): Map<
|
||||
string,
|
||||
{
|
||||
type: 'price' | 'fundamentals';
|
||||
successCount: number;
|
||||
errorCount: number;
|
||||
lastError?: string;
|
||||
lastErrorTime?: Date;
|
||||
}
|
||||
> {
|
||||
const stats = new Map();
|
||||
|
||||
// Price provider stats
|
||||
for (const [name, entry] of this.priceProviders) {
|
||||
stats.set(name, {
|
||||
type: 'price',
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
// Fundamentals provider stats
|
||||
for (const [name, entry] of this.fundamentalsProviders) {
|
||||
stats.set(name, {
|
||||
type: 'fundamentals',
|
||||
successCount: entry.successCount,
|
||||
errorCount: entry.errorCount,
|
||||
lastError: entry.lastError?.message,
|
||||
lastErrorTime: entry.lastErrorTime
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all caches
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.priceCache.clear();
|
||||
this.fundamentalsCache.clear();
|
||||
console.log('All caches cleared');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
priceCache: { size: number; ttl: number };
|
||||
fundamentalsCache: { size: number; ttl: number };
|
||||
maxEntries: number;
|
||||
} {
|
||||
return {
|
||||
priceCache: {
|
||||
size: this.priceCache.size,
|
||||
ttl: this.config.cache.priceTTL
|
||||
},
|
||||
fundamentalsCache: {
|
||||
size: this.fundamentalsCache.size,
|
||||
ttl: this.config.cache.fundamentalsTTL
|
||||
},
|
||||
maxEntries: this.config.cache.maxEntries
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,6 @@ import * as plugins from '../plugins.js';
|
||||
import type { IStockProvider, IProviderConfig, IProviderRegistry } from './interfaces/provider.js';
|
||||
import type {
|
||||
IStockPrice,
|
||||
IStockQuoteRequest,
|
||||
IStockBatchQuoteRequest,
|
||||
IStockPriceError,
|
||||
IStockDataRequest,
|
||||
IStockCurrentRequest,
|
||||
@@ -13,6 +11,15 @@ import type {
|
||||
TIntervalType
|
||||
} from './interfaces/stockprice.js';
|
||||
|
||||
// Simple request interfaces for convenience methods
|
||||
interface ISimpleQuoteRequest {
|
||||
ticker: string;
|
||||
}
|
||||
|
||||
interface ISimpleBatchRequest {
|
||||
tickers: string[];
|
||||
}
|
||||
|
||||
interface IProviderEntry {
|
||||
provider: IStockProvider;
|
||||
config: IProviderConfig;
|
||||
@@ -122,132 +129,26 @@ export class StockPriceService implements IProviderRegistry {
|
||||
.map(entry => entry.provider);
|
||||
}
|
||||
|
||||
public async getPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
|
||||
const cacheKey = this.getCacheKey(request);
|
||||
const cached = this.getFromCache(cacheKey) as IStockPrice | null;
|
||||
|
||||
if (cached) {
|
||||
console.log(`Cache hit for ${request.ticker}`);
|
||||
return cached;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No stock price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.providers.get(provider.name)!;
|
||||
|
||||
try {
|
||||
const price = await this.fetchWithRetry(
|
||||
() => provider.fetchPrice(request),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
// Use smart TTL based on data type
|
||||
const ttl = this.getCacheTTL(price.dataType);
|
||||
this.addToCache(cacheKey, price, ttl);
|
||||
|
||||
console.log(`Successfully fetched ${request.ticker} from ${provider.name}`);
|
||||
return price;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(
|
||||
`Provider ${provider.name} failed for ${request.ticker}: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Failed to fetch price for ${request.ticker} from all providers. Last error: ${lastError?.message}`
|
||||
);
|
||||
/**
|
||||
* Convenience method: Get current price for a single ticker
|
||||
*/
|
||||
public async getPrice(request: ISimpleQuoteRequest): Promise<IStockPrice> {
|
||||
const result = await this.getData({
|
||||
type: 'current',
|
||||
ticker: request.ticker
|
||||
});
|
||||
return result as IStockPrice;
|
||||
}
|
||||
|
||||
public async getPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
|
||||
const cachedPrices: IStockPrice[] = [];
|
||||
const tickersToFetch: string[] = [];
|
||||
|
||||
// Check cache for each ticker
|
||||
for (const ticker of request.tickers) {
|
||||
const cacheKey = this.getCacheKey({ ticker, includeExtendedHours: request.includeExtendedHours });
|
||||
const cached = this.getFromCache(cacheKey) as IStockPrice | null;
|
||||
|
||||
if (cached) {
|
||||
cachedPrices.push(cached);
|
||||
} else {
|
||||
tickersToFetch.push(ticker);
|
||||
}
|
||||
}
|
||||
|
||||
if (tickersToFetch.length === 0) {
|
||||
console.log(`All ${request.tickers.length} tickers served from cache`);
|
||||
return cachedPrices;
|
||||
}
|
||||
|
||||
const providers = this.getEnabledProviders();
|
||||
if (providers.length === 0) {
|
||||
throw new Error('No stock price providers available');
|
||||
}
|
||||
|
||||
let lastError: Error | undefined;
|
||||
let fetchedPrices: IStockPrice[] = [];
|
||||
|
||||
for (const provider of providers) {
|
||||
const entry = this.providers.get(provider.name)!;
|
||||
|
||||
try {
|
||||
fetchedPrices = await this.fetchWithRetry(
|
||||
() => provider.fetchPrices({
|
||||
tickers: tickersToFetch,
|
||||
includeExtendedHours: request.includeExtendedHours
|
||||
}),
|
||||
entry.config
|
||||
);
|
||||
|
||||
entry.successCount++;
|
||||
|
||||
// Cache the fetched prices with smart TTL
|
||||
for (const price of fetchedPrices) {
|
||||
const cacheKey = this.getCacheKey({
|
||||
ticker: price.ticker,
|
||||
includeExtendedHours: request.includeExtendedHours
|
||||
});
|
||||
const ttl = this.getCacheTTL(price.dataType);
|
||||
this.addToCache(cacheKey, price, ttl);
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Successfully fetched ${fetchedPrices.length} prices from ${provider.name}`
|
||||
);
|
||||
break;
|
||||
} catch (error) {
|
||||
entry.errorCount++;
|
||||
entry.lastError = error as Error;
|
||||
entry.lastErrorTime = new Date();
|
||||
lastError = error as Error;
|
||||
|
||||
console.warn(
|
||||
`Provider ${provider.name} failed for batch request: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (fetchedPrices.length === 0 && lastError) {
|
||||
throw new Error(
|
||||
`Failed to fetch prices from all providers. Last error: ${lastError.message}`
|
||||
);
|
||||
}
|
||||
|
||||
return [...cachedPrices, ...fetchedPrices];
|
||||
/**
|
||||
* Convenience method: Get current prices for multiple tickers
|
||||
*/
|
||||
public async getPrices(request: ISimpleBatchRequest): Promise<IStockPrice[]> {
|
||||
const result = await this.getData({
|
||||
type: 'batch',
|
||||
tickers: request.tickers
|
||||
});
|
||||
return result as IStockPrice[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,15 +173,9 @@ export class StockPriceService implements IProviderRegistry {
|
||||
for (const provider of providers) {
|
||||
const entry = this.providers.get(provider.name)!;
|
||||
|
||||
// Check if provider supports the new fetchData method
|
||||
if (typeof (provider as any).fetchData !== 'function') {
|
||||
console.warn(`Provider ${provider.name} does not support new API, skipping`);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.fetchWithRetry(
|
||||
() => (provider as any).fetchData(request),
|
||||
() => provider.fetchData(request),
|
||||
entry.config
|
||||
) as IStockPrice | IStockPrice[];
|
||||
|
||||
@@ -420,13 +315,6 @@ export class StockPriceService implements IProviderRegistry {
|
||||
throw lastError || new Error('Unknown error during fetch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy cache key generation
|
||||
*/
|
||||
private getCacheKey(request: IStockQuoteRequest): string {
|
||||
return `${request.ticker}:${request.includeExtendedHours || false}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* New cache key generation for discriminated union requests
|
||||
*/
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
// Export all interfaces
|
||||
export * from './interfaces/stockprice.js';
|
||||
export * from './interfaces/provider.js';
|
||||
export * from './interfaces/fundamentals.js';
|
||||
export * from './interfaces/stockdata.js';
|
||||
|
||||
// Export main service
|
||||
// Export main services
|
||||
export * from './classes.stockservice.js';
|
||||
export * from './classes.fundamentalsservice.js';
|
||||
export * from './classes.stockdataservice.js'; // ✨ New unified service
|
||||
|
||||
// Export base service (for advanced use cases)
|
||||
export * from './classes.baseproviderservice.js';
|
||||
|
||||
// Export providers
|
||||
export * from './providers/provider.yahoo.js';
|
||||
export * from './providers/provider.marketstack.js';
|
||||
export * from './providers/provider.marketstack.js';
|
||||
export * from './providers/provider.secedgar.js';
|
||||
102
ts/stocks/interfaces/fundamentals.ts
Normal file
102
ts/stocks/interfaces/fundamentals.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Interfaces for stock fundamental data (financials from SEC filings)
|
||||
* Separate from stock price data (OHLCV) to maintain clean architecture
|
||||
*/
|
||||
|
||||
// Request types for fundamental data
|
||||
export interface IFundamentalsCurrentRequest {
|
||||
type: 'fundamentals-current';
|
||||
ticker: string;
|
||||
}
|
||||
|
||||
export interface IFundamentalsBatchRequest {
|
||||
type: 'fundamentals-batch';
|
||||
tickers: string[];
|
||||
}
|
||||
|
||||
export type IFundamentalsRequest =
|
||||
| IFundamentalsCurrentRequest
|
||||
| IFundamentalsBatchRequest;
|
||||
|
||||
/**
|
||||
* Stock fundamental data from SEC filings (10-K, 10-Q)
|
||||
* Contains financial metrics like EPS, Revenue, Assets, etc.
|
||||
*/
|
||||
export interface IStockFundamentals {
|
||||
ticker: string;
|
||||
cik: string;
|
||||
companyName: string;
|
||||
provider: string;
|
||||
timestamp: Date;
|
||||
fetchedAt: Date;
|
||||
|
||||
// Per-share metrics
|
||||
earningsPerShareBasic?: number;
|
||||
earningsPerShareDiluted?: number;
|
||||
sharesOutstanding?: number;
|
||||
weightedAverageSharesOutstanding?: number;
|
||||
|
||||
// Income statement (annual USD)
|
||||
revenue?: number;
|
||||
netIncome?: number;
|
||||
operatingIncome?: number;
|
||||
grossProfit?: number;
|
||||
costOfRevenue?: number;
|
||||
|
||||
// Balance sheet (annual USD)
|
||||
assets?: number;
|
||||
liabilities?: number;
|
||||
stockholdersEquity?: number;
|
||||
cash?: number;
|
||||
propertyPlantEquipment?: number;
|
||||
|
||||
// Calculated metrics (requires current price)
|
||||
marketCap?: number; // price × sharesOutstanding
|
||||
priceToEarnings?: number; // price / EPS
|
||||
priceToBook?: number; // marketCap / stockholdersEquity
|
||||
|
||||
// Metadata
|
||||
fiscalYear?: string;
|
||||
fiscalQuarter?: string;
|
||||
filingDate?: Date;
|
||||
form?: '10-K' | '10-Q' | string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider interface for fetching fundamental data
|
||||
* Parallel to IStockProvider but for fundamentals instead of prices
|
||||
*/
|
||||
export interface IFundamentalsProvider {
|
||||
name: string;
|
||||
priority: number;
|
||||
fetchData(request: IFundamentalsRequest): Promise<IStockFundamentals | IStockFundamentals[]>;
|
||||
isAvailable(): Promise<boolean>;
|
||||
readonly requiresAuth: boolean;
|
||||
readonly rateLimit?: {
|
||||
requestsPerMinute: number;
|
||||
requestsPerDay?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for fundamentals providers
|
||||
*/
|
||||
export interface IFundamentalsProviderConfig {
|
||||
enabled: boolean;
|
||||
priority?: number;
|
||||
timeout?: number;
|
||||
retryAttempts?: number;
|
||||
retryDelay?: number;
|
||||
cacheTTL?: number; // Custom cache TTL for this provider
|
||||
}
|
||||
|
||||
/**
|
||||
* Registry for managing fundamental data providers
|
||||
*/
|
||||
export interface IFundamentalsProviderRegistry {
|
||||
register(provider: IFundamentalsProvider, config?: IFundamentalsProviderConfig): void;
|
||||
unregister(providerName: string): void;
|
||||
getProvider(name: string): IFundamentalsProvider | undefined;
|
||||
getAllProviders(): IFundamentalsProvider[];
|
||||
getEnabledProviders(): IFundamentalsProvider[];
|
||||
}
|
||||
@@ -1,16 +1,15 @@
|
||||
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest } from './stockprice.js';
|
||||
import type { IStockPrice, IStockDataRequest } from './stockprice.js';
|
||||
|
||||
export interface IStockProvider {
|
||||
name: string;
|
||||
priority: number;
|
||||
|
||||
fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice>;
|
||||
fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]>;
|
||||
|
||||
fetchData(request: IStockDataRequest): Promise<IStockPrice | IStockPrice[]>;
|
||||
isAvailable(): Promise<boolean>;
|
||||
|
||||
|
||||
supportsMarket?(market: string): boolean;
|
||||
supportsTicker?(ticker: string): boolean;
|
||||
|
||||
|
||||
readonly requiresAuth: boolean;
|
||||
readonly rateLimit?: {
|
||||
requestsPerMinute: number;
|
||||
|
||||
65
ts/stocks/interfaces/stockdata.ts
Normal file
65
ts/stocks/interfaces/stockdata.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import type { IStockPrice } from './stockprice.js';
|
||||
import type { IStockFundamentals } from './fundamentals.js';
|
||||
|
||||
/**
|
||||
* Combined stock data with price and fundamentals
|
||||
* All calculated metrics (market cap, P/E, P/B) are automatically included
|
||||
*/
|
||||
export interface IStockData {
|
||||
/** Stock ticker symbol */
|
||||
ticker: string;
|
||||
|
||||
/** Price information */
|
||||
price: IStockPrice;
|
||||
|
||||
/** Fundamental data (optional - may not be available for all stocks) */
|
||||
fundamentals?: IStockFundamentals;
|
||||
|
||||
/** When this combined data was fetched */
|
||||
fetchedAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration for StockDataService
|
||||
*/
|
||||
export interface IStockDataServiceConfig {
|
||||
/** Cache configuration */
|
||||
cache?: {
|
||||
/** TTL for price data (default: 24 hours) */
|
||||
priceTTL?: number;
|
||||
/** TTL for fundamentals data (default: 90 days) */
|
||||
fundamentalsTTL?: number;
|
||||
/** Max cache entries (default: 10000) */
|
||||
maxEntries?: number;
|
||||
};
|
||||
|
||||
/** Provider timeouts */
|
||||
timeout?: {
|
||||
/** Timeout for price providers (default: 10000ms) */
|
||||
price?: number;
|
||||
/** Timeout for fundamentals providers (default: 30000ms) */
|
||||
fundamentals?: number;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Request type for getting complete stock data
|
||||
*/
|
||||
export interface ICompleteStockDataRequest {
|
||||
ticker: string;
|
||||
/** Whether to include fundamentals (default: true) */
|
||||
includeFundamentals?: boolean;
|
||||
/** Whether to enrich fundamentals with price calculations (default: true) */
|
||||
enrichFundamentals?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch request for multiple stocks
|
||||
*/
|
||||
export interface ICompleteStockDataBatchRequest {
|
||||
tickers: string[];
|
||||
/** Whether to include fundamentals (default: true) */
|
||||
includeFundamentals?: boolean;
|
||||
/** Whether to enrich fundamentals with price calculations (default: true) */
|
||||
enrichFundamentals?: boolean;
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
|
||||
// Enhanced stock price interface with additional OHLCV data
|
||||
export interface IStockPrice {
|
||||
ticker: string;
|
||||
@@ -22,11 +20,11 @@ export interface IStockPrice {
|
||||
adjusted?: boolean; // If price is split/dividend adjusted
|
||||
dataType: 'eod' | 'intraday' | 'live'; // What kind of data this is
|
||||
fetchedAt: Date; // When we fetched (vs data timestamp)
|
||||
|
||||
// Company identification
|
||||
companyName?: string; // Company name (e.g., "Apple Inc.")
|
||||
companyFullName?: string; // Full company name with exchange (e.g., "Apple Inc. (NASDAQ:AAPL)")
|
||||
}
|
||||
type CheckStockPrice = plugins.tsclass.typeFest.IsEqual<
|
||||
IStockPrice,
|
||||
plugins.tsclass.finance.IStockPrice
|
||||
>;
|
||||
|
||||
export interface IStockPriceError {
|
||||
ticker: string;
|
||||
@@ -94,16 +92,3 @@ export type IStockDataRequest =
|
||||
| IStockHistoricalRequest
|
||||
| IStockIntradayRequest
|
||||
| IStockBatchCurrentRequest;
|
||||
|
||||
// Legacy interfaces (for backward compatibility during migration)
|
||||
/** @deprecated Use IStockDataRequest with type: 'current' instead */
|
||||
export interface IStockQuoteRequest {
|
||||
ticker: string;
|
||||
includeExtendedHours?: boolean;
|
||||
}
|
||||
|
||||
/** @deprecated Use IStockDataRequest with type: 'batch' instead */
|
||||
export interface IStockBatchQuoteRequest {
|
||||
tickers: string[];
|
||||
includeExtendedHours?: boolean;
|
||||
}
|
||||
|
||||
@@ -2,15 +2,11 @@ import * as plugins from '../../plugins.js';
|
||||
import type { IStockProvider, IProviderConfig } from '../interfaces/provider.js';
|
||||
import type {
|
||||
IStockPrice,
|
||||
IStockQuoteRequest,
|
||||
IStockBatchQuoteRequest,
|
||||
IStockDataRequest,
|
||||
IStockCurrentRequest,
|
||||
IStockHistoricalRequest,
|
||||
IStockIntradayRequest,
|
||||
IStockBatchCurrentRequest,
|
||||
IPaginatedResponse,
|
||||
TSortOrder
|
||||
IStockBatchCurrentRequest
|
||||
} from '../interfaces/stockprice.js';
|
||||
|
||||
/**
|
||||
@@ -239,30 +235,6 @@ export class MarketstackProvider implements IStockProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy: Fetch latest EOD price for a single ticker
|
||||
* @deprecated Use fetchData with IStockDataRequest instead
|
||||
*/
|
||||
public async fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
|
||||
// Map legacy request to new format
|
||||
return this.fetchCurrentPrice({
|
||||
type: 'current',
|
||||
ticker: request.ticker
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Legacy: Fetch latest EOD prices for multiple tickers
|
||||
* @deprecated Use fetchData with IStockDataRequest instead
|
||||
*/
|
||||
public async fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
|
||||
// Map legacy request to new format
|
||||
return this.fetchBatchCurrentPrices({
|
||||
type: 'batch',
|
||||
tickers: request.tickers
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Marketstack API is available and accessible
|
||||
*/
|
||||
@@ -349,12 +321,49 @@ export class MarketstackProvider implements IStockProvider {
|
||||
low: data.low,
|
||||
adjusted: data.adj_close !== undefined, // If adj_close exists, price is adjusted
|
||||
dataType: dataType,
|
||||
fetchedAt: fetchedAt
|
||||
fetchedAt: fetchedAt,
|
||||
|
||||
// Company identification
|
||||
companyName: data.company_name || data.name || undefined,
|
||||
companyFullName: this.buildCompanyFullName(data)
|
||||
};
|
||||
|
||||
return stockPrice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build full company name with exchange and ticker information
|
||||
* Example: "Apple Inc (NASDAQ:AAPL)"
|
||||
*/
|
||||
private buildCompanyFullName(data: any): string | undefined {
|
||||
// Check if API already provides full name
|
||||
if (data.full_name || data.long_name) {
|
||||
return data.full_name || data.long_name;
|
||||
}
|
||||
|
||||
// Build from available data
|
||||
const companyName = data.company_name || data.name;
|
||||
const exchangeCode = data.exchange_code; // e.g., "NASDAQ"
|
||||
const symbol = data.symbol; // e.g., "AAPL"
|
||||
|
||||
if (!companyName) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// If we have exchange and symbol, build full name: "Apple Inc (NASDAQ:AAPL)"
|
||||
if (exchangeCode && symbol) {
|
||||
return `${companyName} (${exchangeCode}:${symbol})`;
|
||||
}
|
||||
|
||||
// If we only have symbol: "Apple Inc (AAPL)"
|
||||
if (symbol) {
|
||||
return `${companyName} (${symbol})`;
|
||||
}
|
||||
|
||||
// Otherwise just return company name
|
||||
return companyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format date to YYYY-MM-DD for API requests
|
||||
*/
|
||||
|
||||
429
ts/stocks/providers/provider.secedgar.ts
Normal file
429
ts/stocks/providers/provider.secedgar.ts
Normal file
@@ -0,0 +1,429 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type {
|
||||
IFundamentalsProvider,
|
||||
IStockFundamentals,
|
||||
IFundamentalsRequest,
|
||||
IFundamentalsCurrentRequest,
|
||||
IFundamentalsBatchRequest
|
||||
} from '../interfaces/fundamentals.js';
|
||||
|
||||
/**
|
||||
* Configuration for SEC EDGAR provider
|
||||
*/
|
||||
export interface ISecEdgarConfig {
|
||||
userAgent: string; // Required: Format "Company Name Email" (e.g., "fin.cx info@fin.cx")
|
||||
cikCacheTTL?: number; // Default: 30 days (CIK codes rarely change)
|
||||
fundamentalsCacheTTL?: number; // Default: 90 days (quarterly filings)
|
||||
timeout?: number; // Request timeout in ms
|
||||
}
|
||||
|
||||
/**
|
||||
* Rate limiter for SEC EDGAR API
|
||||
* SEC requires: 10 requests per second maximum
|
||||
*/
|
||||
class RateLimiter {
|
||||
private requestTimes: number[] = [];
|
||||
private readonly maxRequestsPerSecond = 10;
|
||||
|
||||
public async waitForSlot(): Promise<void> {
|
||||
const now = Date.now();
|
||||
const oneSecondAgo = now - 1000;
|
||||
|
||||
// Remove requests older than 1 second
|
||||
this.requestTimes = this.requestTimes.filter(time => time > oneSecondAgo);
|
||||
|
||||
// If we've hit the limit, wait
|
||||
if (this.requestTimes.length >= this.maxRequestsPerSecond) {
|
||||
const oldestRequest = this.requestTimes[0];
|
||||
const waitTime = 1000 - (now - oldestRequest) + 10; // +10ms buffer
|
||||
await plugins.smartdelay.delayFor(waitTime);
|
||||
return this.waitForSlot(); // Recursively check again
|
||||
}
|
||||
|
||||
// Record this request
|
||||
this.requestTimes.push(now);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SEC EDGAR Fundamental Data Provider
|
||||
*
|
||||
* Features:
|
||||
* - Free public access (no API key required)
|
||||
* - All US public companies
|
||||
* - Financial data from 10-K/10-Q filings
|
||||
* - US GAAP standardized metrics
|
||||
* - Historical data back to ~2009
|
||||
* - < 1 minute filing delay
|
||||
*
|
||||
* Documentation: https://www.sec.gov/edgar/sec-api-documentation
|
||||
*
|
||||
* Rate Limits:
|
||||
* - 10 requests per second (enforced by SEC)
|
||||
* - Requires User-Agent header in format: "Company Name Email"
|
||||
*
|
||||
* Data Sources:
|
||||
* - Company Facts API: /api/xbrl/companyfacts/CIK##########.json
|
||||
* - Ticker Lookup: /files/company_tickers.json
|
||||
*/
|
||||
export class SecEdgarProvider implements IFundamentalsProvider {
|
||||
public name = 'SEC EDGAR';
|
||||
public priority = 100; // High priority - free, authoritative, comprehensive
|
||||
public readonly requiresAuth = false; // No API key needed!
|
||||
public readonly rateLimit = {
|
||||
requestsPerMinute: 600, // 10 requests/second = 600/minute
|
||||
requestsPerDay: undefined // No daily limit
|
||||
};
|
||||
|
||||
private logger = console;
|
||||
private baseUrl = 'https://data.sec.gov/api/xbrl';
|
||||
private tickersUrl = 'https://www.sec.gov/files/company_tickers.json';
|
||||
private userAgent: string;
|
||||
private config: Required<ISecEdgarConfig>;
|
||||
|
||||
// Caching
|
||||
private cikCache = new Map<string, { cik: string; timestamp: Date }>();
|
||||
private tickerListCache: { data: any; timestamp: Date } | null = null;
|
||||
|
||||
// Rate limiting
|
||||
private rateLimiter = new RateLimiter();
|
||||
|
||||
constructor(config: ISecEdgarConfig) {
|
||||
// Validate User-Agent
|
||||
if (!config.userAgent) {
|
||||
throw new Error('User-Agent is required for SEC EDGAR provider (format: "Company Name Email")');
|
||||
}
|
||||
|
||||
// Validate User-Agent format (must contain space and @ symbol)
|
||||
if (!config.userAgent.includes(' ') || !config.userAgent.includes('@')) {
|
||||
throw new Error(
|
||||
'Invalid User-Agent format. Required: "Company Name Email" (e.g., "fin.cx info@fin.cx")'
|
||||
);
|
||||
}
|
||||
|
||||
this.userAgent = config.userAgent;
|
||||
this.config = {
|
||||
userAgent: config.userAgent,
|
||||
cikCacheTTL: config.cikCacheTTL || 30 * 24 * 60 * 60 * 1000, // 30 days
|
||||
fundamentalsCacheTTL: config.fundamentalsCacheTTL || 90 * 24 * 60 * 60 * 1000, // 90 days
|
||||
timeout: config.timeout || 30000
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Unified data fetching method
|
||||
*/
|
||||
public async fetchData(
|
||||
request: IFundamentalsRequest
|
||||
): Promise<IStockFundamentals | IStockFundamentals[]> {
|
||||
switch (request.type) {
|
||||
case 'fundamentals-current':
|
||||
return this.fetchFundamentals(request);
|
||||
case 'fundamentals-batch':
|
||||
return this.fetchBatchFundamentals(request);
|
||||
default:
|
||||
throw new Error(`Unsupported request type: ${(request as any).type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch fundamental data for a single ticker
|
||||
*/
|
||||
private async fetchFundamentals(request: IFundamentalsCurrentRequest): Promise<IStockFundamentals> {
|
||||
try {
|
||||
// 1. Get CIK for ticker (with caching)
|
||||
const cik = await this.getCIK(request.ticker);
|
||||
|
||||
// 2. Fetch company facts from SEC (with rate limiting)
|
||||
const companyFacts = await this.fetchCompanyFacts(cik);
|
||||
|
||||
// 3. Parse facts into structured fundamental data
|
||||
return this.parseCompanyFacts(request.ticker, cik, companyFacts);
|
||||
} catch (error) {
|
||||
this.logger.error(`Failed to fetch fundamentals for ${request.ticker}:`, error);
|
||||
throw new Error(`SEC EDGAR: Failed to fetch fundamentals for ${request.ticker}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch fundamental data for multiple tickers
|
||||
*/
|
||||
private async fetchBatchFundamentals(
|
||||
request: IFundamentalsBatchRequest
|
||||
): Promise<IStockFundamentals[]> {
|
||||
const results: IStockFundamentals[] = [];
|
||||
const errors: string[] = [];
|
||||
|
||||
for (const ticker of request.tickers) {
|
||||
try {
|
||||
const fundamentals = await this.fetchFundamentals({
|
||||
type: 'fundamentals-current',
|
||||
ticker
|
||||
});
|
||||
results.push(fundamentals);
|
||||
} catch (error) {
|
||||
this.logger.warn(`Failed to fetch fundamentals for ${ticker}:`, error);
|
||||
errors.push(`${ticker}: ${error.message}`);
|
||||
// Continue with other tickers
|
||||
}
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
throw new Error(`Failed to fetch fundamentals for all tickers. Errors: ${errors.join(', ')}`);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get CIK (Central Index Key) for a ticker symbol
|
||||
* Uses SEC's public ticker-to-CIK mapping file
|
||||
*/
|
||||
private async getCIK(ticker: string): Promise<string> {
|
||||
const tickerUpper = ticker.toUpperCase();
|
||||
|
||||
// Check cache first
|
||||
const cached = this.cikCache.get(tickerUpper);
|
||||
if (cached) {
|
||||
const age = Date.now() - cached.timestamp.getTime();
|
||||
if (age < this.config.cikCacheTTL) {
|
||||
return cached.cik;
|
||||
}
|
||||
// Cache expired, remove it
|
||||
this.cikCache.delete(tickerUpper);
|
||||
}
|
||||
|
||||
// Fetch ticker list (with caching at list level)
|
||||
const tickers = await this.fetchTickerList();
|
||||
|
||||
// Find ticker in list (case-insensitive)
|
||||
const entry = Object.values(tickers).find((t: any) => t.ticker === tickerUpper);
|
||||
|
||||
if (!entry) {
|
||||
throw new Error(`CIK not found for ticker ${ticker}`);
|
||||
}
|
||||
|
||||
const cik = String((entry as any).cik_str);
|
||||
|
||||
// Cache the result
|
||||
this.cikCache.set(tickerUpper, {
|
||||
cik,
|
||||
timestamp: new Date()
|
||||
});
|
||||
|
||||
return cik;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the SEC ticker-to-CIK mapping list
|
||||
* Cached for 24 hours (list updates daily)
|
||||
*/
|
||||
private async fetchTickerList(): Promise<any> {
|
||||
// Check cache
|
||||
if (this.tickerListCache) {
|
||||
const age = Date.now() - this.tickerListCache.timestamp.getTime();
|
||||
if (age < 24 * 60 * 60 * 1000) { // 24 hours
|
||||
return this.tickerListCache.data;
|
||||
}
|
||||
}
|
||||
|
||||
// Wait for rate limit slot
|
||||
await this.rateLimiter.waitForSlot();
|
||||
|
||||
// Fetch from SEC
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(this.tickersUrl)
|
||||
.headers({
|
||||
'User-Agent': this.userAgent,
|
||||
'Accept': 'application/json'
|
||||
})
|
||||
.timeout(this.config.timeout)
|
||||
.get();
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Cache the list
|
||||
this.tickerListCache = {
|
||||
data,
|
||||
timestamp: new Date()
|
||||
};
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch company facts from SEC EDGAR
|
||||
*/
|
||||
private async fetchCompanyFacts(cik: string): Promise<any> {
|
||||
// Pad CIK to 10 digits
|
||||
const paddedCIK = cik.padStart(10, '0');
|
||||
const url = `${this.baseUrl}/companyfacts/CIK${paddedCIK}.json`;
|
||||
|
||||
// Wait for rate limit slot
|
||||
await this.rateLimiter.waitForSlot();
|
||||
|
||||
// Fetch from SEC
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(url)
|
||||
.headers({
|
||||
'User-Agent': this.userAgent,
|
||||
'Accept': 'application/json',
|
||||
'Accept-Encoding': 'gzip, deflate',
|
||||
'Host': 'data.sec.gov'
|
||||
})
|
||||
.timeout(this.config.timeout)
|
||||
.get();
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Validate response
|
||||
if (!data || !data.facts) {
|
||||
throw new Error('Invalid response from SEC EDGAR API');
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse SEC company facts into structured fundamental data
|
||||
*/
|
||||
private parseCompanyFacts(ticker: string, cik: string, data: any): IStockFundamentals {
|
||||
const usGaap = data.facts?.['us-gaap'];
|
||||
|
||||
if (!usGaap) {
|
||||
throw new Error('No US GAAP data available');
|
||||
}
|
||||
|
||||
// Extract latest values for key metrics
|
||||
const fundamentals: IStockFundamentals = {
|
||||
ticker: ticker.toUpperCase(),
|
||||
cik: cik,
|
||||
companyName: data.entityName,
|
||||
provider: this.name,
|
||||
timestamp: new Date(),
|
||||
fetchedAt: new Date(),
|
||||
|
||||
// Per-share metrics
|
||||
earningsPerShareBasic: this.getLatestValue(usGaap, 'EarningsPerShareBasic'),
|
||||
earningsPerShareDiluted: this.getLatestValue(usGaap, 'EarningsPerShareDiluted'),
|
||||
sharesOutstanding: this.getLatestValue(usGaap, 'CommonStockSharesOutstanding'),
|
||||
weightedAverageSharesOutstanding: this.getLatestValue(
|
||||
usGaap,
|
||||
'WeightedAverageNumberOfSharesOutstandingBasic'
|
||||
),
|
||||
|
||||
// Income statement
|
||||
revenue: this.getLatestValue(usGaap, 'Revenues') ||
|
||||
this.getLatestValue(usGaap, 'RevenueFromContractWithCustomerExcludingAssessedTax'),
|
||||
netIncome: this.getLatestValue(usGaap, 'NetIncomeLoss'),
|
||||
operatingIncome: this.getLatestValue(usGaap, 'OperatingIncomeLoss'),
|
||||
grossProfit: this.getLatestValue(usGaap, 'GrossProfit'),
|
||||
costOfRevenue: this.getLatestValue(usGaap, 'CostOfRevenue'),
|
||||
|
||||
// Balance sheet
|
||||
assets: this.getLatestValue(usGaap, 'Assets'),
|
||||
liabilities: this.getLatestValue(usGaap, 'Liabilities'),
|
||||
stockholdersEquity: this.getLatestValue(usGaap, 'StockholdersEquity'),
|
||||
cash: this.getLatestValue(usGaap, 'CashAndCashEquivalentsAtCarryingValue'),
|
||||
propertyPlantEquipment: this.getLatestValue(usGaap, 'PropertyPlantAndEquipmentNet'),
|
||||
|
||||
// Metadata (from latest available data point)
|
||||
fiscalYear: this.getLatestMetadata(usGaap, 'EarningsPerShareDiluted')?.fy,
|
||||
fiscalQuarter: this.getLatestMetadata(usGaap, 'EarningsPerShareDiluted')?.fp,
|
||||
filingDate: this.getLatestMetadata(usGaap, 'EarningsPerShareDiluted')?.filed
|
||||
? new Date(this.getLatestMetadata(usGaap, 'EarningsPerShareDiluted')!.filed)
|
||||
: undefined,
|
||||
form: this.getLatestMetadata(usGaap, 'EarningsPerShareDiluted')?.form
|
||||
};
|
||||
|
||||
return fundamentals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest value for a US GAAP metric
|
||||
*/
|
||||
private getLatestValue(usGaap: any, metricName: string): number | undefined {
|
||||
const metric = usGaap[metricName];
|
||||
if (!metric?.units) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Get the first unit type (USD, shares, etc.)
|
||||
const unitType = Object.keys(metric.units)[0];
|
||||
const values = metric.units[unitType];
|
||||
|
||||
if (!values || !Array.isArray(values) || values.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Get the latest value (last in array)
|
||||
const latest = values[values.length - 1];
|
||||
return latest?.val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metadata from the latest data point
|
||||
*/
|
||||
private getLatestMetadata(usGaap: any, metricName: string): any | undefined {
|
||||
const metric = usGaap[metricName];
|
||||
if (!metric?.units) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const unitType = Object.keys(metric.units)[0];
|
||||
const values = metric.units[unitType];
|
||||
|
||||
if (!values || !Array.isArray(values) || values.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return values[values.length - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if SEC EDGAR API is available
|
||||
*/
|
||||
public async isAvailable(): Promise<boolean> {
|
||||
try {
|
||||
// Test with Apple's well-known CIK
|
||||
const url = `${this.baseUrl}/companyfacts/CIK0000320193.json`;
|
||||
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
.url(url)
|
||||
.headers({
|
||||
'User-Agent': this.userAgent,
|
||||
'Accept': 'application/json'
|
||||
})
|
||||
.timeout(5000)
|
||||
.get();
|
||||
|
||||
const data = await response.json();
|
||||
return data && data.facts !== undefined;
|
||||
} catch (error) {
|
||||
this.logger.warn('SEC EDGAR provider is not available:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cache statistics
|
||||
*/
|
||||
public getCacheStats(): {
|
||||
cikCacheSize: number;
|
||||
hasTickerList: boolean;
|
||||
} {
|
||||
return {
|
||||
cikCacheSize: this.cikCache.size,
|
||||
hasTickerList: this.tickerListCache !== null
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all caches
|
||||
*/
|
||||
public clearCache(): void {
|
||||
this.cikCache.clear();
|
||||
this.tickerListCache = null;
|
||||
this.logger.log('SEC EDGAR cache cleared');
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { IStockProvider, IProviderConfig } from '../interfaces/provider.js';
|
||||
import type { IStockPrice, IStockQuoteRequest, IStockBatchQuoteRequest } from '../interfaces/stockprice.js';
|
||||
import type {
|
||||
IStockPrice,
|
||||
IStockDataRequest,
|
||||
IStockCurrentRequest,
|
||||
IStockBatchCurrentRequest
|
||||
} from '../interfaces/stockprice.js';
|
||||
|
||||
export class YahooFinanceProvider implements IStockProvider {
|
||||
public name = 'Yahoo Finance';
|
||||
@@ -17,7 +22,28 @@ export class YahooFinanceProvider implements IStockProvider {
|
||||
|
||||
constructor(private config?: IProviderConfig) {}
|
||||
|
||||
public async fetchPrice(request: IStockQuoteRequest): Promise<IStockPrice> {
|
||||
/**
|
||||
* Unified data fetching method
|
||||
*/
|
||||
public async fetchData(request: IStockDataRequest): Promise<IStockPrice | IStockPrice[]> {
|
||||
switch (request.type) {
|
||||
case 'current':
|
||||
return this.fetchCurrentPrice(request);
|
||||
case 'batch':
|
||||
return this.fetchBatchCurrentPrices(request);
|
||||
case 'historical':
|
||||
throw new Error('Yahoo Finance provider does not support historical data. Use Marketstack provider instead.');
|
||||
case 'intraday':
|
||||
throw new Error('Yahoo Finance provider does not support intraday data yet. Use Marketstack provider instead.');
|
||||
default:
|
||||
throw new Error(`Unsupported request type: ${(request as any).type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch current price for a single ticker
|
||||
*/
|
||||
private async fetchCurrentPrice(request: IStockCurrentRequest): Promise<IStockPrice> {
|
||||
try {
|
||||
const url = `${this.baseUrl}/v8/finance/chart/${request.ticker}`;
|
||||
const response = await plugins.smartrequest.SmartRequest.create()
|
||||
@@ -64,7 +90,10 @@ export class YahooFinanceProvider implements IStockProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public async fetchPrices(request: IStockBatchQuoteRequest): Promise<IStockPrice[]> {
|
||||
/**
|
||||
* Fetch batch current prices
|
||||
*/
|
||||
private async fetchBatchCurrentPrices(request: IStockBatchCurrentRequest): Promise<IStockPrice[]> {
|
||||
try {
|
||||
const symbols = request.tickers.join(',');
|
||||
const url = `${this.baseUrl}/v8/finance/spark?symbols=${symbols}&range=1d&interval=5m`;
|
||||
@@ -123,7 +152,7 @@ export class YahooFinanceProvider implements IStockProvider {
|
||||
public async isAvailable(): Promise<boolean> {
|
||||
try {
|
||||
// Test with a well-known ticker
|
||||
await this.fetchPrice({ ticker: 'AAPL' });
|
||||
await this.fetchData({ type: 'current', ticker: 'AAPL' });
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.warn('Yahoo Finance provider is not available:', error);
|
||||
|
||||
Reference in New Issue
Block a user