From 37e122038ef34b50a3d86ca732c9f58ff6528e12 Mon Sep 17 00:00:00 2001 From: SeanChenTaipei Date: Tue, 7 Mar 2023 18:07:54 +0800 Subject: [PATCH 1/2] r4 --- main.py | 28 ++++++++++++++-------------- templates/strategy_tw.html | 4 ---- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/main.py b/main.py index b756b0d..0ad72ff 100644 --- a/main.py +++ b/main.py @@ -20,25 +20,25 @@ pd.options.plotting.backend = "plotly" # PARAMETERS CONFIGS = { - # "ENV": "development", - # "DEBUG": True, + "ENV": "development", + "DEBUG": True, "SECRET_KEY": os.urandom(30), # Set the secret key for session authentication "PERMANENT_SESSION_LIFETIME": timedelta(minutes=60) } -SQL_CONFIG = dict( - database= os.getenv("PGDATABASE"), - user=os.getenv("PGUSER"), - host=os.getenv("PGHOST"), - port=os.getenv("PGPORT"), - password=os.getenv("PGPASSWORD") -) # SQL_CONFIG = dict( -# database="railway", -# user="postgres", -# host="containers-us-west-103.railway.app", -# port="5913", -# password="gv5Mh7cPjCm9YTjAmsYD" +# database= os.getenv("PGDATABASE"), +# user=os.getenv("PGUSER"), +# host=os.getenv("PGHOST"), +# port=os.getenv("PGPORT"), +# password=os.getenv("PGPASSWORD") # ) +SQL_CONFIG = dict( + database="railway", + user="postgres", + host="containers-us-west-103.railway.app", + port="5913", + password="gv5Mh7cPjCm9YTjAmsYD" +) # SQL_CONFIG = { # 'database': "tpm", # 'user': "hsienchen", diff --git a/templates/strategy_tw.html b/templates/strategy_tw.html index 0660cfc..a61b9c8 100644 --- a/templates/strategy_tw.html +++ b/templates/strategy_tw.html @@ -209,10 +209,6 @@ div.card{ {% endblock %} - - - - {% block script %} From 07f7686a7d21597cdbb442b3e02c095ea6b92b42 Mon Sep 17 00:00:00 2001 From: SeanChenTaipei Date: Wed, 8 Mar 2023 00:00:34 +0800 Subject: [PATCH 2/2] d --- main.py | 35 +++++++++++++++-------------------- portfolio_builder.py | 28 ++++++++++++++++++++++------ static/js/addStock.js | 5 +++-- templates/strategy_tw.html | 2 +- 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/main.py b/main.py index 93e65ae..0d59bbb 100644 --- a/main.py +++ b/main.py @@ -20,25 +20,25 @@ pd.options.plotting.backend = "plotly" # PARAMETERS CONFIGS = { - "ENV": "development", - "DEBUG": True, + # "ENV": "development", + # "DEBUG": True, "SECRET_KEY": os.urandom(30), # Set the secret key for session authentication "PERMANENT_SESSION_LIFETIME": timedelta(minutes=60) } -# SQL_CONFIG = dict( -# database= os.getenv("PGDATABASE"), -# user=os.getenv("PGUSER"), -# host=os.getenv("PGHOST"), -# port=os.getenv("PGPORT"), -# password=os.getenv("PGPASSWORD") -# ) SQL_CONFIG = dict( - database="railway", - user="postgres", - host="containers-us-west-103.railway.app", - port="5913", - password="gv5Mh7cPjCm9YTjAmsYD" + database= os.getenv("PGDATABASE"), + user=os.getenv("PGUSER"), + host=os.getenv("PGHOST"), + port=os.getenv("PGPORT"), + password=os.getenv("PGPASSWORD") ) +# SQL_CONFIG = dict( +# database="railway", +# user="postgres", +# host="containers-us-west-103.railway.app", +# port="5913", +# password="gv5Mh7cPjCm9YTjAmsYD" +# ) # SQL_CONFIG = { # 'database': "tpm", # 'user': "hsienchen", @@ -178,10 +178,6 @@ def logout(): flash('請先登入。', 'warning') return redirect(url_for('login')) if 'username' in session: - # for key in list(session.keys()): - # print(key, session[key]) - # for key in list(session.keys()): - # session.pop(key) session.clear() return redirect(url_for('index')) @@ -254,7 +250,6 @@ def submit_stock_list(): print(key, request.form[key]) # Do something with the stock list heres return graphJSON - # return jsonify({'message': 'Stock list received successfully, NOOOOO'}) @app.route('/postPort', methods=['POST']) def buildPort(): @@ -341,7 +336,7 @@ def buildPort(): strategy_id = curs.fetchone()[0] conn.close() print("\n------Write in Success--------\n") - return f'''投資組合已完成建立,請至 {strategy_id}查詢分析結果。''' + return f'''投資組合已完成建立,請至 {strategy_id}查詢分析結果。''' diff --git a/portfolio_builder.py b/portfolio_builder.py index 30b7ef2..df6144f 100644 --- a/portfolio_builder.py +++ b/portfolio_builder.py @@ -60,8 +60,8 @@ class MVO(object): retPort = ret@w # T-dimensional array stdPort = np.std(retPort) g1=manual_ret/stdPort - g2=np.mean(retPort)*(-0.5)*stdPort**(-3)*(2*cov@w) - return g1+g2 + g2=np.mean(retPort)*stdPort**(-3)*cov@w + return g1-g2 @staticmethod def sortino_ratio(w, ret): retPort = ret@w # T-dimensional array @@ -74,8 +74,23 @@ class MVO(object): retPort = ret@w # T-dimensional arrayss stdPort = np.std(retPort) g1=manual_ret/stdPort - g2=np.mean(retPort)*(-0.5)*stdPort**(-3)*(2*cov_sor@w) - return g1+g2 + g2=np.mean(retPort)*stdPort**(-3)*cov_sor@w + return g1-g2 + @staticmethod + def sortino_ratio(w, ret): + retPort = ret@w # T-dimensional array + stdPort = np.std(np.maximum(-retPort, 0)) + return np.mean(retPort)/stdPort + @staticmethod + def sortino_grad(w, ret, cov_sor): + manual_ret = np.mean(ret, axis=0) + # print(cov.shape, w.shape) + retPort = ret@w # T-dimensional arrayss + stdPort = np.std(retPort) + g1=manual_ret/stdPort + g2=np.mean(retPort)*stdPort**(-3)*cov_sor@w + return g1-g2 + @staticmethod def volatility(w, ret): retPort = ret@w # T-dimensional array @@ -85,7 +100,7 @@ class MVO(object): def volatility_grad(w, ret, cov): retPort = ret@w # T-dimensional array stdPort = np.std(retPort) - return cov@w/stdPort**(0.5) + return cov@w/stdPort @classmethod def opt(cls, ret, role="max_sharpe"): n = ret.shape[1] @@ -105,11 +120,12 @@ class MVO(object): else: return init bnds = [[0, 0.6] for i in range(n)] - opts = {'maxiter': 10000, 'disp': False} + opts = {'maxiter': 1000, 'disp': False} cons = ({'type': 'eq', 'fun': lambda w: np.sum(w) - 1}) result = minimize(loss, init, method="SLSQP",\ options=opts, bounds=bnds, tol = None, jac = grad, constraints=cons) sol = result['x'] + print(sol) return np.round(sol, 2) diff --git a/static/js/addStock.js b/static/js/addStock.js index 9c93efb..d9dbdf8 100644 --- a/static/js/addStock.js +++ b/static/js/addStock.js @@ -1,7 +1,8 @@ // Initialize empty stock list let stockList = ['2330.TW']; let currentList = []; -const layout={'autosize': true, 'title': {'text': 'Assets'}, +const layout={'autosize': true, 'markers':true, +'title': {'text': 'Assets'}, 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'rangeslider': {'visible': true}, 'rangeselector':{'rangeselector': @@ -10,7 +11,7 @@ const layout={'autosize': true, 'title': {'text': 'Assets'}, {'count': 6, 'label': '6m', 'step': 'month', 'stepmode': 'backward'}, {'count': 1, 'label': 'YTD', 'step': 'year', 'stepmode': 'todate'}, {'count': 1, 'label': '1y', 'step': 'year', 'stepmode': 'backward'}, {'step': 'all'}]}, 'rangeslider': {'visible': true},'type': 'date'}}, -'yaxis': {'anchor': 'x', 'domain': [0.0, 0.9]}, +'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'fixedrange': false}, 'legend': {'yanchor': 'top', 'y': 1.8, 'xanchor': 'left', 'x': 0.01}, 'margin': {'l': 25, 'r': 5, 't': 10, 'b': 5}, } // Cache frequently-used DOM elements diff --git a/templates/strategy_tw.html b/templates/strategy_tw.html index 8e75aa3..494fd4b 100644 --- a/templates/strategy_tw.html +++ b/templates/strategy_tw.html @@ -123,7 +123,7 @@ div.card{
-
+
按下