30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
import yfinance as yf
|
|
import pandas as pd
|
|
|
|
# 1. 애플 주식 종목 코드 (티커) 정의
|
|
ticker_symbol = "AAPL"
|
|
|
|
# 2. Ticker 객체 생성
|
|
apple = yf.Ticker(ticker_symbol)
|
|
|
|
# 3. 현재 가격 정보 가져오기
|
|
try:
|
|
current_price = apple.info['regularMarketPrice']
|
|
print(f"애플 주식 현재 가격: ${current_price:.2f}")
|
|
except KeyError:
|
|
print("현재 가격 정보를 가져오는 데 실패했습니다.")
|
|
|
|
# 4. 과거 캔들 데이터 가져오기
|
|
# 기간(period) 설정: 1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max
|
|
# 간격(interval) 설정: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo
|
|
# 최근 30일치 일봉(1d) 데이터 가져오기 예제
|
|
hist = apple.history(period="30d", interval="1d")
|
|
|
|
# 5. 데이터 출력
|
|
if not hist.empty:
|
|
print("\n최근 30일치 애플 주식 캔들 데이터 (일봉):")
|
|
print(hist[['Open', 'High', 'Low', 'Close', 'Volume']].tail())
|
|
# DataFrame을 CSV 파일로 저장
|
|
# hist.to_csv('aapl_30days.csv')
|
|
else:
|
|
print("과거 데이터를 가져오는 데 실패했습니다. 티커를 확인하세요.") |