投資組合大擂台 Ver. 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

35 lines
1.2 KiB

import pandas as pd
import json
import yfinance as yf
import numpy as np
import psycopg2
from psycopg2.extras import execute_values
from tqdm import tqdm
from config import SQL_CONFIG
with open('data_init/assets_us.json') as f:
data_us = json.load(f)
# US Stocks
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
# Schema already created by data_init_tw_v0.py, just check connection
cursor.execute("SELECT COUNT(*) FROM stock_price;")
row_count = cursor.fetchone()[0]
if row_count <= 0 :
for ticker in tqdm(data_us):
df = yf.download(ticker, start="2007-01-01", progress=False, threads=False)
if df is None or df.empty:
continue
price_col = 'Close' if 'Close' in df.columns else ('Adj Close' if 'Adj Close' in df.columns else None)
if price_col is None:
continue
value = [(ticker, row.name.strftime('%Y-%m-%d'), float(row[price_col])) for _, row in df.iterrows()]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price (ticker, date, price) values %s"
execute_values(curs, sql, value)
print("Finish initialize")
else:
print("DB already have data")