add week Opt & everyday update stock price

master
joey0629 2 years ago
parent 38f1bf56e7
commit 8629ea6c7e
  1. 2
      docker-compose.yml
  2. 1
      requirements.txt
  3. 1
      templates/strategy_tw.html
  4. 66
      update_data_daily.py

@ -47,7 +47,7 @@ services:
command: bash -c "cp -r /flask/* /flask_run" command: bash -c "cp -r /flask/* /flask_run"
flask: flask:
container_name: flask container_name: flask
command: bash -c "cd flask_run; python main.py runserver 0.0.0.0:8000" command: bash -c "cd flask_run && python main.py runserver 0.0.0.0:8000 && python update_data_daily.py"
image: tpm-flask image: tpm-flask
volumes: volumes:
- flask-data:/flask_run - flask-data:/flask_run

@ -16,3 +16,4 @@ gevent==22.10.2
yfinance==0.2.22 yfinance==0.2.22
tqdm==4.62.3 tqdm==4.62.3
werkzeug==2.2.2 werkzeug==2.2.2
schedule==1.2.1

@ -167,6 +167,7 @@ div.card{
<div class="input-group"> <div class="input-group">
<span class="input-group-text bg-info">最佳化頻率</span> <span class="input-group-text bg-info">最佳化頻率</span>
<select id="opt-frequency" class="form-select"> <select id="opt-frequency" class="form-select">
<option value="5">每日</option>
<option value="21">每月</option> <option value="21">每月</option>
<option value="63">每季</option> <option value="63">每季</option>
<option selected value="126">每半年</option> <option selected value="126">每半年</option>

@ -0,0 +1,66 @@
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
import datetime
import schedule
import time
from config import SQL_CONFIG
#SQL setting
def update_data():
# Connect to the database
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
# Get the list of tickers
cursor.execute("SELECT DISTINCT ticker FROM stock_price")
tickers = [row[0] for row in cursor.fetchall()]
for ticker in tqdm(tickers):
# Get the latest date for this ticker
cursor.execute(f"SELECT MAX(date) FROM stock_price WHERE ticker = '{ticker}'")
latest_date = cursor.fetchone()[0]
# If the latest date is not today, update the data
if latest_date < datetime.date.today():
df = yf.download(ticker, start=latest_date.strftime('%Y-%m-%d'),
end=datetime.date.today().strftime('%Y-%m-%d'), 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("Stock prices US updated")
def update_data_tw():
# Connect to the database
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
# Get the list of tickers
cursor.execute("SELECT DISTINCT ticker FROM stock_price_tw")
tickers = [row[0] for row in cursor.fetchall()]
for ticker in tqdm(tickers):
# Get the latest date for this ticker
cursor.execute(f"SELECT MAX(date) FROM stock_price_tw WHERE ticker = '{ticker}'")
latest_date = cursor.fetchone()[0]
# If the latest date is not today, update the data
if latest_date < datetime.date.today():
df = yf.download(ticker, start=latest_date.strftime('%Y-%m-%d'),
end=datetime.date.today().strftime('%Y-%m-%d'), 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("Stock prices TW updated")
# Schedule the task
schedule.every().day.at("1:00").do(update_data)
schedule.every().day.at("2:00").do(update_data_tw)
# Keep the script running
while True:
schedule.run_pending()
time.sleep(1)
Loading…
Cancel
Save