/
main.py
140 lines (101 loc) · 5.23 KB
/
main.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from core.models import Portfolio, Plot
from core.utils import get_data, get_latest_date, set_values, render_template, send_email
from core.constants import Email, Graph, Simfonia, Diverso, ActiuniA, Obligatiuni, Total
# ----- Simfonia -----
# Name will be used to print the plot name.
name = Simfonia.NAME
# Data is a list of integers calculated by multiplying VUAN and UNITS.
data = set_values(get_data(Simfonia.URL), Simfonia.UNITS)
# Gain refers to actual gain over the invested amount.
gain = data[0] - Simfonia.INVESTED
max_gain = max(data) - Simfonia.INVESTED
# We plot the latest date.
latest_date = get_latest_date(Simfonia.URL)
# To correctly plot any range of values we use a pixel delimiter defined in Graph constants.
# In short, we set the maximum height for the highest value and 50 pixels for lowest value.
# Each plot has a gradient so we define a start and end color.
plot = Plot(Graph.PLOT_MAX_PIXELS, Simfonia.START_COLOR, Simfonia.END_COLOR)
# Instantiate portfolio.
simfonia = Portfolio(name, data, gain, max_gain, Simfonia.INVESTED ,latest_date, plot)
# ----- Diverso -----
# Name will be used to print the plot name.
name = Diverso.NAME
# Data is a list of integers calculated by multiplying VUAN and UNITS.
data = set_values(get_data(Diverso.URL), Diverso.UNITS)
# Gain refers to actual gain over the invested amount.
gain = data[0] - Diverso.INVESTED
max_gain = max(data) - Diverso.INVESTED
# We plot the latest date.
latest_date = get_latest_date(Diverso.URL)
# To correctly plot any range of values we use a pixel delimiter defined in Graph constants.
# In short, we set the maximum height for the highest value and 50 pixels for lowest value.
# Each plot has a gradient so we define a start and end color.
plot = Plot(Graph.PLOT_MAX_PIXELS, Diverso.START_COLOR, Diverso.END_COLOR)
# Instantiate portfolio.
diverso = Portfolio(name, data, gain, max_gain, Diverso.INVESTED, latest_date, plot)
# ----- Actiuni A -----
# Name will be used to print the plot name.
name = ActiuniA.NAME
# Data is a list of integers calculated by multiplying VUAN and UNITS.
data = set_values(get_data(ActiuniA.URL), ActiuniA.UNITS)
# Gain refers to actual gain over the invested amount.
gain = data[0] - ActiuniA.INVESTED
max_gain = max(data) - ActiuniA.INVESTED
# We plot the latest date.
latest_date = get_latest_date(ActiuniA.URL)
# To correctly plot any range of values we use a pixel delimiter defined in Graph constants.
# In short, we set the maximum height for the highest value and 50 pixels for lowest value.
# Each plot has a gradient so we define a start and end color.
plot = Plot(Graph.PLOT_MAX_PIXELS, ActiuniA.START_COLOR, ActiuniA.END_COLOR)
# Instantiate portfolio.
actiunia = Portfolio(name, data, gain, max_gain, ActiuniA.INVESTED, latest_date, plot)
# ----- Obligatiuni -----
# Name will be used to print the plot name.
name = Obligatiuni.NAME
# Data is a list of integers calculated by multiplying VUAN and UNITS.
data = set_values(get_data(Obligatiuni.URL), Obligatiuni.UNITS)
# Gain refers to actual gain over the invested amount.
gain = data[0] - Obligatiuni.INVESTED
max_gain = max(data) - Obligatiuni.INVESTED
# We plot the latest date.
latest_date = get_latest_date(Obligatiuni.URL)
# To correctly plot any range of values we use a pixel delimiter defined in Graph constants.
# In short, we set the maximum height for the highest value and 50 pixels for lowest value.
# Each plot has a gradient so we define a start and end color.
plot = Plot(Graph.PLOT_MAX_PIXELS, Obligatiuni.START_COLOR, Obligatiuni.END_COLOR)
# Instantiate portfolio.
obligatiuni = Portfolio(name, data, gain, max_gain, Obligatiuni.INVESTED, latest_date, plot)
# ----- Total -----
# Total doesn't actually use NAME anywhere, we might remove it later on.
name = Total.NAME
# We add the data from the portfolios here so that total data is sum of all data.
data = []
if len(simfonia.data) > len(diverso.data):
data_range = len(diverso.data)
else:
data_range = len(simfonia.data)
for index in range(data_range):
data.append(simfonia.data[index] + diverso.data[index] + actiunia.data[index] + obligatiuni.data[index])
gain = simfonia.gain + diverso.gain + actiunia.gain + obligatiuni.gain
max_gain = simfonia.max_gain + diverso.max_gain + actiunia.max_gain + obligatiuni.max_gain
# Not that important, can be the first portfolio date.
latest_date = simfonia.latest_date
plot = Plot(Graph.PLOT_MAX_PIXELS, Total.START_COLOR, Total.END_COLOR)
total = Portfolio(name, data, gain, max_gain, (Simfonia.INVESTED + Diverso.INVESTED + ActiuniA.INVESTED + Obligatiuni.INVESTED), latest_date, plot)
def local():
"""
This is used to run the render locally. Upon run it will open
a new tab in firefox and plot the portfolios data.
"""
render_template(simfonia, diverso, actiunia, obligatiuni, total, Graph.PLOT_MAX_PIXELS, True)
def lambda_handler(event, context):
"""
This is the function AWS Lambda will call when running the application.
"""
email_body = render_template(simfonia, diverso, actiunia, obligatiuni, total,
Graph.PLOT_MAX_PIXELS)
send_email(Email.SUBJECT, email_body, Email.AWS_REGION, Email.RECIPIENT,
Email.CHARSET, Email.SENDER)
return {'statusCode': 200}
if __name__ == '__main__':
local()