data_clear_and_update.py added

master
joseph 2 years ago
parent 412ffce2b4
commit 289e6410bb
  1. 2
      Dockerfile
  2. 2
      data_init/Dockerfile
  3. 25
      data_init/data_clear&update_tw_v0.py
  4. 25
      data_init/data_clear&update_us_v0.py

@ -1,7 +1,7 @@
FROM python:3.9.6
WORKDIR /flask
ADD . /flask
RUN apt-get update
RUN apt update
RUN apt install nano
RUN pip install --upgrade pip
RUN pip3 install -r requirements.txt

@ -1,7 +1,7 @@
FROM python:3.9.6
WORKDIR /flask
ADD . /flask/data_init
RUN apt-get update
RUN apt update
#RUN apt install nano
RUN pip install --upgrade pip
RUN pip3 install -r ./data_init/requirements_data.txt

@ -0,0 +1,25 @@
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_tw.json') as f:
data_tw = json.load(f)
#SQL setting
SQL_CONFIG = dict(database="portfolio_platform", user='postgres', password='password', host='db',port ='5432')
# TW Stocks
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
cursor.execute("TRUNCATE TABLE stock_price_tw")
print("TW stock price cleared")
for ticker in tqdm(data_tw):
df = yf.download(ticker, start="2020-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][i]) for i in range(len(df))]
with conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
sql = "insert into stock_price_tw (ticker, date, price) values %s"
execute_values(curs, sql, value)
print("TW stock price updated")

@ -0,0 +1,25 @@
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='password', 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="2020-1-1", progress=False)
value =[(ticker, df.index[i], df['Close'][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")
Loading…
Cancel
Save