Terry Very Good

[ChatGPT] colab에서 ChatGPT에게 물어보며 FinanceDataReader 사용하여 주식 데이터 가져오기( 티커를 리스트로 만든 다음 어제 오늘 사이의 주가 현황을 엑셀로 보여주는 코드) 본문

신기술 습득/ChatGPT

[ChatGPT] colab에서 ChatGPT에게 물어보며 FinanceDataReader 사용하여 주식 데이터 가져오기( 티커를 리스트로 만든 다음 어제 오늘 사이의 주가 현황을 엑셀로 보여주는 코드)

테리베리 2023. 4. 11. 15:15
728x90
반응형

 

https://colab.research.google.com/

 

Google Colaboratory

 

colab.research.google.com

1. 아래와 같이 colab에서 새 노트를 만들고, hello world! 를 출력해보자

2. KRX 종목을 가져와서 출력하고, 삼성전자의 21년간 주가현황을 출력하고 엑셀로 저장해줘!

# finance-datareader 다운로드
!pip install finance-datareader

# 국내 증시 데이터 수집 예시
import FinanceDataReader as fdr

# KRX 종목 코드 가져오기
krx = fdr.StockListing('KRX')
print(krx.head())

# 삼성전자(005930)의 2021년 데이터 가져오기
samsung = fdr.DataReader('005930', '2021-01-01', '2021-12-31')
print(samsung.head())

# 엑셀 저장
samsung.to_excel('삼성전자2021.xlsx')
samsung.to_csv('삼성전자2021.csv')

 

3. 내가 원하는 티커를 리스트로 담은 다음, 그 주식의 어제/오늘 사이의 주가를 알려줘!

import pandas as pd
import FinanceDataReader as fdr
from datetime import datetime, timedelta

# Define the item codes
item_code_list = ['AAPL', 'GOOGL', 'AMZN']

# Define yesterday's date
yesterday = (datetime.today() - timedelta(days=1)).strftime('%Y-%m-%d')

# Initialize an empty list to store the dataframes
df_list = []

# Loop through the item codes
for item in item_code_list:
    # Get the stock price data for yesterday
    df = fdr.DataReader(item, yesterday, yesterday)
    # Add the ticker name as a new column
    df['Ticker'] = item
    # Append the dataframe to the list
    df_list.append(df)

# Concatenate the dataframes into a single dataframe
result_df = pd.concat(df_list)

# Save the result to an Excel file
result_df.to_excel('미국주식.xlsx', index=False)

결과물




(잡상식: 파이썬은 내가짜려고하지말고, 남이짠거 써라. numpy같은거 써야 최소 200배 빠름)

728x90
반응형