-
Notifications
You must be signed in to change notification settings - Fork 29
/
app.py
125 lines (102 loc) · 3.77 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import matplotlib.dates as mdates
from flask import Flask, render_template, request
from backend.backend import BackendClass
from io import BytesIO
# Initialize
MY_DPI = 96
DATAFILE = 'data/data.h5'
USERFILE = 'data/user.pkl'
sns.set_style("whitegrid")
app = Flask(__name__)
app.config['SECRET_KEY'] = 'My_l0ng_very_secure_secret_k3y'
app.config['DEBUG'] = False
app.config['TESTING'] = False
def plot_returns(data):
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(900/MY_DPI, 400/MY_DPI), dpi=MY_DPI)
x = data.index.values
y = data.values
ax.plot(x, y, linewidth=1.0, color='#2c7fb8')
ax.axhline(y=0, color='#e34a33', linestyle='-', linewidth=0.5)
ax.set_ylabel("Portfolio returns")
# format y-axis
ax.get_yaxis().set_major_formatter(
ticker.FuncFormatter(lambda x, p: '${:.0f}'.format(x)))
# format dates
# months_ticks = mdates.MonthLocator() # every month
date_fmt = mdates.DateFormatter('%b-%Y')
# ax.xaxis.set_major_locator(months_ticks)
ax.xaxis.set_major_formatter(date_fmt)
ax.grid(False, axis='both', linestyle='-', linewidth=0.5, color="#deebf7")
# saving and exporting the svg
with BytesIO() as img_svg:
f.savefig(img_svg, format='svg', dpi=MY_DPI, bbox_inches='tight')
figdata_svg =\
'<svg' + img_svg.getvalue().decode('utf-8').split('<svg')[1]
plt.close(f)
return figdata_svg
def plot_heatmap(corr):
# prepare data
corr = corr.copy()
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True
# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(900/MY_DPI, 400/MY_DPI), dpi=MY_DPI)
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 20, n=10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
ax = sns.heatmap(
corr, mask=mask, cmap=cmap, center=0,
square=False, linewidths=.5,
cbar_kws={
"shrink": .8,
'format': '%.2f'},
annot=True, fmt=".3f")
plt.setp(ax.get_yticklabels(), rotation=0)
# saving and exporting the svg
with BytesIO() as img_svg:
f.savefig(img_svg, format='svg', dpi=MY_DPI, bbox_inches='tight')
figdata_svg =\
'<svg' + img_svg.getvalue().decode('utf-8').split('<svg')[1]
plt.close(f)
return figdata_svg
# default route
@app.route('/', methods=["GET", "POST"])
def portfolio():
date_fmt = '{:%d-%b-%Y}'
# initiate backend and get all values
bc = BackendClass(DATAFILE, USERFILE)
bc = bc.calculate_all()
# create plots
plot_corr_svg = plot_heatmap(bc.stock['corr'])
plot_returns_svg = plot_returns(bc.portfolio['daily'])
# handle update of market or robinhood data
if request.method == 'POST':
if request.form['refresh'] == 'market':
bc.update_market_data()
elif request.form['refresh'] == 'robinhood':
user = request.form['inputUser']
password = request.form['inputPassword']
bc.update_robinhood_data(user, password)
bc = bc.calculate_all()
return render_template(
'pages/portfolio.html',
plot_returns_svg=plot_returns_svg,
plot_corr_svg=plot_corr_svg,
portfolio=bc.portfolio,
trades=bc.trades,
stock=bc.stock,
markowitz=bc.markowitz,
rb_dates=[date_fmt.format(x) for x in bc.user['rb_dates']],
mkt_dates=[date_fmt.format(x) for x in bc.user['mkt_dates']],
today=date_fmt.format(bc.user['today']),
)
if __name__ == '__main__':
PORT = int(os.getenv('PORT', 8080))
HOST = os.getenv('HOST', '0.0.0.0')
app.run(debug=True, host=HOST, port=PORT)