From f6ab0812eb0a356642d1c0402e4db50cdb9e547d Mon Sep 17 00:00:00 2001 From: SeanChenTaipei Date: Mon, 13 Mar 2023 19:09:34 +0800 Subject: [PATCH] f --- main.py | 26 +++++++++++++----- static/js/addStock.js | 7 ++++- static/js/view.js | 56 ++++++++++++++++++++++++++++++++++++++ templates/result_view.html | 29 +++++++++++++++++--- templates/strategy_tw.html | 5 ++-- 5 files changed, 109 insertions(+), 14 deletions(-) create mode 100644 static/js/view.js diff --git a/main.py b/main.py index cdd1bd9..74aafbc 100644 --- a/main.py +++ b/main.py @@ -50,8 +50,6 @@ role_map = dict(max_sharpe='最大化夏普比率', max_sortino='最大化索提 app = Flask(__name__) app.config.from_mapping(CONFIGS) -# app.add_url_rule('/growth.ico', -# redirect_to=url_for('static', filename='img/growth.ico')) # Load Assets with open('assets_tw.json') as f: @@ -209,7 +207,7 @@ def strategy(): flash('使用投組功能請先登入。', 'warning') return redirect(url_for('login')) session['tw'] = 0 - return render_template('strategy_tw.html', data_us = data_us, data_tw=data_tw) + return render_template('strategy_tw.html', data_us = data_us, data_tw=data_tw, stock=['TSLA']) @@ -222,7 +220,7 @@ def strategy_tw(): flash('使用投組功能請先登入。', 'warning') return redirect(url_for('login')) session['tw'] = 1 - return render_template('strategy_tw.html', data_tw=data_tw) + return render_template('strategy_tw.html', data_tw=data_tw, stock=['2330.TW']) @@ -466,11 +464,11 @@ def result(): with conn.cursor() as curs: if comp is None: sql=f"select id, date, name, username, annual_ret, vol, annual_sr, mdd\ - from strategy order by {role} {order} limit 50" + from strategy order by {escape(role)} {escape(order)} limit 50" curs.execute(sql) else: sql=f"select id, date, name, username, annual_ret, vol, annual_sr, mdd\ - from strategy where competition=%s order by {role} {order} limit 50;" + from strategy where competition=%s order by {escape(role)} {escape(order)} limit 50;" curs.execute(sql, (comp, )) data= curs.fetchall() conn.close() @@ -517,7 +515,21 @@ def result_view(): fig['layout'] = {} data['bar'] = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) return render_template('result_view.html', data=data) - +@app.route('/copyPort', methods=['POST']) +def copy_portfolio(): + if login_required(): + pass + else: + flash('使用投組功能請先登入。', 'warning') + return redirect(url_for('login')) + if not 'tw' in session: + return redirect(url_for('index')) + stock_list = request.form.get('stockList') + stock_list = json.loads(stock_list) + session['tw'] = 0 + print(stock_list) + return render_template('strategy_tw.html', data_us = data_us, data_tw=data_tw, stock=stock_list) + @app.errorhandler(404) def page_not_found(e): # note that we set the 404 status explicitly diff --git a/static/js/addStock.js b/static/js/addStock.js index db9425d..202561f 100644 --- a/static/js/addStock.js +++ b/static/js/addStock.js @@ -1,5 +1,10 @@ // Initialize empty stock list -let stockList = ['2330.TW']; +// let stockList = ['2330.TW']; +var stockList = []; + $('#stock-list span').each(function(){ + stockList.push($(this).text()); +}); +console.log(stockList); let currentList = []; const layout={'autosize': true, 'markers':true, 'title': {'text': ''}, diff --git a/static/js/view.js b/static/js/view.js new file mode 100644 index 0000000..32e761c --- /dev/null +++ b/static/js/view.js @@ -0,0 +1,56 @@ +const wlayout = { +'autosize': true, +'barmode': 'relative', +'title': {'text': ''}, +'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title':''}, +'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title':''}, +'margin': {'l': 50, 'r': 50, 't': 10, 'b': 50}, +'legend': {'yanchor': 'bottom', 'y': 1.3, 'xanchor': 'left', 'x': 0, 'orientation':'h', + 'font': {'size':8} +} +}; +const rlayout = { + 'autosize': true, + 'title': {'text': ''}, + 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title':'', 'rangeslider': {'visible': true} + }, + 'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title':''}, + 'margin': {'l': 40, 'r': 20, 't': 30, 'b': 50}, + 'legend': {'yanchor': 'top', 'y': 1.3, 'xanchor': 'left', 'x': 0.01, 'orientation':'h'} +}; +const blayout = { + 'autosize': true, + 'title': {'text': ''}, + 'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title':''}, + 'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title':''}, + 'margin': {'l': 40, 'r': 20, 't': 50, 'b': 70}, + 'legend': {'yanchor': 'top', 'y': 1.3, 'xanchor': 'left', 'x': 0.01, 'orientation':'h'} +}; +var w = {{ data.weight|safe }}; +var r = {{ data.ret|safe }}; +var b = {{ data.bar|safe }}; +Plotly.newPlot("weight", w.data, wlayout, {responsive: true}); +Plotly.newPlot("price", r.data, rlayout, {responsive: true}); +Plotly.newPlot("bar", b.data, blayout, {responsive: true}); + +$('#copy-port').click(function(event) { + var stockList = []; + $('.asset').each(function(){ + stockList.push($(this).text()); + }); + // alert(currentList.includes(texts)); + if (stockList.length > 0) { + $.ajax({ + url: '/copyPort', //todo create_strategy + method: 'POST', + data: { stockList: JSON.stringify(stockList) }, + success: function(response) { + console.log('success'); + document.write(response); + }, + error: function(xhr) { + console.log('Error submitting stock list: ' + xhr.responseText); + } + }); + } +}); \ No newline at end of file diff --git a/templates/result_view.html b/templates/result_view.html index 57ac1fe..42970f4 100644 --- a/templates/result_view.html +++ b/templates/result_view.html @@ -71,8 +71,11 @@
{% for a in data.assets %} - {{ a|safe }} + {{ a|safe }} {% endfor %} +
@@ -120,9 +123,6 @@
- - -
@@ -187,5 +187,26 @@ Plotly.newPlot("weight", w.data, wlayout, {responsive: true}); Plotly.newPlot("price", r.data, rlayout, {responsive: true}); Plotly.newPlot("bar", b.data, blayout, {responsive: true}); + $('#copy-port').click(function(event) { + var stockList = []; + $('.asset').each(function(){ + stockList.push($(this).text()); + }); + // alert(currentList.includes(texts)); + if (stockList.length > 0) { + $.ajax({ + url: '/copyPort', + method: 'POST', + data: { stockList: JSON.stringify(stockList) }, + success: function(response) { + console.log('success'); + $(document.body).html(response); + }, + error: function(xhr) { + console.log('Error submitting stock list: ' + xhr.responseText); + } + }); + } + }); {% endblock script %} \ No newline at end of file diff --git a/templates/strategy_tw.html b/templates/strategy_tw.html index fd904b7..517721f 100644 --- a/templates/strategy_tw.html +++ b/templates/strategy_tw.html @@ -110,12 +110,14 @@ div.card{
    + {% for s in stock %}
  1. - 2330.TW + {{ s|safe }}
  2. + {% endfor %}
@@ -241,6 +243,5 @@ div.card{ {% block script %} - {% endblock script %}