|
|
|
@ -109,7 +109,6 @@ def rolling_optimize(ret, lookback=126, backtest=126, role="max_sharpe", gamma=N |
|
|
|
|
rets = pd.DataFrame(np.vstack([rets, equally_weighted]).T, columns=['Portfolio', 'Equally'], index=ret.index[lookback:]) |
|
|
|
|
return weight, rets |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Define the route for the index pages |
|
|
|
|
@app.route('/') |
|
|
|
|
def index(): |
|
|
|
@ -127,8 +126,6 @@ def login_post(): |
|
|
|
|
username = request.form.get('username') |
|
|
|
|
password = request.form.get('password') |
|
|
|
|
print(username, password) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Connect to the database |
|
|
|
|
conn = psycopg2.connect(**SQL_CONFIG) |
|
|
|
|
with conn: |
|
|
|
@ -253,19 +250,16 @@ def submit_stock_list(): |
|
|
|
|
## Query DB |
|
|
|
|
conn = psycopg2.connect(**SQL_CONFIG) |
|
|
|
|
port = get_stock(conn, stock_list, session['tw']) |
|
|
|
|
if len(port.index) > 908: |
|
|
|
|
port = port.iloc[-908:, :] |
|
|
|
|
if len(port.index) > 1008: |
|
|
|
|
port = port.iloc[-1008:, :] |
|
|
|
|
conn.close() |
|
|
|
|
port = port.iloc[::3, :] |
|
|
|
|
port = port/port.iloc[0, :] |
|
|
|
|
|
|
|
|
|
fig = port.plot(title='資產價格變化', labels=dict(index="Date", value="Price", variable="Assets")) |
|
|
|
|
fig['layout'] = {} |
|
|
|
|
|
|
|
|
|
# 序列化 |
|
|
|
|
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key in request.form: |
|
|
|
|
print(key, request.form[key]) |
|
|
|
|
# Do something with the stock list heres |
|
|
|
@ -284,14 +278,11 @@ def buildPort(): |
|
|
|
|
if time.time() - session['lastCreateTime'] < 60: |
|
|
|
|
print("UNTIL: ", time.time()-session['lastCreateTime']) |
|
|
|
|
return '''<span>投資組合建立時間間隔(或與登入時間間隔)必須大於60秒!</span>''' |
|
|
|
|
# print('last_creation', time.time() - session['lastCreateTime']) |
|
|
|
|
session['lastCreateTime'] = time.time() |
|
|
|
|
# print('last_creation', session['lastCreateTime']) |
|
|
|
|
# print("-"*10) |
|
|
|
|
for key in request.form: |
|
|
|
|
print(key, request.form[key], type(request.form[key])) |
|
|
|
|
|
|
|
|
|
# Portfolio Info |
|
|
|
|
# Portfolio Info , random name generator |
|
|
|
|
name = request.form.get('name') |
|
|
|
|
if name == '': |
|
|
|
|
prefix=''.join(random.choices(string.ascii_uppercase + string.digits, k=6)) |
|
|
|
@ -348,14 +339,17 @@ def buildPort(): |
|
|
|
|
info['vol'], info['mdd'], info['annual_sr'], |
|
|
|
|
info['beta'], info['alpha'], info['var10'], info['R2'], True, comment, stock_list, json.dumps(weight.to_dict()), json.dumps(rets.to_dict())) |
|
|
|
|
sql='insert into strategy \ |
|
|
|
|
(date, name, username, competition, role, annual_ret, vol, mdd, annual_sr, beta, alpha, var10, R2, tw, notes, assets, weight, ret)\ |
|
|
|
|
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING id;' |
|
|
|
|
(date, name, username,\ |
|
|
|
|
competition, role, annual_ret,\ |
|
|
|
|
vol, mdd, annual_sr, beta, alpha,\ |
|
|
|
|
var10, R2, tw, notes, assets, weight, ret)\ |
|
|
|
|
values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING id;' |
|
|
|
|
with conn: |
|
|
|
|
with conn.cursor() as curs: |
|
|
|
|
curs.execute(sql, data) |
|
|
|
|
strategy_id = curs.fetchone()[0] |
|
|
|
|
conn.close() |
|
|
|
|
print("\n------Write in Success--------\n") |
|
|
|
|
print("\n------Strategy write in Success--------\n") |
|
|
|
|
return f'''<span>投資組合已完成建立,請點擊 <a class="badge rounded-pill text-bg-warning" href="/result_view?strategy_id={strategy_id}">{strategy_id}</a>查詢分析結果。</span>''' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|