-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahoo.ts
61 lines (56 loc) · 2.23 KB
/
yahoo.ts
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
import yahooFinance from 'yahoo-finance2';
import * as fs from 'fs';
import * as csv from 'csv-parser';
// CSV 파일을 읽어 리스트로 변환하는 함수
async function csvToList(filePath: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
const list: string[] = [];
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => {
const value = Object.values(row)[0];
list.push(value as string);
})
.on('end', () => {
console.log('CSV 파일 처리 완료.');
resolve(list);
})
.on('error', (err) => {
console.error('CSV 파일 처리 중 오류 발생:', err);
reject(err);
});
});
}
async function fetchNasdaqStocks(symbols: string[]) {
try {
for (const symbol of symbols) {
try {
const quote = await yahooFinance.quote(symbol);
console.log(`Symbol: ${quote.symbol}, Price: ${quote.regularMarketPrice}`);
const chart = await yahooFinance.chart(symbol, {
period1: '2024-01-01', // 시작 날짜
period2: '2024-02-01', // 종료 날짜
interval: '1mo', // 데이터 간격 (1d, 1wk, 1mo)
});
console.log(chart.quotes);
} catch (error: any) {
if (error.name === 'BadRequestError') {
console.warn(`데이터가 존재하지 않습니다. Symbol: ${symbol}`);
} else {
console.error(`Symbol ${symbol} 처리 중 에러 발생:`, error);
}
continue; // 다음 심볼로 넘어감
}
}
} catch (error) {
console.error('주식 데이터를 처리하는 중 전체적인 오류 발생:', error);
}
}
// 실행 함수
(async () => {
const csvFilePath = 'nasdaq_list.csv'; // CSV 파일 경로
const nasdaqSymbols = await csvToList(csvFilePath); // CSV를 리스트로 변환
console.log('NASDAQ Symbols:', nasdaqSymbols);
// 주식 데이터 가져오기
await fetchNasdaqStocks(nasdaqSymbols);
})();