Skip to content

Instantly share code, notes, and snippets.

@dharma6872
Created February 14, 2021 02:12
Show Gist options
  • Save dharma6872/3ee40f9027c18384563565f5d4e58061 to your computer and use it in GitHub Desktop.
Save dharma6872/3ee40f9027c18384563565f5d4e58061 to your computer and use it in GitHub Desktop.
[주가 데이터 다운로드] #퀀트
import datetime
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
# 과거 주가 다운로드 함수
# stock_no: 한국 주식 번호, 현재는 한국 주식만 다운로드 가능
def get_stock_history_pdr(stock_no):
# 과거 주가 데이터, 기간은 3.5년으로 설정
DT_DIFF = 365.25*3.5
END_DT = datetime.datetime.now()
START_DT = END_DT+datetime.timedelta(days=-DT_DIFF)
STOCK_KS = stock_no+'.KS'
df = pdr.get_data_yahoo(STOCK_KS, start=START_DT.strftime("%Y-%m-%d"), end=END_DT.strftime("%Y-%m-%d"))
df.dropna(subset = ["Close"], inplace=True)
return df
import datetime
import yfinance as yf
# 과거 주가 다운로드 함수
# stock_no: 한국 주식 번호, 현재는 한국 주식만 다운로드 가능
def get_stock_history_yf(stock_no):
# 과거 주가 데이터, 기간은 3.5년으로 설정
DT_DIFF = 365.25*3.5
END_DT = datetime.datetime.now()
START_DT = END_DT+datetime.timedelta(days=-DT_DIFF)
STOCK_KS = stock_no+'.KS'
df = yf.Ticker(STOCK_KS).history(start=START_DT.strftime("%Y-%m-%d"), end=END_DT.strftime("%Y-%m-%d"))
df.dropna(subset = ["Close"], inplace=True)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment