forked from koala73/worldmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock-analysis.ts
More file actions
67 lines (58 loc) · 2.18 KB
/
stock-analysis.ts
File metadata and controls
67 lines (58 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { MARKET_SYMBOLS } from '@/config';
import {
MarketServiceClient,
type AnalyzeStockResponse,
} from '@/generated/client/worldmonitor/market/v1/service_client';
import { getMarketWatchlistEntries } from '@/services/market-watchlist';
const client = new MarketServiceClient('', {
fetch: (...args: Parameters<typeof fetch>) => globalThis.fetch(...args),
});
export type StockAnalysisResult = AnalyzeStockResponse;
export interface StockAnalysisTarget {
symbol: string;
name: string;
display: string;
}
const DEFAULT_LIMIT = 4;
function isAnalyzableSymbol(symbol: string): boolean {
return !symbol.startsWith('^') && !symbol.includes('=');
}
export function getStockAnalysisTargets(limit = DEFAULT_LIMIT): StockAnalysisTarget[] {
const customEntries = getMarketWatchlistEntries().filter((entry) => isAnalyzableSymbol(entry.symbol));
const baseEntries = customEntries.length > 0
? customEntries.map((entry) => ({
symbol: entry.symbol,
name: entry.name || entry.symbol,
display: entry.display || entry.symbol,
}))
: MARKET_SYMBOLS.filter((entry) => isAnalyzableSymbol(entry.symbol));
const seen = new Set<string>();
const targets: StockAnalysisTarget[] = [];
for (const entry of baseEntries) {
if (seen.has(entry.symbol)) continue;
seen.add(entry.symbol);
targets.push({ symbol: entry.symbol, name: entry.name, display: entry.display });
if (targets.length >= limit) break;
}
return targets;
}
export async function fetchStockAnalysesForTargets(targets: StockAnalysisTarget[]): Promise<StockAnalysisResult[]> {
const results: StockAnalysisResult[] = [];
for (let i = 0; i < targets.length; i++) {
if (i > 0) await new Promise((resolve) => setTimeout(resolve, 200));
try {
const result = await client.analyzeStock({
symbol: targets[i]!.symbol,
name: targets[i]!.name,
includeNews: true,
});
if (result.available) results.push(result);
} catch {
// Skip failed individual analysis
}
}
return results;
}
export async function fetchStockAnalyses(limit = DEFAULT_LIMIT): Promise<StockAnalysisResult[]> {
return fetchStockAnalysesForTargets(getStockAnalysisTargets(limit));
}