WEBVTT

0
00:00.180 --> 00:02.310
Now I'm going to walk you through the solution.

1
00:02.790 --> 00:06.870
And I want to make sure that you've actually given this a go before you look

2
00:06.870 --> 00:10.920
through the solution, because you can watch me type in code all day long,

3
00:10.950 --> 00:13.410
and it's still not going to get you where you want to be.

4
00:13.470 --> 00:16.530
So make sure you've actually practiced and you've struggled.

5
00:16.890 --> 00:18.180
Even if you get stuck,

6
00:18.300 --> 00:22.950
it's still better than just watching the videos and seeing the code written.

7
00:23.190 --> 00:27.210
You have to write code to get good at writing code. All right,

8
00:27.210 --> 00:28.260
enough motivation.

9
00:28.470 --> 00:33.470
Now I'm going to take the normal starting point to guide you through step by

10
00:33.990 --> 00:38.640
step. Now, notice how in the starting project I've included a bunch of to-dos.

11
00:39.060 --> 00:42.810
And one of the nice things about PyCharm is you can view all of the

12
00:42.810 --> 00:47.220
to-dos a found in the file and you can delete them as you check them off,

13
00:47.460 --> 00:51.150
and then you'll be able to see what is remaining. And when you click on it,

14
00:51.150 --> 00:55.470
it takes you directly to that particular to-do's location, which is really neat.

15
00:56.040 --> 00:57.240
Let's start from the top.

16
00:57.510 --> 01:02.510
We want to be able to use the alphavantage.co's APIs in order to get

17
01:03.420 --> 01:07.020
yesterday's closing stock price. So that's the first thing.

18
01:08.610 --> 01:13.260
How do we do that? Well, Alpha vantage is a stock market API

19
01:13.770 --> 01:16.680
and what we have to do is first set up an API key.

20
01:17.970 --> 01:22.500
You can select yourself as a student and then your organization if you have one

21
01:23.280 --> 01:28.110
and then enter your email. Now, once you filled in the form, you can click on

22
01:28.110 --> 01:29.550
get a free API key

23
01:29.940 --> 01:33.300
and you should see your API key show up below.

24
01:33.900 --> 01:37.800
Make sure that you copy this API key and save it in your project.

25
01:38.310 --> 01:41.370
So I'm going to head over here and right at the top,

26
01:41.400 --> 01:46.400
I'm going to add my STOCK_API_KEY as a constant like this.

27
01:47.940 --> 01:48.390
That way

28
01:48.390 --> 01:52.260
I won't forget it because it's actually quite hard to navigate back to this page

29
01:52.590 --> 01:56.190
and get your API key. Now, the next step is of course,

30
01:56.190 --> 01:59.130
the digging through the documentation. Now,

31
01:59.130 --> 02:02.190
for those of you guys who chose the hard and extra hard versions,

32
02:02.220 --> 02:06.510
I didn't really point you towards which part of the API documentation is most

33
02:06.510 --> 02:10.440
relevant, because I hope that if you had chosen to give yourself a challenge,

34
02:10.500 --> 02:14.850
you would appreciate having the opportunity to dig through all of the different

35
02:14.850 --> 02:18.180
things you can do with the API. Now, in our case,

36
02:18.210 --> 02:20.400
we actually want is the daily data.

37
02:21.240 --> 02:24.720
So this API will return the daily data

38
02:25.020 --> 02:29.790
which will give us the open price, the highest price of the day,

39
02:29.790 --> 02:33.210
the lowest price of the day, and also the closing price at the day

40
02:33.240 --> 02:37.470
which is what we're mostly interested in. There's a couple of API parameters

41
02:37.470 --> 02:39.720
which are required; the function

42
02:39.870 --> 02:43.530
which in this case is a time series daily, and also the symbol

43
02:43.560 --> 02:47.370
which is the name of the equity of your choice. In our case,

44
02:47.400 --> 02:50.850
we've already defined that at the top. It's the stock name right here.

45
02:51.510 --> 02:56.370
So let's see how we can fetch yesterday's closing stock price. To start

46
02:56.370 --> 02:59.220
we of course need our trusty requests module.

47
03:01.120 --> 03:03.010
And using requests

48
03:03.040 --> 03:08.040
we're going to fetch from the stock endpoint.

49
03:08.860 --> 03:10.960
So you can see that in their example,

50
03:11.020 --> 03:15.970
the end point is everything up to and not including the question

51
03:15.970 --> 03:18.670
mark. This should be all pretty familiar to you,

52
03:18.760 --> 03:22.600
but you could also just type in simply the one that I've already included.

53
03:22.720 --> 03:27.610
This stock endpoint. Now next we're going to need to add some parameters.

54
03:27.790 --> 03:32.790
So let's create something called our stock_params and let's see what we need

55
03:33.190 --> 03:35.920
to add. So we have to add the function

