# --- Installing Libraries ---
!pip install kaggle
!pip install yahoo_fin
# --- Importing Libraries ---
import pandas as pd
import kaggle
import yahoo_fin
from datetime import datetime
# --- Create a Function to Get Stock Data ---
def hit_api(dataset):
# --- Creating Parameters ---
from yahoo_fin.stock_info import get_data
today = datetime.now().strftime("%m/%d/%Y")
period = {"1d": "", "1wk": "_weekly", "1mo": "_monthly"}
column_rename = ["Open", "High", "Low", "Close", "Adj Close", "Volume"]
# --- Dataset Dictionary For Loop ---
for x in dataset:
for i in period:
df_hist = get_data(dataset[x]['stock_code'], start_date=dataset[x]['start_date'], end_date=today,
index_as_date=True, interval=i)
# --- Preprocessing Dataset ---
df_hist.drop('ticker', axis=1, inplace=True)
df_hist.columns = column_rename
df_hist.index.names = ['Date']
# --- Export to `.csv` Files based on Dataset Name ---
df_hist.to_csv(x+"/"+dataset[x]['stock_code']+period[i]+".csv")
# --- Dataset Dictionary ---
dataset = {
"samsung": {
"id": "samsung-electronics-stock-historical-price",
"stock_code": "005930.KS",
"start_date": "01/01/2019"
},
"hyundai": {
"id": "hyundai-motor-company-stock-historical-price",
"stock_code": "005380.KS",
"start_date": "01/01/2016"
}
}
# --- Calling Get Stock Data Function ---
hit_api(dataset)
# --- Updating "Hyundai" Datasets ---
!kaggle datasets version -p hyundai -m "Automatic Update"
# --- Updating "Samsung" Datasets ---
!kaggle datasets version -p samsung -m "Automatic Update"