-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject_6.html
186 lines (170 loc) · 7.8 KB
/
project_6.html
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Montserrat:600,700,800" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css"
integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link rel="stylesheet" href="./style.css">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
<link rel="stylesheet" href="./code.css">
<title>Pascal Schlaak | Portfolio</title>
<link rel="icon" type="image/png" href="./imgs/icons/logo_small.svg">
</head>
<body>
<div id="app">
<navbar-normal></navbar-normal>
<navbar-small></navbar-small>
<hamburger></hamburger>
<section id="project">
<img src="./imgs/dots.png" id="dots">
<div class="container project-text">
<h1>Stock performance summary automation</h1>
<div class="flex-row">
<div class="column-65">
<p class="text">Since I'm interested in stocks, I wanted to dive deeper into stock analysis.
My goal in this project was to build an stock visualization I can later automate, to send
myself
daily summaries of stock performances.
</p>
<p class="text">Therefor I searched for an stock-data API, I can retrieve their
values from the current day, week, ... I found the <code>Alpha Vantage API</code>, that was
not deprecated and can
retrieve realtime stock data like: open, close, low, high. To send the plotted summary via
e-mail I used
the <code>SMTP</code> protocol to send data to my personal e-mail.
</p>
<pre class="prettyprint text fact">
<code class="lang-py">
# Define message content
msg = MIMEMultipart()
msg["Subject"] = "Daily summary stocks"
msg["From"] = "<sender>"
msg["To"] = "<receiver>"
# Append every plot to e-mail
for plot in plot_names:
# Read image of current plot
img_data = open(plot, "rb").read()
image = MIMEImage(img_data, name=os.path.basename(plot))
msg.attach(image)
# Open connection
s = smtplib.SMTP("smtp.gmail.com", 587)
s.ehlo()
# Secure connection
s.starttls()
s.ehlo()
s.login("<e-mail>", "<pw>")
s.sendmail(msg["From"], msg["To"], msg.as_string())
s.quit()
print("E-Mail sent!")</code>
</pre>
<p class="text">
For automation reasons, the <code>Python</code>
script will wait to plot and send data at 19:00 o'clock, when stock market is closing. To
run by itself I built a
<code>Docker</code> image to run my script in a container instance.
</p>
<pre class="prettyprint text fact">
<code class="lang-xml">
# Create python image
FROM python:3
# Set working directory
WORKDIR /usr/src/app
# Copy requirements .txt-file
COPY requirements.txt ./
# Install required python packages
RUN pip3 install --no-cache-dir -r requirements.txt
# Copy files to current working directory
COPY . .
# Run script
CMD [ "python3", "./stock_analysis.py" ]</code>
</pre>
<p class="text">
For visualizing data I used a so called
candlestick graph to present all data in a compact way.
</p>
<pre class="prettyprint text fact">
<code class="lang-py">
data_indexes = []
# Get date needed for saving plot to file
plot_date = str((current_data.index)[0])
# Parse timestamps
for i in current_data.index:
i = parser.parse(i)
data_indexes.append(mdates.date2num(i))
# Create plot
_, ax = plt.subplots()
# Create candlesticks from data
candlestick_ohlc(ax, zip(data_indexes, current_data["1. open"],
current_data["2. high"],
current_data["3. low"], current_data["4. close"]), width=1./(24*60)*3,
colorup="g", colordown="r")
# Plot hours as x-ticks and autoscale
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%H-%M"))
plt.autoscale()
# Add grid lines
plt.grid(b=True, axis="y", color="0.15", linewidth="0.1")
# Set axis labels and title
plt.xlabel("Time [hours]")
plt.ylabel("Stock price [USD]")
plt.title("Stock: " + stock_symbol)
# Check if plot should be showed or saved
if save_plot == 0:
plt.show()
elif save_plot == 1:
plot_name = plot_date[0:10] + "_" + stock_symbol.lower() + ".png"
plt.savefig(plot_name, dpi=400)
plt.close()
else:
print("\nError: Wrong save_plot value!
Should be 0 or 1 see help() of function!")</code>
</pre>
<p class="text">
In the following you can see generated plots of
Apple and Walt Disney on 30 July 2019.
</p>
<div class="grid">
<img src="./imgs/project_6/example_plot_apple.png" alt="Example plot apple stock">
<img src="./imgs/project_6/example_plot_disney.png" alt="Example plot walt disney stock">
</div>
<p class="text">You can find the complete code on my <a
href="https://github.com/Schlagoo/stock_data_analysis" style="color: #2F58F7">Github
repository</a>!</p>
</div>
<div>
<div class="information border">
<p class="text"><b>Type</b></p>
<p class="text">Private project</p>
<p class="text"><b>Tools</b></p>
<p class="text">Python, Docker, Pandas, Numpy, Alpha Vantage API</p>
<p class="text"><b>Partners</b></p>
<p class="text">-</p>
<p class="text"><b>Date</b></p>
<p class="text">2019-06-31</p>
<p class="text"><b>Source</b></p>
<p class="text">
<a href="https://github.com/Schlagoo/stock_data_analysis" style="color: #2F58F7">Github
repository</a>
</p>
</div>
</div>
</div>
</div>
</section>
<footer-portfolio></footer-portfolio>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('#hamburger, #home, #proj, abo').click(function () {
$("#hamburger").toggleClass('open');
});
});
</script>
<script type="text/javascript" src="./main.js"></script>
</body>
</html>