56
03:36.220 --> 03:39.640
which is equal to time series daily. I'm going to copy that

57
03:39.640 --> 03:41.020
so I don't make any typos.

58
03:41.560 --> 03:46.560
So let's create a Python dictionary and this is going to be the key and value.

59
03:47.710 --> 03:48.160
Next,

60
03:48.160 --> 03:52.870
we have to add our symbol and that is going to be the name of our stock or

61
03:52.870 --> 03:53.703
equity.

62
03:55.780 --> 03:59.830
The symbol, in this case, is simply going to be our stock name TSLA

63
04:00.210 --> 04:01.043
<v 1>Right.</v>

64
04:03.420 --> 04:07.500
<v 0>And stock symbols tend to have a maximum number of four letters.</v>

65
04:07.500 --> 04:12.500
So that's why you see it abbreviated like Snap for Snapchat or TSLA for Tesla.

66
04:14.220 --> 04:17.790
Now the final required parameter is our API key.

67
04:18.300 --> 04:22.800
The whole thing is not going to work unless we authenticate ourselves using the

68
04:22.800 --> 04:24.420
API key that we got just now,

69
04:24.690 --> 04:27.810
which is saved as our STOCK_API_KEY.

70
04:28.860 --> 04:33.270
Now that we've got all three parameters in there, well, we can add it to our

71
04:33.270 --> 04:37.170
request method, and it will just add it as the stock_params.

72
04:37.710 --> 04:42.710
Now let's save this as the response and let's print out what the response

73
04:43.680 --> 04:47.040
actually look like when it's in the format of a JSON.

74
04:48.420 --> 04:53.400
So let's run this code and I'm getting a error that says indentation error

75
04:53.490 --> 04:57.390
unexpected indent. And that's because my stock_params here

76
04:57.390 --> 05:02.390
really should actually be within the same indentation as the starting point

77
05:04.950 --> 05:08.400
like this. So be careful that when you have a to-do that's

78
05:08.400 --> 05:12.540
indented every line that you start afterward is going to be indented as

79
05:12.540 --> 05:13.373
well.

80
05:13.920 --> 05:18.920
So it probably helps to just unindent everything so that they will start at the

81
05:19.260 --> 05:22.530
margin here. So let's try that again.

82
05:25.140 --> 05:28.800
And this time it looks like we're getting back actual data in the format of

83
05:28.800 --> 05:33.030
a JSON. Let's view it using our JSON viewer.

84
05:34.830 --> 05:36.870
You can see that we've got some metadata

85
05:36.900 --> 05:41.880
which basically just describes what we told the API. For example,

86
05:41.880 --> 05:46.880
we wanted the Tesla symbol and we wanted the daily price.

87
05:47.370 --> 05:51.660
But what we're actually interested is in the time series daily data.

88
05:52.110 --> 05:56.280
So you can see it's giving us the data for all of the dates prior to today.

89
05:56.400 --> 05:58.640
So currently it's 22nd of July

90
05:58.940 --> 06:03.940
so we can see the complete data for the 21st of July and the 20th of July. Right

91
06:05.090 --> 06:09.590
now, what we're interested in is yesterday's closing stock price.

92
06:10.190 --> 06:14.600
So this is yesterday and this is the closing stock price.

93
06:14.930 --> 06:19.130
So it's 1,568. How do we get hold of it? Well,

94
06:19.130 --> 06:22.970
we have to step through a number of keys in order to get to this particular

95
06:22.970 --> 06:25.130
value. First,

96
06:25.160 --> 06:30.160
we need to pass in our time series daily and notice how this has brackets and

97
06:30.830 --> 06:35.810
everything. We have to include all of the spaces, all the brackets in our key.

98
06:36.380 --> 06:40.880
Let's say that our data is equal to the response.

99
06:40.910 --> 06:44.870
json, let's go ahead and add a square bracket,

100
06:45.170 --> 06:47.270
and then we can provide the key

101
06:47.660 --> 06:50.600
which I'm actually just going to copy from down here.

102
06:51.440 --> 06:53.510
So I don't make any typos and

103
06:53.540 --> 06:58.190
make sure that I preserve all of the spaces and all of the symbols.

104
06:58.430 --> 07:03.430
So basically everything in between the quotation marks like this.

105
07:04.220 --> 07:06.680
Now, if we print our data,

106
07:07.910 --> 07:11.480
you can see it's stepped into that particular dictionary

107
07:11.810 --> 07:16.730
and it's now giving us all of this data. But this is not a list.

108
07:16.790 --> 07:20.390
As you can see, the data starts out with a curly bracket. In fact,

109
07:20.390 --> 07:24.260
it's a really big dictionary with the key being the date.

110
07:25.670 --> 07:28.940
Now, while I can tap into yesterday's data

