From 769143c339b2137cd23e13d63d31f72251df39d8 Mon Sep 17 00:00:00 2001 From: SeanChenTaipei Date: Fri, 10 Mar 2023 16:51:34 +0800 Subject: [PATCH 1/4] y --- main.py | 4 ++-- portfolio_builder.py | 3 +-- sql_script/create_strategy.sql | 2 +- static/js/addStock.js | 6 ++--- templates/result.html | 6 +---- templates/result_view.html | 4 ++-- templates/strategy_tw.html | 44 +++++++++++++--------------------- 7 files changed, 27 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index 26cbb0a..107895c 100644 --- a/main.py +++ b/main.py @@ -295,8 +295,8 @@ def buildPort(): # Opt Parameters comp = request.form.get('comp') - # ts = int(request.form.get('ts')) - ts = datetime.now().strftime("%Y-%m-%d, %H:%M:%S") + ts = request.form.get('ts') + # ts = datetime.now().strftime("%Y-%m-%d, %H:%M:%S") role = request.form.get('role') lookback = int(request.form.get('lookback')) backtest = int(request.form.get('frequency')) diff --git a/portfolio_builder.py b/portfolio_builder.py index e2ca465..4fdaec0 100644 --- a/portfolio_builder.py +++ b/portfolio_builder.py @@ -49,7 +49,7 @@ class MVO(object): @staticmethod def sharpe_ratio(w, ret): cov = np.cov(ret.T) - print(cov.shape, w.shape) + # print(cov.shape, w.shape) retPort = ret@w # T-dimensional array stdPort = np.std(retPort) return np.mean(retPort)/stdPort @@ -138,7 +138,6 @@ class MVO(object): 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/sql_script/create_strategy.sql b/sql_script/create_strategy.sql index f3918ab..98c938c 100644 --- a/sql_script/create_strategy.sql +++ b/sql_script/create_strategy.sql @@ -1,7 +1,7 @@ DROP TABLE IF EXISTS strategy; CREATE TABLE strategy ( id SERIAL PRIMARY KEY, - date VARCHAR(24) NOT NULL, + date VARCHAR(64) NOT NULL, name VARCHAR(32) NOT NULL, username VARCHAR(32) NOT NULL, competition VARCHAR(32) NOT NULL, diff --git a/static/js/addStock.js b/static/js/addStock.js index c809e63..912362e 100644 --- a/static/js/addStock.js +++ b/static/js/addStock.js @@ -100,7 +100,7 @@ $sendPort.click(function(event) { method: 'POST', data: { name: $('input[name=portName]').val(), - ts: Date.now(), + ts: Date(Date.now()), comp: $('#competition').val(), lookback: $('#lookback').val(), frequency: $('#opt-frequency').val(), @@ -110,7 +110,7 @@ $sendPort.click(function(event) { stockList: JSON.stringify(stockList) }, success: function(response) { - console.log(response); + // console.log(response); // var res = JSON.parse(response); event.preventDefault(); // $('#modalTitle').text('完成建立') @@ -151,7 +151,7 @@ $submitBtn.click(function(event) { success: function(response) { $('#graph').html('') var graphs = JSON.parse(response); - console.log(graphs.data); + // console.log(graphs.data); Plotly.newPlot("graph", graphs.data, layout, {responsive: true}); // console.log(response.layout); diff --git a/templates/result.html b/templates/result.html index 7e793cd..5ac27db 100644 --- a/templates/result.html +++ b/templates/result.html @@ -36,12 +36,8 @@
-
-
- 策略顯示 -
-
-
diff --git a/templates/strategy_tw.html b/templates/strategy_tw.html index 6ec2e64..2b64695 100644 --- a/templates/strategy_tw.html +++ b/templates/strategy_tw.html @@ -47,33 +47,24 @@ div.card{
{% if session.tw == 1 %}台股{% endif %}投資組合建立指南
    -
  1. 輸入投資組合名稱
  2. -
  3. 選擇所參加的課程或競賽
  4. +
  5. + 輸入投資組合名稱 + +
  6. +
  7. + 選擇所參加的課程或競賽 + +
  8. 選擇資產後按下加入
  9. -
  10. 確認資產後按下確認資產後, 查看資產價格動態圖表
  11. +
  12. 確認資產後按下確認資產 查看資產價格動態圖表
  13. 選擇建立策略相關參數
  14. -
  15. 確認後按下確認建立後, 查看回傳訊息
  16. +
  17. 確認後按下確認建立 查看回傳訊息
-
-
- 投資組合名稱 -
-
- -
-
- 請選擇所參加的課程/競賽 -
-
- -
-
-
@@ -81,12 +72,6 @@ div.card{
已選擇的資產
-
- -
-
@@ -120,6 +105,11 @@ div.card{
價格動態圖表
+
+ +
From f97fc13c0aa8abc2a7a3dffab0aabdf0ecb1a5ec Mon Sep 17 00:00:00 2001 From: SeanChenTaipei Date: Sat, 11 Mar 2023 22:38:12 +0800 Subject: [PATCH 2/4] style change --- main.py | 18 ++--- static/js/addStock.js | 2 +- templates/base.html | 4 +- templates/result_view.html | 11 +-- templates/strategy_tw.html | 151 ++++++++++++++++++++----------------- 5 files changed, 98 insertions(+), 88 deletions(-) diff --git a/main.py b/main.py index 107895c..6509069 100644 --- a/main.py +++ b/main.py @@ -251,11 +251,13 @@ def submit_stock_list(): ## Query DB conn = psycopg2.connect(**SQL_CONFIG) port = get_stock(conn, stock_list, session['tw']) - if len(port.index) > 750: - port = port.iloc[-750:, :] + if len(port.index) > 908: + port = port.iloc[-908:, :] conn.close() + port = port.iloc[::3, :] + port = port/port.iloc[0, :] - fig = port.plot(title='資產價格走勢', labels=dict(index="Date", value="Price", variable="Assets")) + fig = port.plot(title='資產價格變化', labels=dict(index="Date", value="Price", variable="Assets")) fig['layout'] = {} # 序列化 @@ -277,7 +279,7 @@ def buildPort(): if not 'tw' in session: return redirect(url_for('index')) # Stop frequently building strategy - if time.time() - session['lastCreateTime'] < 10: + if time.time() - session['lastCreateTime'] < 60: print("UNTIL: ", time.time()-session['lastCreateTime']) return '''投資組合建立時間間隔(或與登入時間間隔)必須大於60秒!''' # print('last_creation', time.time() - session['lastCreateTime']) @@ -305,8 +307,6 @@ def buildPort(): stock_list = json.loads(request.form.get('stockList')) - ratio=0.7 - # Algorithm MVO print("-"*10) print("Enter Algorithms") @@ -325,9 +325,9 @@ def buildPort(): n = len(port.index) if n < lookback+backtest+63: return f'''投資組合無法建立,資料長度與所選參數不符。''' - elif n > 757+lookback: - port = port.iloc[-(757+lookback):, :] - market = market.iloc[-757:] + elif n > 909+lookback: + port = port.iloc[-(909+lookback):, :] + market = market.iloc[-909:] else: market = market.iloc[lookback:] diff --git a/static/js/addStock.js b/static/js/addStock.js index 912362e..a5472e4 100644 --- a/static/js/addStock.js +++ b/static/js/addStock.js @@ -4,7 +4,7 @@ let currentList = []; const layout={'autosize': true, 'markers':true, 'title': {'text': ''}, 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'rangeslider': {'visible': true}}, -'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'fixedrange': false}, +'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0]}, 'legend': {'yanchor': 'top', 'y': 1.1, 'xanchor': 'left', 'x': 0.01, 'orientation':'h'}, 'margin': {'l': 25, 'r': 5, 't': 10, 'b': 5}, } diff --git a/templates/base.html b/templates/base.html index d892d4b..9603407 100644 --- a/templates/base.html +++ b/templates/base.html @@ -23,8 +23,8 @@ {% endblock %}