본문 바로가기
Javascript

[nodeJs] Yahoo Finance에서 주식 데이터 끌어오기

by SeanK 2022. 1. 29.

국내 커뮤니티를 돌아다니다 보면 주식 데이터를 활용한 프로그래밍에 대한 글은 많지만

 

막상 그 안을 들여다보면 거의 크롤링을 이용한 데이터 끌어오는 방식에 초점이 맞춰져 있다. 

 

사실 그럴 수 밖에 없는 것이, 국내에 주식 데이터 api를 제공하는 기업이 사실상 전무하다 보니 이런 현상이 일어나는 듯하다. 

 

인공지능과 빅데이터를 외치는 사람은 많지만 정작 프로그래밍에 필요한 데이터를 제공하는 곳은 없는 대한민국...

 

이전에는 한국거래소에서 실시간 정보를 api를 통해 제공한 듯 하지만 지금은 그마저도 서비스를 종료해 데이터를 받아올 수 없게 되어 버렸다. 

 

그래서 방법이 없을까... 고민을 하던 중 Yahoo Finance에서 주식 데이터를 api로 받아 올 수 있음을 발견했다. 

 

 

1. RapidApi 가입

 

https://rapidapi.com/hub

 

API Hub - Free Public & Open Rest APIs | RapidAPI

The spoonacular Nutrition, Recipe, and Food API allows you to access over 365,000 recipes and 86,000 food products. Our food ontology and semantic recipe search engine makes it possible to search for recipes using natural language queries, such as "gluten

rapidapi.com

 

rapidapi는 여러가지 api를 요청할 수 있는 유료 사이트이다. 유료지만 일정 횟수 미만의 요청의 경우 과금이 되지 않으니 시험용 프로그래밍을 제작할 때는 문제가 없을 듯하다.

 

2. Subscibe

 

https://rapidapi.com/apidojo/api/yh-finance/pricing

 

YH Finance API: Pricing & Cost (apidojo) | RapidAPI

1 Call volume and billing Monitor call volumes and corresponding billing charges for all APIs in one dashboard. 2 Errors and latency Ensure your app’s uptime by keeping track of API errors and trends in latency. 3 Logs for your API calls Debug faster by

rapidapi.com

그리고 다음 페이지에서 0원짜리 플랜을 subscribe 하면 끝.

 

3. Axios를 이용해 api 요청하기 

 

Endpoints 탭에 이동하면 이용할 수 있는 여러 가지 api요청들이 잘 정리가 되어있다. 

 

필자의 경우 summary 정보만을 필요로 했기 때문에 아래와 같이 api 요청을 보내면 다음과 같은 response가 돌아온다. 

 

참고로 한국 주식의 q값은 Yahoo Finance에서 확인할 수 있다. 

https://finance.yahoo.com/

 

Yahoo Finance - Stock Market Live, Quotes, Business & Finance News

At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market data, social interaction and mortgage rates that help you manage your financial life.

finance.yahoo.com

 

삼성의 경우 q값은 '005930.KS'

region의 경우 'KR'로 설정한다. 

 

var axios = require("axios").default;

var options = {
  method: 'GET',
  url: 'https://yh-finance.p.rapidapi.com/auto-complete',
  params: {q: 'tesla', region: 'US'},
  headers: {
    'x-rapidapi-host': 'yh-finance.p.rapidapi.com',
    'x-rapidapi-key': ''
  }
};

axios.request(options).then(function (response) {
	console.log(response.data);
}).catch(function (error) {
	console.error(error);
});

 

Response

 

  summaryDetail: {
    previousClose: { raw: 71300, fmt: '71,300.00' },
    regularMarketOpen: { raw: 71300, fmt: '71,300.00' },
    twoHundredDayAverage: { raw: 77108, fmt: '77,108.00' },
    trailingAnnualDividendYield: {},
    payoutRatio: { raw: 0.5843, fmt: '58.43%' },
    volume24Hr: {},
    regularMarketDayHigh: { raw: 73700, fmt: '73,700.00' },
    navPrice: {},
    averageDailyVolume10Day: { raw: 12955931, fmt: '12.96M', longFmt: '12,955,931' },
    totalAssets: {},
    regularMarketPreviousClose: { raw: 71300, fmt: '71,300.00' },
    fiftyDayAverage: { raw: 76724, fmt: '76,724.00' },
    trailingAnnualDividendRate: {},
    open: { raw: 71300, fmt: '71,300.00' },
    toCurrency: null,
    averageVolume10days: { raw: 12955931, fmt: '12.96M', longFmt: '12,955,931' },
    expireDate: {},
    yield: {},
    algorithm: null,
    dividendRate: { raw: 1444, fmt: '1,444.00' },
    exDividendDate: { raw: 1640736000, fmt: '2021-12-29' },
    beta: { raw: 0.926548, fmt: '0.93' },
    circulatingSupply: {},
    startDate: {},
    regularMarketDayLow: { raw: 71200, fmt: '71,200.00' },
    priceHint: { raw: 2, fmt: '2', longFmt: '2' },
    currency: 'KRW',
    regularMarketVolume: { raw: 21026251, fmt: '21.03M', longFmt: '21,026,251' },
    lastMarket: null,
    maxSupply: {},
    openInterest: {},
    marketCap: {
      raw: 493080331419648,
      fmt: '493.08T',
      longFmt: '493,080,331,419,648'
    },
    volumeAllCurrencies: {},
    strikePrice: {},
    averageVolume: { raw: 14441150, fmt: '14.44M', longFmt: '14,441,150' },
    priceToSalesTrailing12Months: { raw: 1.8635582, fmt: '1.86' },
    dayLow: { raw: 71200, fmt: '71,200.00' },
    ask: { raw: 73100, fmt: '73,100.00' },
    ytdReturn: {},
    askSize: { raw: 0, fmt: null, longFmt: '0' },
    volume: { raw: 21026251, fmt: '21.03M', longFmt: '21,026,251' },
    fiftyTwoWeekHigh: { raw: 86400, fmt: '86,400.00' },
    forwardPE: {},
    maxAge: 1,
    fromCurrency: null,
    fiveYearAvgDividendYield: { raw: 2.75, fmt: '2.75' },
    fiftyTwoWeekLow: { raw: 68300, fmt: '68,300.00' },
    bid: { raw: 73000, fmt: '73,000.00' },
    tradeable: false,
    dividendYield: { raw: 0.020299999, fmt: '2.03%' },
    bidSize: { raw: 0, fmt: null, longFmt: '0' },
    dayHigh: { raw: 73700, fmt: '73,700.00' }
  }