111
07:28.970 --> 07:32.390
by simply providing this as the key,

112
07:32.780 --> 07:36.740
this is going to be pretty hard-coded because once I get to tomorrow

113
07:36.740 --> 07:41.450
when it becomes the 23rd, then yesterday's date is going to change.

114
07:41.960 --> 07:46.960
So we have to figure out another way of getting hold of that first value.

115
07:47.960 --> 07:52.960
How about turning this large dictionary into a list where we get each of the

116
07:55.940 --> 08:00.940
pieces of data that's associated with each of the keys? To do that,

117
08:01.070 --> 08:03.380
we're going to need to do a bit of a list comprehension.

118
08:03.770 --> 08:05.810
So I'm going to create a variable called data_list

119
08:06.020 --> 08:09.650
and then I'm going to use my list comprehension keywords.

120
08:09.650 --> 08:12.860
So we did list comprehensions a while back,

121
08:12.890 --> 08:16.280
but this might be a good time to remind yourself of how to use it.

122
08:17.060 --> 08:21.200
It's going to be new item for item in list.

123
08:21.680 --> 08:25.670
Our list is actually not a list. It's in fact a dictionary.

124
08:26.120 --> 08:29.960
So we're going to tap into our data, which is the dictionary,

125
08:30.350 --> 08:33.860
and we're going to call the items method on it.

126
08:34.490 --> 08:37.550
And this is going to give us not just the item,

127
08:37.640 --> 08:40.250
but the key and the value.

128
08:40.730 --> 08:45.080
So we're actually only interested in the values. So we can put that in here

129
08:45.440 --> 08:50.440
so that each item in this new list is composed of only the values and not the

130
08:51.290 --> 08:54.830
keys. Now, if I print this data list,

131
08:56.340 --> 09:00.510
you can see I've now got a list denoted by the square brackets

132
09:00.900 --> 09:05.900
and each item in the list is a dictionary with each day's data. Using that I can

133
09:08.850 --> 09:10.110
get hold of yesterday_

134
09:10.110 --> 09:15.110
data by simply tapping to our data list and getting the object at index zero.

135
09:17.280 --> 09:19.410
Now, once I've got yesterday's data,

136
09:19.470 --> 09:24.470
then it's pretty simple to get yesterday's closing price,

137
09:26.640 --> 09:31.320
because that is simply stored under this particular key. Now again,

138
09:31.350 --> 09:36.060
notice how the entire key is this. It's everything in between the quotation

139
09:36.060 --> 09:39.420
marks, including the number and the dot and all of that.

140
09:39.810 --> 09:43.380
So this is a bit of a strange way of formatting your keys, to be honest.

141
09:43.440 --> 09:47.610
I haven't seen many APIs that do this because it's a little bit confusing to

142
09:47.610 --> 09:50.760
have spaces and dots and numbers,

143
09:50.970 --> 09:53.370
but that's just the way that they decided to do it.

144
09:53.400 --> 09:56.130
So in order to get the closing price,

145
09:56.160 --> 10:01.140
we can tap into yesterday's data, add a set of square brackets, pass in this

146
10:01.140 --> 10:05.880
particular key. So now if we print out yesterday's closing price,

147
10:06.000 --> 10:11.000
you can see we've narrowed down on the exact price of the Tesla stock at

148
10:11.730 --> 10:13.110
yesterday market close.

149
10:15.230 --> 10:15.680
<v 2>Right?</v>

150
10:15.680 --> 10:19.670
<v 0>That's to-do number one completed. We can delete the to-do.</v>

151
10:20.570 --> 10:20.960
Now

152
10:20.960 --> 10:25.910
the next one is to get the day before yesterday's closing stock price.

153
10:26.270 --> 10:28.670
So now that we've actually got this data list,

154
10:28.760 --> 10:33.760
it's pretty simple to do that; day_before_yesterday_

155
10:34.490 --> 10:39.490
data is going to be equal to the data list at index one

156
10:40.190 --> 10:44.840
because remember everything in the list is ordered going backward from today's

157
10:44.840 --> 10:48.650
date. So that is going to be the day before yesterday's data.

158
10:51.380 --> 10:54.830
And then the day before yesterday's closing price,

159
10:54.890 --> 10:56.420
we can fetch that in the same way

160
10:56.420 --> 11:00.380
by passing in that key 4.space close.

161
11:01.130 --> 11:03.200
And just to confirm everything works,

162
11:03.260 --> 11:05.960
let's print out both of the closing prices

163
11:06.260 --> 11:10.790
so you can see that yesterday it closed at $1,568

164
11:11.120 --> 11:14.780
and then the day before it closed at $1,643.

165
11:15.020 --> 11:18.740
So that's actually quite a big drop, almost a hundred dollars.

166
11:20.600 --> 11:22.580
That's to-do number two done.

