GIL's LAB

pandas_datareader를 이용한 미국 주식(나스닥) 데이터 수집 본문

퀀트 투자/데이터 수집

pandas_datareader를 이용한 미국 주식(나스닥) 데이터 수집

GIL~ 2022. 12. 7. 14:35

이번 포스팅에서는 pandas_datareader를 이용하여 미국 주식 데이터를 수집하는 방법에 대해 알아보겠습니다.

이 패키지는 다양한 금융 데이터를 손쉽게 수집할 수 있도록 하는 패키지입니다. 

 

패키지 설치

pandas-datareader는 pip을 이용해 다음과 같이 설치할 수 있습니다.

pip install pandas-datareader

 

나스닥 종목 불러오기

나스닥 종목은 pandas_datareade.nasdaq_trader의 get_nasdaq_symbols 함수를 이용하여 구할 수 있습니다.

from pandas_datareader.nasdaq_trader import get_nasdaq_symbols
nasdaq_list = get_nasdaq_symbols()
nasdaq_list.head()

[실행 결과]

실행 결과에서 각 컬럼의 정보는 다음과 같습니다 (출처: https://www.nasdaqtrader.com/trader.aspx?id=symboldirdefs) 

 

Symbol The one to four or five character identifier for each NASDAQ-listed security.
Security Name Company issuing the security.
Market Category The category assigned to the issue by NASDAQ based on Listing Requirements. Values:
  • Q = NASDAQ Global Select MarketSM
  • G = NASDAQ Global MarketSM
  • S = NASDAQ Capital Market
Test Issue Indicates whether or not the security is a test security. Values: Y = yes, it is a test issue. N = no, it is not a test issue.
Financial Status Indicates when an issuer has failed to submit its regulatory filings on a timely basis, has failed to meet NASDAQ's continuing listing standards, and/or has filed for bankruptcy. Values include:
  • D = Deficient: Issuer Failed to Meet NASDAQ Continued Listing Requirements
  • E = Delinquent: Issuer Missed Regulatory Filing Deadline
  • Q = Bankrupt: Issuer Has Filed for Bankruptcy
  • N = Normal (Default): Issuer Is NOT Deficient, Delinquent, or Bankrupt.
  • G = Deficient and Bankrupt
  • H = Deficient and Delinquent
  • J = Delinquent and Bankrupt
  • K = Deficient, Delinquent, and Bankrupt
Round Lot Indicates the number of shares that make up a round lot for the given security.
File Creation Time: The last row of each Symbol Directory text file contains a timestamp that reports the File Creation Time. The file creation time is based on when NASDAQ Trader generates the file and can be used to determine the timeliness of the associated data. The row contains the words File Creation Time followed by mmddyyyyhhmm as the first field, followed by all delimiters to round out the row. An example: File Creation Time: 1217200717:03|||||

 

예를 들어, Market Category가 Q이면서 Financial Status가 N인 종목만 가져오겠습니다.

f_nasdaq_list = nasdaq_list.loc[(nasdaq_list['Market Category'] == "Q") & (nasdaq_list['Financial Status'] == "N")]
f_nasdaq_list.head()

[실행 결과]

 

나스닥 종목 불러오기

이제 본격적으로 데이터를 불러오겠습니다.

먼저 필요한 두 모듈을 불러옵니다.

import pandas as pd
import pandas_datareader.data as reader

다음으로 데이터를 수집할 기간을 YYYYMMDD 형식으로 다음과 같이 설정합니다.

start_date = "20210101"
end_date = "20211231"

이제 DataReader 함수를 이용하여 데이터를 불러오겠습니다. 

이 함수는 심볼, 데이터 수집 소스, 시작 날짜, 종료 날짜를 입력으로 받습니다.

야후 파이낸셜에서 애플(심볼: AAPL)의 주가를 수집해보겠습니다. 

df = reader.DataReader('AAPL', 'yahoo', start=start_date, end=end_date)
df.head()

[실행 결과]

굳이 로컬에 저장하지 않아도 될 정도로, 너무나 쉽게 데이터를 수집할 수 있음을 알 수 있습니다.

 


데이터 분석 서비스가 필요한 분은 아래 링크로! 

https://kmong.com/gig/374194 

 

데이터사이언스 박사의 데이터 분석 서비스 드립니다. | 150000원부터 시작 가능한 총 평점 5점의 I

78개 총 작업 개수 완료한 총 평점 5점인 데이터사이언스박사의 IT·프로그래밍, 데이터 분석·시각화 서비스를 68개의 리뷰와 함께 확인해 보세요. IT·프로그래밍, 데이터 분석·시각화 제공 등 150

kmong.com

 

Comments