Update daily, fix database create bug

master
josephwang 1 year ago
parent 8629ea6c7e
commit e8ddb42cac
  1. 3
      data_init/Dockerfile
  2. 27
      data_init/config.py
  3. 1
      data_init/requirements_data.txt
  4. 8
      data_init/update_data_daily.py
  5. 7
      docker-compose.yml
  6. 4
      sql_script/Dockerfile

@ -5,6 +5,5 @@ RUN apt update
#RUN apt install nano
RUN pip install --upgrade pip
RUN pip3 install -r ./data_init/requirements_data.txt
CMD bash -c "python data_init/data_init_tw_v0.py && python data_init/data_init_us_v0.py"
CMD bash -c "python data_init/data_init_tw_v0.py && python data_init/data_init_us_v0.py ; python data_init/update_data_daily.py"

@ -0,0 +1,27 @@
import os
import time
from datetime import datetime, date, timedelta
# PARAMETERS
CONFIGS = {
"SECRET_KEY": os.urandom(30), # Set the secret key for session authentication
"PERMANENT_SESSION_LIFETIME": timedelta(minutes=60)
}
SQL_CONFIG = dict(
database="portfolio_platform",
user="postgres",
host="db",
port="5432",
password="password"
)
CACHE_CONFIG = {
'CACHE_TYPE': 'redis',
# 'CACHE_REDIS_USER': 'default',
'CACHE_REDIS_HOST': 'redis',
'CACHE_REDIS_PORT': 6379,
# 'CACHE_REDIS_PASSWORD': '5rP99RevPMW94rswBXAL',
# 'CACHE_KEY_PREFIX': 'railway_redis_'
}
role_map = dict(max_sharpe='最大化夏普比率',
max_sortino='最大化索提諾比率',
min_volatility='最小化波動率',
quadratic_utility='最大化效用函數')

@ -4,3 +4,4 @@ requests==2.28.2
SQLAlchemy==2.0.4
yfinance==0.2.22
tqdm==4.62.3
schedule==1.2.1

@ -15,7 +15,7 @@ def update_data():
# Connect to the database
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
print("Stock prices US updating")
# Get the list of tickers
cursor.execute("SELECT DISTINCT ticker FROM stock_price")
tickers = [row[0] for row in cursor.fetchall()]
@ -38,7 +38,7 @@ def update_data_tw():
# Connect to the database
conn = psycopg2.connect(**SQL_CONFIG)
cursor = conn.cursor()
print("Stock prices US updating")
# Get the list of tickers
cursor.execute("SELECT DISTINCT ticker FROM stock_price_tw")
tickers = [row[0] for row in cursor.fetchall()]
@ -58,8 +58,8 @@ def update_data_tw():
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)
schedule.every().day.at("17:00").do(update_data)
schedule.every().day.at("18:00").do(update_data_tw)
# Keep the script running
while True:
schedule.run_pending()

@ -3,7 +3,6 @@ services:
db:
build:
context: ./sql_script/
image: postgres:15.3-alpine
environment:
POSTGRES_DB: portfolio_platform
POSTGRES_USER: postgres
@ -11,8 +10,8 @@ services:
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/var/lib/postgresql/data
# ports:
# - "5432:5432"
ports:
- "5436:5432"
networks:
- common_network
#Comment for security
@ -47,7 +46,7 @@ services:
command: bash -c "cp -r /flask/* /flask_run"
flask:
container_name: flask
command: bash -c "cd flask_run && python main.py runserver 0.0.0.0:8000 && python update_data_daily.py"
command: bash -c "cd ../flask_run ; python main.py runserver 0.0.0.0:8000"
image: tpm-flask
volumes:
- flask-data:/flask_run

@ -1,3 +1,3 @@
FROM postgres:15.3-alpine
FROM postgres
COPY create_all.sql /docker-entrypoint-initdb.d/
COPY create_all.sql /docker-entrypoint-initdb.d/

Loading…
Cancel
Save