167
11:22.580 --> 11:27.580
We've managed to get the day before yesterday's closing stock price. To-do number

168
11:27.980 --> 11:28.813
three

169
11:28.940 --> 11:33.050
involves finding the positive difference between step one and two,

170
11:33.350 --> 11:37.370
so yesterday's closing price and the day before yesterday's closing price.

171
11:38.090 --> 11:39.320
And it gives us a hint.

172
11:40.790 --> 11:44.360
Now the hint takes us to the Python abs function,

173
11:44.450 --> 11:46.220
which we've also seen before.

174
11:46.880 --> 11:50.300
It's a function that returns the absolute value of a number.

175
11:50.300 --> 11:54.700
So it basically just gets rid of any negative sign in front of a number.

176
11:55.630 --> 11:59.860
And this is what we need in order to complete to-do number three.

177
11:59.880 --> 12:00.713
<v 2>Right?</v>

178
12:02.130 --> 12:07.130
<v 0>The difference is going to be yesterday's closing price subtract by the day</v>

179
12:08.670 --> 12:12.180
before yesterday's closing price. Now,

180
12:12.240 --> 12:16.800
because these two are strings, if we actually run the code right now,

181
12:16.830 --> 12:18.510
you're going to get an error.

182
12:19.380 --> 12:23.310
And the error tells you that you can't subtract between a string and a string.

183
12:23.910 --> 12:26.250
We know that these numbers look like this,

184
12:26.310 --> 12:31.310
so we can quite easily convert them into a floating point numbers just by

185
12:31.350 --> 12:35.850
wrapping the float function around them. If we print that out,

186
12:35.880 --> 12:39.960
you can see that it's actually going to be a negative number.

187
12:40.500 --> 12:42.780
And the reason is because this is yesterday's price,

188
12:42.810 --> 12:44.940
this is the day before yesterday's price.

189
12:45.330 --> 12:48.660
The price actually went down by $74.

190
12:49.290 --> 12:53.700
What if we want to get the positive difference between one and two?

191
12:55.770 --> 12:56.160
Well,

192
12:56.160 --> 13:01.050
this is where the abs or absolute value function comes in handy.

193
13:01.260 --> 13:05.490
If we wrap all of that around the absolute function, now, if I run it,

194
13:05.640 --> 13:08.970
you can see we're getting a positive value instead.

195
13:10.590 --> 13:12.600
Now that's step three completed.

196
13:12.720 --> 13:17.370
We found the positive difference between yesterday's closing price and the day

197
13:17.370 --> 13:19.470
before yesterday's closing price.

198
13:20.610 --> 13:22.770
Now let's tackle step number four.

199
13:23.400 --> 13:27.570
We want to be able to work out the percentage difference in price between the

200
13:27.570 --> 13:31.980
closing price yesterday and the closing price the day before yesterday.

201
13:33.390 --> 13:38.390
The diff_percent is going to be the difference divided by the closing price

202
13:40.920 --> 13:41.753
yesterday.

203
13:43.830 --> 13:46.860
And in order to turn this into a percentage,

204
13:46.920 --> 13:51.210
we're going to have to multiply it by 100. But again,

205
13:51.240 --> 13:56.040
notice that this is a floating point number because it was calculated here,

206
13:56.370 --> 13:58.290
but this is still the string.

207
13:58.770 --> 14:02.670
So we're going to have to turn that into a float for this calculation to

208
14:02.670 --> 14:06.510
actually go through. And once it does that, we're going to print it out.

209
14:08.370 --> 14:12.870
We've got about a 4.75% difference.

210
14:14.220 --> 14:17.700
Now we can tackle to-do number five,

211
14:18.150 --> 14:22.350
which is if the percentage here is greater than five,

212
14:22.680 --> 14:27.600
then we're going to print get news. This is pretty simple.

213
14:27.630 --> 14:31.650
All we have to do is check if the diff_percent is greater than five,

214
14:32.130 --> 14:36.810
in which case we're going to print get news. Now,

215
14:36.810 --> 14:39.630
depending on the time when you're completing this project,

216
14:39.960 --> 14:43.290
the stock price of Tesla may not show a difference

217
14:43.320 --> 14:47.880
that's greater than five. Because in order to continue testing our code

218
14:47.910 --> 14:50.670
we're going to need this to be true,

219
14:51.110 --> 14:54.530
then we can change this number to a smaller number,

220
14:54.560 --> 14:57.890
just so that we can actually get it to work. So my case,

221
14:57.890 --> 15:02.150
the difference is 4.75. So I'm actually just going to change this down to 4.

222
15:04.130 --> 15:06.380
And now if I run all of this,

223
15:06.710 --> 15:09.680
then you can see it's going to print get news.

224
15:10.190 --> 15:12.350
So that is what we're going to do on the next lesson

225
15:12.620 --> 15:14.450
in the second part of the solution.