|
|
|
|
|
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
|
|
|
|
|
|
with open('assets_us.json') as f:
|
|
|
|
|
|
data_tw = json.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
#SQL setting
|
|
|
|
|
|
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='thiispassword1qaz!QAZ', host='db',port ='5432')
|
|
|
|
|
|
# TW Stocks
|
|
|
|
|
|
conn = psycopg2.connect(**SQL_CONFIG)
|
|
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
cursor.execute("TRUNCATE TABLE stock_price")
|
|
|
|
|
|
print("US stock price cleared")
|
|
|
|
|
|
for ticker in tqdm(data_tw):
|
|
|
|
|
|
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, df.index[i], float(df[price_col].iloc[i])) for i in range(len(df))]
|
|
|
|
|
|
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("US stock price updated")
|