opencandle 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/logo.svg +187 -0
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +38 -2
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +9 -0
- package/dist/config.js +13 -0
- package/dist/config.js.map +1 -1
- package/dist/infra/browser.d.ts +10 -0
- package/dist/infra/browser.js +1 -0
- package/dist/infra/browser.js.map +1 -1
- package/dist/infra/native-dependencies.d.ts +1 -0
- package/dist/infra/native-dependencies.js +10 -0
- package/dist/infra/native-dependencies.js.map +1 -0
- package/dist/infra/node-version.d.ts +2 -0
- package/dist/infra/node-version.js +23 -0
- package/dist/infra/node-version.js.map +1 -0
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +1 -0
- package/dist/memory/index.js.map +1 -1
- package/dist/memory/sqlite.js +42 -4
- package/dist/memory/sqlite.js.map +1 -1
- package/dist/memory/storage.d.ts +6 -0
- package/dist/memory/storage.js +3 -3
- package/dist/memory/storage.js.map +1 -1
- package/dist/memory/tool-defaults.d.ts +8 -0
- package/dist/memory/tool-defaults.js +59 -0
- package/dist/memory/tool-defaults.js.map +1 -0
- package/dist/onboarding/connect.d.ts +13 -1
- package/dist/onboarding/connect.js +21 -10
- package/dist/onboarding/connect.js.map +1 -1
- package/dist/onboarding/prompt-user.d.ts +1 -1
- package/dist/onboarding/providers.d.ts +7 -0
- package/dist/onboarding/providers.js +6 -3
- package/dist/onboarding/providers.js.map +1 -1
- package/dist/onboarding/tool-helpers.d.ts +1 -1
- package/dist/pi/opencandle-extension.d.ts +7 -1
- package/dist/pi/opencandle-extension.js +186 -10
- package/dist/pi/opencandle-extension.js.map +1 -1
- package/dist/pi/session-storage.d.ts +2 -0
- package/dist/pi/session-storage.js +5 -0
- package/dist/pi/session-storage.js.map +1 -0
- package/dist/pi/session.d.ts +4 -1
- package/dist/pi/session.js +25 -3
- package/dist/pi/session.js.map +1 -1
- package/dist/pi/setup.d.ts +1 -1
- package/dist/pi/setup.js +1 -1
- package/dist/pi/setup.js.map +1 -1
- package/dist/pi/tool-adapter.d.ts +2 -2
- package/dist/pi/tool-adapter.js +14 -1
- package/dist/pi/tool-adapter.js.map +1 -1
- package/dist/prompts/context-builder.d.ts +22 -0
- package/dist/prompts/context-builder.js +45 -10
- package/dist/prompts/context-builder.js.map +1 -1
- package/dist/prompts/disclaimer.d.ts +6 -0
- package/dist/prompts/disclaimer.js +9 -0
- package/dist/prompts/disclaimer.js.map +1 -0
- package/dist/prompts/workflow-prompts.d.ts +8 -0
- package/dist/prompts/workflow-prompts.js +39 -5
- package/dist/prompts/workflow-prompts.js.map +1 -1
- package/dist/providers/yahoo-finance.js +70 -33
- package/dist/providers/yahoo-finance.js.map +1 -1
- package/dist/routing/defaults.js +1 -1
- package/dist/routing/defaults.js.map +1 -1
- package/dist/routing/index.d.ts +4 -0
- package/dist/routing/index.js +3 -0
- package/dist/routing/index.js.map +1 -1
- package/dist/routing/router-llm-client.d.ts +11 -0
- package/dist/routing/router-llm-client.js +42 -0
- package/dist/routing/router-llm-client.js.map +1 -0
- package/dist/routing/router-prompt.d.ts +2 -0
- package/dist/routing/router-prompt.js +138 -0
- package/dist/routing/router-prompt.js.map +1 -0
- package/dist/routing/router-types.d.ts +62 -0
- package/dist/routing/router-types.js +2 -0
- package/dist/routing/router-types.js.map +1 -0
- package/dist/routing/router.d.ts +10 -0
- package/dist/routing/router.js +194 -0
- package/dist/routing/router.js.map +1 -0
- package/dist/runtime/session-coordinator.d.ts +63 -3
- package/dist/runtime/session-coordinator.js +155 -4
- package/dist/runtime/session-coordinator.js.map +1 -1
- package/dist/runtime/tool-defaults-wrapper.d.ts +3 -0
- package/dist/runtime/tool-defaults-wrapper.js +25 -0
- package/dist/runtime/tool-defaults-wrapper.js.map +1 -0
- package/dist/sentiment/store.js +5 -0
- package/dist/sentiment/store.js.map +1 -1
- package/dist/system-prompt.js +20 -12
- package/dist/system-prompt.js.map +1 -1
- package/dist/tool-kit.d.ts +4 -4
- package/dist/tools/fundamentals/company-overview.d.ts +1 -1
- package/dist/tools/fundamentals/comps.d.ts +1 -1
- package/dist/tools/fundamentals/dcf.d.ts +1 -1
- package/dist/tools/fundamentals/earnings.d.ts +1 -1
- package/dist/tools/fundamentals/financials.d.ts +1 -1
- package/dist/tools/fundamentals/sec-filings.d.ts +1 -1
- package/dist/tools/index.d.ts +28 -1
- package/dist/tools/index.js +27 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/interaction/ask-user.d.ts +1 -1
- package/dist/tools/interaction/twitter-login.d.ts +1 -1
- package/dist/tools/macro/fear-greed.d.ts +1 -1
- package/dist/tools/macro/fred-data.d.ts +1 -1
- package/dist/tools/market/crypto-history.d.ts +1 -1
- package/dist/tools/market/crypto-price.d.ts +1 -1
- package/dist/tools/market/search-ticker.d.ts +1 -1
- package/dist/tools/market/stock-history.d.ts +1 -1
- package/dist/tools/market/stock-quote.d.ts +1 -1
- package/dist/tools/options/option-chain.d.ts +1 -1
- package/dist/tools/options/option-chain.js +4 -1
- package/dist/tools/options/option-chain.js.map +1 -1
- package/dist/tools/portfolio/correlation.d.ts +1 -1
- package/dist/tools/portfolio/predictions.d.ts +1 -1
- package/dist/tools/portfolio/risk-analysis.d.ts +1 -1
- package/dist/tools/portfolio/tracker.d.ts +1 -1
- package/dist/tools/portfolio/watchlist.d.ts +1 -1
- package/dist/tools/sentiment/reddit-sentiment.d.ts +1 -1
- package/dist/tools/sentiment/sentiment-summary.d.ts +1 -1
- package/dist/tools/sentiment/sentiment-trend.d.ts +1 -1
- package/dist/tools/sentiment/twitter-sentiment.d.ts +1 -1
- package/dist/tools/sentiment/web-search.d.ts +1 -1
- package/dist/tools/sentiment/web-sentiment.d.ts +1 -1
- package/dist/tools/technical/backtest.d.ts +1 -1
- package/dist/tools/technical/indicators.d.ts +1 -1
- package/dist/tools/technical/indicators.js +7 -1
- package/dist/tools/technical/indicators.js.map +1 -1
- package/dist/workflows/options-screener.js +7 -2
- package/dist/workflows/options-screener.js.map +1 -1
- package/dist/workflows/portfolio-builder.js +3 -3
- package/dist/workflows/portfolio-builder.js.map +1 -1
- package/gui/server/background-quotes.ts +31 -0
- package/gui/server/chat-event-adapter.ts +142 -0
- package/gui/server/invoke-tool.ts +89 -0
- package/gui/server/live-chat-event-adapter.ts +181 -0
- package/gui/server/model-setup.ts +100 -0
- package/gui/server/package.json +5 -0
- package/gui/server/projector.ts +212 -0
- package/gui/server/server.ts +592 -0
- package/gui/server/session-actions.ts +31 -0
- package/gui/server/tool-metadata.ts +88 -0
- package/gui/server/websocket.ts +128 -0
- package/gui/server/writer-lock.ts +118 -0
- package/gui/shared/chat-events.ts +118 -0
- package/gui/shared/event-reducer.ts +186 -0
- package/gui/web/dist/assets/CatalogOverlay-D1ImSJTe.js +1 -0
- package/gui/web/dist/assets/index-DBrWq43L.css +1 -0
- package/gui/web/dist/assets/index-RflHaj0y.js +67 -0
- package/gui/web/dist/assets/logo-CWpt6Y2a.svg +187 -0
- package/gui/web/dist/index.html +17 -0
- package/package.json +44 -18
- package/src/analysts/contracts.ts +189 -0
- package/src/analysts/orchestrator.ts +300 -0
- package/src/cli.ts +205 -0
- package/src/config.ts +161 -0
- package/src/index.ts +5 -0
- package/src/infra/browser.ts +111 -0
- package/src/infra/cache.ts +103 -0
- package/src/infra/http-client.ts +68 -0
- package/src/infra/index.ts +18 -0
- package/src/infra/native-dependencies.ts +12 -0
- package/src/infra/node-version.ts +24 -0
- package/src/infra/open-url.ts +28 -0
- package/src/infra/opencandle-paths.ts +64 -0
- package/src/infra/rate-limiter.ts +64 -0
- package/src/memory/index.ts +10 -0
- package/src/memory/manager.ts +159 -0
- package/src/memory/preference-extractor.ts +106 -0
- package/src/memory/retrieval.ts +70 -0
- package/src/memory/sqlite.ts +172 -0
- package/src/memory/storage.ts +204 -0
- package/src/memory/tool-defaults.ts +87 -0
- package/src/memory/types.ts +67 -0
- package/src/onboarding/connect.ts +184 -0
- package/src/onboarding/credential-interceptor.ts +134 -0
- package/src/onboarding/degradation-accumulator.ts +79 -0
- package/src/onboarding/prompt-user.ts +85 -0
- package/src/onboarding/providers.ts +315 -0
- package/src/onboarding/state.ts +218 -0
- package/src/onboarding/tool-helpers.ts +111 -0
- package/src/onboarding/tool-tags.ts +201 -0
- package/src/onboarding/validation.ts +158 -0
- package/src/pi/opencandle-extension.ts +724 -0
- package/src/pi/session-storage.ts +5 -0
- package/src/pi/session.ts +81 -0
- package/src/pi/setup.ts +371 -0
- package/src/pi/tool-adapter.ts +36 -0
- package/src/prompts/context-builder.ts +204 -0
- package/src/prompts/disclaimer.ts +9 -0
- package/src/prompts/sections.ts +46 -0
- package/src/prompts/workflow-prompts.ts +279 -0
- package/src/providers/alpha-vantage.ts +292 -0
- package/src/providers/coingecko.ts +96 -0
- package/src/providers/exa-search.ts +373 -0
- package/src/providers/fear-greed.ts +45 -0
- package/src/providers/finnhub.ts +124 -0
- package/src/providers/fred.ts +83 -0
- package/src/providers/index.ts +9 -0
- package/src/providers/provider-credential-error.ts +23 -0
- package/src/providers/reddit.ts +151 -0
- package/src/providers/sec-edgar.ts +96 -0
- package/src/providers/twitter.ts +173 -0
- package/src/providers/web-search.ts +293 -0
- package/src/providers/with-fallback.ts +41 -0
- package/src/providers/wrap-provider.ts +64 -0
- package/src/providers/yahoo-finance.ts +367 -0
- package/src/routing/classify-intent.ts +194 -0
- package/src/routing/defaults.ts +29 -0
- package/src/routing/entity-extractor.ts +140 -0
- package/src/routing/index.ts +26 -0
- package/src/routing/router-llm-client.ts +51 -0
- package/src/routing/router-prompt.ts +159 -0
- package/src/routing/router-types.ts +66 -0
- package/src/routing/router.ts +213 -0
- package/src/routing/slot-resolver.ts +152 -0
- package/src/routing/types.ts +63 -0
- package/src/runtime/evidence.ts +77 -0
- package/src/runtime/index.ts +55 -0
- package/src/runtime/prompt-step.ts +75 -0
- package/src/runtime/provider-ids.ts +15 -0
- package/src/runtime/provider-tracker.ts +40 -0
- package/src/runtime/run-context.ts +22 -0
- package/src/runtime/session-coordinator.ts +406 -0
- package/src/runtime/tool-defaults-wrapper.ts +35 -0
- package/src/runtime/validation.ts +214 -0
- package/src/runtime/workflow-events.ts +75 -0
- package/src/runtime/workflow-runner.ts +188 -0
- package/src/runtime/workflow-types.ts +102 -0
- package/src/sentiment/adapters/finnhub.ts +44 -0
- package/src/sentiment/adapters/reddit.ts +65 -0
- package/src/sentiment/adapters/twitter.ts +36 -0
- package/src/sentiment/adapters/web.ts +44 -0
- package/src/sentiment/index.ts +58 -0
- package/src/sentiment/keywords.ts +9 -0
- package/src/sentiment/pipeline.ts +68 -0
- package/src/sentiment/scorer.ts +78 -0
- package/src/sentiment/store.ts +260 -0
- package/src/sentiment/trends.ts +90 -0
- package/src/sentiment/types.ts +108 -0
- package/src/system-prompt.ts +115 -0
- package/src/tool-kit.ts +68 -0
- package/src/tools/AGENTS.md +36 -0
- package/src/tools/fundamentals/company-overview.ts +54 -0
- package/src/tools/fundamentals/comps.ts +156 -0
- package/src/tools/fundamentals/dcf.ts +267 -0
- package/src/tools/fundamentals/earnings.ts +47 -0
- package/src/tools/fundamentals/financials.ts +54 -0
- package/src/tools/fundamentals/sec-filings.ts +61 -0
- package/src/tools/index.ts +88 -0
- package/src/tools/interaction/ask-user.ts +81 -0
- package/src/tools/interaction/twitter-login.ts +93 -0
- package/src/tools/macro/fear-greed.ts +41 -0
- package/src/tools/macro/fred-data.ts +54 -0
- package/src/tools/market/crypto-history.ts +51 -0
- package/src/tools/market/crypto-price.ts +53 -0
- package/src/tools/market/search-ticker.ts +53 -0
- package/src/tools/market/stock-history.ts +79 -0
- package/src/tools/market/stock-quote.ts +64 -0
- package/src/tools/options/greeks.ts +82 -0
- package/src/tools/options/option-chain.ts +91 -0
- package/src/tools/portfolio/correlation.ts +162 -0
- package/src/tools/portfolio/predictions.ts +253 -0
- package/src/tools/portfolio/risk-analysis.ts +134 -0
- package/src/tools/portfolio/tracker.ts +147 -0
- package/src/tools/portfolio/watchlist.ts +153 -0
- package/src/tools/sentiment/reddit-sentiment.ts +164 -0
- package/src/tools/sentiment/sentiment-summary.ts +256 -0
- package/src/tools/sentiment/sentiment-trend.ts +58 -0
- package/src/tools/sentiment/twitter-sentiment.ts +96 -0
- package/src/tools/sentiment/web-search.ts +150 -0
- package/src/tools/sentiment/web-sentiment.ts +76 -0
- package/src/tools/technical/backtest.ts +246 -0
- package/src/tools/technical/indicators.ts +258 -0
- package/src/types/fundamentals.ts +46 -0
- package/src/types/index.ts +20 -0
- package/src/types/macro.ts +27 -0
- package/src/types/market.ts +43 -0
- package/src/types/options.ts +35 -0
- package/src/types/portfolio.ts +41 -0
- package/src/types/sentiment.ts +70 -0
- package/src/workflows/compare-assets.ts +39 -0
- package/src/workflows/index.ts +4 -0
- package/src/workflows/options-screener.ts +49 -0
- package/src/workflows/portfolio-builder.ts +52 -0
- package/src/workflows/types.ts +4 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { httpGet, HttpError } from "../infra/http-client.js";
|
|
2
|
+
import { cache, TTL, STALE_LIMIT } from "../infra/cache.js";
|
|
3
|
+
import { rateLimiter } from "../infra/rate-limiter.js";
|
|
4
|
+
import { ProviderCredentialError } from "./provider-credential-error.js";
|
|
5
|
+
import type { CompanyOverview, EarningsData, FinancialStatement } from "../types/fundamentals.js";
|
|
6
|
+
import type { StockQuote, OHLCV } from "../types/market.js";
|
|
7
|
+
|
|
8
|
+
const BASE_URL = "https://www.alphavantage.co/query";
|
|
9
|
+
const MISSING_OVERVIEW_TTL = 15 * 60_000;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Detects authentication failures in HTTP errors and re-throws them as a
|
|
13
|
+
* typed `ProviderCredentialError` so the tool layer can convert them into
|
|
14
|
+
* a `[OPENCANDLE_CREDENTIAL_REQUIRED ...]` tagged content block.
|
|
15
|
+
*
|
|
16
|
+
* Call from every catch block in this module BEFORE any stale-cache fallback
|
|
17
|
+
* so that a real 401/403 does not silently get masked by cached data.
|
|
18
|
+
*/
|
|
19
|
+
function throwIfAuthError(error: unknown): void {
|
|
20
|
+
if (error instanceof HttpError && (error.status === 401 || error.status === 403)) {
|
|
21
|
+
throw new ProviderCredentialError("alpha_vantage", "stale", error.status);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function buildUrl(fn: string, params: Record<string, string>, apiKey: string): string {
|
|
26
|
+
const qs = new URLSearchParams({ function: fn, ...params, apikey: apiKey });
|
|
27
|
+
return `${BASE_URL}?${qs}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function getOverview(
|
|
31
|
+
symbol: string,
|
|
32
|
+
apiKey: string,
|
|
33
|
+
): Promise<CompanyOverview> {
|
|
34
|
+
const cacheKey = `av:overview:${symbol}`;
|
|
35
|
+
const missingCacheKey = `${cacheKey}:missing`;
|
|
36
|
+
const cached = cache.get<CompanyOverview>(cacheKey);
|
|
37
|
+
if (cached) return cached;
|
|
38
|
+
if (cache.get<string>(missingCacheKey)) {
|
|
39
|
+
throw new Error(`Alpha Vantage: No data found for ${symbol}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
await rateLimiter.acquire("alphavantage");
|
|
44
|
+
|
|
45
|
+
const url = buildUrl("OVERVIEW", { symbol }, apiKey);
|
|
46
|
+
const data = await httpGet<Record<string, string>>(url);
|
|
47
|
+
|
|
48
|
+
if (!data.Symbol) {
|
|
49
|
+
cache.set(missingCacheKey, "missing", MISSING_OVERVIEW_TTL);
|
|
50
|
+
throw new Error(`Alpha Vantage: No data found for ${symbol}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const result: CompanyOverview = {
|
|
54
|
+
symbol: data.Symbol,
|
|
55
|
+
name: data.Name,
|
|
56
|
+
description: data.Description,
|
|
57
|
+
exchange: data.Exchange,
|
|
58
|
+
sector: data.Sector,
|
|
59
|
+
industry: data.Industry,
|
|
60
|
+
marketCap: parseNum(data.MarketCapitalization),
|
|
61
|
+
pe: parseNullableNum(data.PERatio),
|
|
62
|
+
forwardPe: parseNullableNum(data.ForwardPE),
|
|
63
|
+
eps: parseNullableNum(data.EPS),
|
|
64
|
+
dividendYield: parseNullableNum(data.DividendYield),
|
|
65
|
+
beta: parseNullableNum(data.Beta),
|
|
66
|
+
week52High: parseNum(data["52WeekHigh"]),
|
|
67
|
+
week52Low: parseNum(data["52WeekLow"]),
|
|
68
|
+
avgVolume: 0, // Alpha Vantage OVERVIEW does not expose average volume
|
|
69
|
+
profitMargin: parseNullableNum(data.ProfitMargin),
|
|
70
|
+
revenueGrowth: parseNullableNum(data.QuarterlyRevenueGrowthYOY),
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
cache.set(cacheKey, result, TTL.FUNDAMENTALS);
|
|
74
|
+
return result;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
throwIfAuthError(error);
|
|
77
|
+
const stale = cache.getStale<CompanyOverview>(cacheKey, STALE_LIMIT.FUNDAMENTALS);
|
|
78
|
+
if (stale) return stale.value;
|
|
79
|
+
throw error;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export async function getEarnings(
|
|
84
|
+
symbol: string,
|
|
85
|
+
apiKey: string,
|
|
86
|
+
): Promise<EarningsData> {
|
|
87
|
+
const cacheKey = `av:earnings:${symbol}`;
|
|
88
|
+
const cached = cache.get<EarningsData>(cacheKey);
|
|
89
|
+
if (cached) return cached;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
await rateLimiter.acquire("alphavantage");
|
|
93
|
+
|
|
94
|
+
const url = buildUrl("EARNINGS", { symbol }, apiKey);
|
|
95
|
+
const data = await httpGet<{ quarterlyEarnings: any[] }>(url);
|
|
96
|
+
|
|
97
|
+
const quarterly = (data.quarterlyEarnings ?? []).slice(0, 8).map((e: any) => ({
|
|
98
|
+
date: e.fiscalDateEnding,
|
|
99
|
+
reportedEPS: parseFloat(e.reportedEPS) || 0,
|
|
100
|
+
estimatedEPS: parseFloat(e.estimatedEPS) || 0,
|
|
101
|
+
surprise: parseFloat(e.surprise) || 0,
|
|
102
|
+
surprisePercent: parseFloat(e.surprisePercentage) || 0,
|
|
103
|
+
}));
|
|
104
|
+
|
|
105
|
+
const result: EarningsData = { symbol, quarterly };
|
|
106
|
+
cache.set(cacheKey, result, TTL.FUNDAMENTALS);
|
|
107
|
+
return result;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
throwIfAuthError(error);
|
|
110
|
+
const stale = cache.getStale<EarningsData>(cacheKey, STALE_LIMIT.FUNDAMENTALS);
|
|
111
|
+
if (stale) return stale.value;
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export async function getFinancials(
|
|
117
|
+
symbol: string,
|
|
118
|
+
apiKey: string,
|
|
119
|
+
): Promise<FinancialStatement[]> {
|
|
120
|
+
const cacheKey = `av:financials:${symbol}`;
|
|
121
|
+
const cached = cache.get<FinancialStatement[]>(cacheKey);
|
|
122
|
+
if (cached) return cached;
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
// Fetch sequentially to respect Alpha Vantage rate limits (5 req/min free tier)
|
|
126
|
+
const incomeData = await fetchStatement<{ annualReports: any[] }>("INCOME_STATEMENT", symbol, apiKey);
|
|
127
|
+
const balanceData = await fetchStatement<{ annualReports: any[] }>("BALANCE_SHEET", symbol, apiKey);
|
|
128
|
+
const cashFlowData = await fetchStatement<{ annualReports: any[] }>("CASH_FLOW", symbol, apiKey);
|
|
129
|
+
|
|
130
|
+
const incomeReports = incomeData.annualReports ?? [];
|
|
131
|
+
const balanceReports = balanceData.annualReports ?? [];
|
|
132
|
+
const cashFlowReports = cashFlowData.annualReports ?? [];
|
|
133
|
+
|
|
134
|
+
// Index balance sheet and cash flow by fiscal date for merging
|
|
135
|
+
const balanceByDate = new Map(
|
|
136
|
+
balanceReports.map((r: any) => [r.fiscalDateEnding, r]),
|
|
137
|
+
);
|
|
138
|
+
const cashFlowByDate = new Map(
|
|
139
|
+
cashFlowReports.map((r: any) => [r.fiscalDateEnding, r]),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
const statements = incomeReports.slice(0, 4).map((r: any) => {
|
|
143
|
+
const balance = balanceByDate.get(r.fiscalDateEnding) ?? {};
|
|
144
|
+
const cf = cashFlowByDate.get(r.fiscalDateEnding) ?? {};
|
|
145
|
+
const opCashFlow = parseNum(cf.operatingCashflow);
|
|
146
|
+
const capex = parseNum(cf.capitalExpenditures);
|
|
147
|
+
|
|
148
|
+
const totalDebt = parseNum(balance.shortLongTermDebtTotal);
|
|
149
|
+
const cash = parseNum(balance.cashAndCashEquivalentsAtCarryingValue);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
fiscalDate: r.fiscalDateEnding,
|
|
153
|
+
revenue: parseNum(r.totalRevenue),
|
|
154
|
+
grossProfit: parseNum(r.grossProfit),
|
|
155
|
+
operatingIncome: parseNum(r.operatingIncome),
|
|
156
|
+
netIncome: parseNum(r.netIncome),
|
|
157
|
+
eps: parseFloat(r.reportedEPS) || 0,
|
|
158
|
+
totalAssets: parseNum(balance.totalAssets),
|
|
159
|
+
totalLiabilities: parseNum(balance.totalLiabilities),
|
|
160
|
+
totalEquity: parseNum(balance.totalShareholderEquity),
|
|
161
|
+
operatingCashFlow: opCashFlow,
|
|
162
|
+
freeCashFlow: opCashFlow - capex,
|
|
163
|
+
totalDebt: totalDebt || undefined,
|
|
164
|
+
cashAndEquivalents: cash || undefined,
|
|
165
|
+
};
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
cache.set(cacheKey, statements, TTL.FUNDAMENTALS);
|
|
169
|
+
return statements;
|
|
170
|
+
} catch (error) {
|
|
171
|
+
throwIfAuthError(error);
|
|
172
|
+
const stale = cache.getStale<FinancialStatement[]>(cacheKey, STALE_LIMIT.FUNDAMENTALS);
|
|
173
|
+
if (stale) return stale.value;
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function fetchStatement<T>(fn: string, symbol: string, apiKey: string): Promise<T> {
|
|
179
|
+
await rateLimiter.acquire("alphavantage");
|
|
180
|
+
const url = buildUrl(fn, { symbol }, apiKey);
|
|
181
|
+
return httpGet<T>(url);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function getGlobalQuote(
|
|
185
|
+
symbol: string,
|
|
186
|
+
apiKey: string,
|
|
187
|
+
): Promise<StockQuote> {
|
|
188
|
+
const cacheKey = `av:globalquote:${symbol}`;
|
|
189
|
+
const cached = cache.get<StockQuote>(cacheKey);
|
|
190
|
+
if (cached) return cached;
|
|
191
|
+
|
|
192
|
+
try {
|
|
193
|
+
await rateLimiter.acquire("alphavantage");
|
|
194
|
+
|
|
195
|
+
const url = buildUrl("GLOBAL_QUOTE", { symbol }, apiKey);
|
|
196
|
+
const data = await httpGet<{ "Global Quote": Record<string, string> }>(url);
|
|
197
|
+
const gq = data["Global Quote"];
|
|
198
|
+
|
|
199
|
+
if (!gq || !gq["05. price"]) {
|
|
200
|
+
throw new Error(`Alpha Vantage: No quote data for ${symbol}`);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const price = parseFloat(gq["05. price"]) || 0;
|
|
204
|
+
const result: StockQuote = {
|
|
205
|
+
symbol: gq["01. symbol"] ?? symbol,
|
|
206
|
+
price,
|
|
207
|
+
change: parseFloat(gq["09. change"]) || 0,
|
|
208
|
+
changePercent: parseFloat(gq["10. change percent"]?.replace("%", "")) || 0,
|
|
209
|
+
open: parseFloat(gq["02. open"]) || 0,
|
|
210
|
+
high: parseFloat(gq["03. high"]) || 0,
|
|
211
|
+
low: parseFloat(gq["04. low"]) || 0,
|
|
212
|
+
previousClose: parseFloat(gq["08. previous close"]) || 0,
|
|
213
|
+
volume: parseInt(gq["06. volume"], 10) || 0,
|
|
214
|
+
marketCap: 0, // Not available from GLOBAL_QUOTE
|
|
215
|
+
pe: null, // Not available from GLOBAL_QUOTE
|
|
216
|
+
week52High: 0, // Not available from GLOBAL_QUOTE
|
|
217
|
+
week52Low: 0, // Not available from GLOBAL_QUOTE
|
|
218
|
+
timestamp: Date.now(),
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
cache.set(cacheKey, result, TTL.QUOTE);
|
|
222
|
+
return result;
|
|
223
|
+
} catch (error) {
|
|
224
|
+
throwIfAuthError(error);
|
|
225
|
+
const stale = cache.getStale<StockQuote>(cacheKey, STALE_LIMIT.QUOTE);
|
|
226
|
+
if (stale) return stale.value;
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export async function getDailyHistory(
|
|
232
|
+
symbol: string,
|
|
233
|
+
apiKey: string,
|
|
234
|
+
range: string = "6mo",
|
|
235
|
+
): Promise<OHLCV[]> {
|
|
236
|
+
const cacheKey = `av:daily:${symbol}:${range}`;
|
|
237
|
+
const cached = cache.get<OHLCV[]>(cacheKey);
|
|
238
|
+
if (cached) return cached;
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await rateLimiter.acquire("alphavantage");
|
|
242
|
+
|
|
243
|
+
// compact = last 100 data points, full = full 20+ year history
|
|
244
|
+
const daysNeeded = rangeToDays(range);
|
|
245
|
+
const outputsize = daysNeeded > 100 ? "full" : "compact";
|
|
246
|
+
const url = buildUrl("TIME_SERIES_DAILY", { symbol, outputsize }, apiKey);
|
|
247
|
+
const data = await httpGet<{ "Time Series (Daily)": Record<string, Record<string, string>> }>(url);
|
|
248
|
+
|
|
249
|
+
const timeSeries = data["Time Series (Daily)"];
|
|
250
|
+
if (!timeSeries) {
|
|
251
|
+
throw new Error(`Alpha Vantage: No daily history for ${symbol}`);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const ohlcv: OHLCV[] = Object.entries(timeSeries)
|
|
255
|
+
.map(([date, bar]) => ({
|
|
256
|
+
date,
|
|
257
|
+
open: parseFloat(bar["1. open"]) || 0,
|
|
258
|
+
high: parseFloat(bar["2. high"]) || 0,
|
|
259
|
+
low: parseFloat(bar["3. low"]) || 0,
|
|
260
|
+
close: parseFloat(bar["4. close"]) || 0,
|
|
261
|
+
volume: parseInt(bar["5. volume"], 10) || 0,
|
|
262
|
+
}))
|
|
263
|
+
.sort((a, b) => a.date.localeCompare(b.date))
|
|
264
|
+
.slice(-daysNeeded);
|
|
265
|
+
|
|
266
|
+
cache.set(cacheKey, ohlcv, TTL.HISTORY);
|
|
267
|
+
return ohlcv;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
throwIfAuthError(error);
|
|
270
|
+
const stale = cache.getStale<OHLCV[]>(cacheKey, STALE_LIMIT.HISTORY);
|
|
271
|
+
if (stale) return stale.value;
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function rangeToDays(range: string): number {
|
|
277
|
+
const map: Record<string, number> = {
|
|
278
|
+
"1d": 1, "5d": 5, "1mo": 22, "3mo": 66, "6mo": 130,
|
|
279
|
+
"1y": 252, "2y": 504, "5y": 1260, "max": 5000,
|
|
280
|
+
};
|
|
281
|
+
return map[range] ?? 130;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function parseNum(s: string | undefined): number {
|
|
285
|
+
return parseFloat(s ?? "0") || 0;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function parseNullableNum(s: string | undefined): number | null {
|
|
289
|
+
if (!s || s === "None" || s === "-") return null;
|
|
290
|
+
const n = parseFloat(s);
|
|
291
|
+
return isNaN(n) ? null : n;
|
|
292
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { httpGet } from "../infra/http-client.js";
|
|
2
|
+
import { cache, TTL, STALE_LIMIT } from "../infra/cache.js";
|
|
3
|
+
import { rateLimiter } from "../infra/rate-limiter.js";
|
|
4
|
+
import type { CryptoPrice, OHLCV } from "../types/market.js";
|
|
5
|
+
|
|
6
|
+
const BASE_URL = "https://api.coingecko.com/api/v3";
|
|
7
|
+
|
|
8
|
+
interface CoinGeckoDetailResponse {
|
|
9
|
+
id: string;
|
|
10
|
+
symbol: string;
|
|
11
|
+
name: string;
|
|
12
|
+
market_data: {
|
|
13
|
+
current_price: { usd: number };
|
|
14
|
+
price_change_24h: number;
|
|
15
|
+
price_change_percentage_24h: number;
|
|
16
|
+
market_cap: { usd: number };
|
|
17
|
+
total_volume: { usd: number };
|
|
18
|
+
high_24h: { usd: number };
|
|
19
|
+
low_24h: { usd: number };
|
|
20
|
+
ath: { usd: number };
|
|
21
|
+
ath_date: { usd: string };
|
|
22
|
+
circulating_supply: number;
|
|
23
|
+
total_supply: number | null;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function getCryptoPrice(id: string): Promise<CryptoPrice> {
|
|
28
|
+
const cacheKey = `coingecko:price:${id}`;
|
|
29
|
+
const cached = cache.get<CryptoPrice>(cacheKey);
|
|
30
|
+
if (cached) return cached;
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
await rateLimiter.acquire("coingecko");
|
|
34
|
+
|
|
35
|
+
const url = `${BASE_URL}/coins/${encodeURIComponent(id)}?localization=false&tickers=false&community_data=false&developer_data=false`;
|
|
36
|
+
const data = await httpGet<CoinGeckoDetailResponse>(url);
|
|
37
|
+
|
|
38
|
+
const md = data.market_data;
|
|
39
|
+
const result: CryptoPrice = {
|
|
40
|
+
id: data.id,
|
|
41
|
+
symbol: data.symbol,
|
|
42
|
+
name: data.name,
|
|
43
|
+
price: md.current_price.usd,
|
|
44
|
+
change24h: md.price_change_24h,
|
|
45
|
+
changePercent24h: md.price_change_percentage_24h,
|
|
46
|
+
marketCap: md.market_cap.usd,
|
|
47
|
+
volume24h: md.total_volume.usd,
|
|
48
|
+
high24h: md.high_24h.usd,
|
|
49
|
+
low24h: md.low_24h.usd,
|
|
50
|
+
ath: md.ath.usd,
|
|
51
|
+
athDate: md.ath_date.usd,
|
|
52
|
+
circulatingSupply: md.circulating_supply,
|
|
53
|
+
totalSupply: md.total_supply,
|
|
54
|
+
timestamp: Date.now(),
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
cache.set(cacheKey, result, TTL.QUOTE);
|
|
58
|
+
return result;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const stale = cache.getStale<CryptoPrice>(cacheKey, STALE_LIMIT.QUOTE);
|
|
61
|
+
if (stale) return stale.value;
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function getCryptoHistory(
|
|
67
|
+
id: string,
|
|
68
|
+
days: number = 180,
|
|
69
|
+
): Promise<OHLCV[]> {
|
|
70
|
+
const cacheKey = `coingecko:history:${id}:${days}`;
|
|
71
|
+
const cached = cache.get<OHLCV[]>(cacheKey);
|
|
72
|
+
if (cached) return cached;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
await rateLimiter.acquire("coingecko");
|
|
76
|
+
|
|
77
|
+
const url = `${BASE_URL}/coins/${encodeURIComponent(id)}/ohlc?vs_currency=usd&days=${days}`;
|
|
78
|
+
const data = await httpGet<number[][]>(url);
|
|
79
|
+
|
|
80
|
+
const ohlcv: OHLCV[] = data.map(([ts, open, high, low, close]) => ({
|
|
81
|
+
date: new Date(ts).toISOString().split("T")[0],
|
|
82
|
+
open,
|
|
83
|
+
high,
|
|
84
|
+
low,
|
|
85
|
+
close,
|
|
86
|
+
volume: 0, // OHLC endpoint doesn't include volume
|
|
87
|
+
}));
|
|
88
|
+
|
|
89
|
+
cache.set(cacheKey, ohlcv, TTL.HISTORY);
|
|
90
|
+
return ohlcv;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const stale = cache.getStale<OHLCV[]>(cacheKey, STALE_LIMIT.HISTORY);
|
|
93
|
+
if (stale) return stale.value;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|