1
00:00:00,180 --> 00:00:02,310
Now I'm going to walk you through the solution.

2
00:00:02,790 --> 00:00:06,870
And I want to make sure that you've actually given this a go before you look

3
00:00:06,870 --> 00:00:10,920
through the solution, because you can watch me type in code all day long,

4
00:00:10,950 --> 00:00:13,410
and it's still not going to get you where you want to be.

5
00:00:13,470 --> 00:00:16,530
So make sure you've actually practiced and you've struggled.

6
00:00:16,890 --> 00:00:18,180
Even if you get stuck,

7
00:00:18,300 --> 00:00:22,950
it's still better than just watching the videos and seeing the code written.

8
00:00:23,190 --> 00:00:27,210
You have to write code to get good at writing code. All right,

9
00:00:27,210 --> 00:00:28,260
enough motivation.

10
00:00:28,470 --> 00:00:33,470
Now I'm going to take the normal starting point to guide you through step by

11
00:00:33,990 --> 00:00:38,640
step. Now, notice how in the starting project I've included a bunch of to-dos.

12
00:00:39,060 --> 00:00:42,810
And one of the nice things about PyCharm is you can view all of the

13
00:00:42,810 --> 00:00:47,220
to-dos a found in the file and you can delete them as you check them off,

14
00:00:47,460 --> 00:00:51,150
and then you'll be able to see what is remaining. And when you click on it,

15
00:00:51,150 --> 00:00:55,470
it takes you directly to that particular to-do's location, which is really neat.

16
00:00:56,040 --> 00:00:57,240
Let's start from the top.

17
00:00:57,510 --> 00:01:02,510
We want to be able to use the alphavantage.co's APIs in order to get

18
00:01:03,420 --> 00:01:07,020
yesterday's closing stock price. So that's the first thing.

19
00:01:08,610 --> 00:01:13,260
How do we do that? Well, Alpha vantage is a stock market API

20
00:01:13,770 --> 00:01:16,680
and what we have to do is first set up an API key.

21
00:01:17,970 --> 00:01:22,500
You can select yourself as a student and then your organization if you have one

22
00:01:23,280 --> 00:01:28,110
and then enter your email. Now, once you filled in the form, you can click on

23
00:01:28,110 --> 00:01:29,550
get a free API key

24
00:01:29,940 --> 00:01:33,300
and you should see your API key show up below.

25
00:01:33,900 --> 00:01:37,800
Make sure that you copy this API key and save it in your project.

26
00:01:38,310 --> 00:01:41,370
So I'm going to head over here and right at the top,

27
00:01:41,400 --> 00:01:46,400
I'm going to add my STOCK_API_KEY as a constant like this.

28
00:01:47,940 --> 00:01:48,390
That way

29
00:01:48,390 --> 00:01:52,260
I won't forget it because it's actually quite hard to navigate back to this page

30
00:01:52,590 --> 00:01:56,190
and get your API key. Now, the next step is of course,

31
00:01:56,190 --> 00:01:59,130
the digging through the documentation. Now,

32
00:01:59,130 --> 00:02:02,190
for those of you guys who chose the hard and extra hard versions,

33
00:02:02,220 --> 00:02:06,510
I didn't really point you towards which part of the API documentation is most

34
00:02:06,510 --> 00:02:10,440
relevant, because I hope that if you had chosen to give yourself a challenge,

35
00:02:10,500 --> 00:02:14,850
you would appreciate having the opportunity to dig through all of the different

36
00:02:14,850 --> 00:02:18,180
things you can do with the API. Now, in our case,

37
00:02:18,210 --> 00:02:20,400
we actually want is the daily data.

38
00:02:21,240 --> 00:02:24,720
So this API will return the daily data

39
00:02:25,020 --> 00:02:29,790
which will give us the open price, the highest price of the day,

40
00:02:29,790 --> 00:02:33,210
the lowest price of the day, and also the closing price at the day

41
00:02:33,240 --> 00:02:37,470
which is what we're mostly interested in. There's a couple of API parameters

42
00:02:37,470 --> 00:02:39,720
which are required; the function

43
00:02:39,870 --> 00:02:43,530
which in this case is a time series daily, and also the symbol

44
00:02:43,560 --> 00:02:47,370
which is the name of the equity of your choice. In our case,

45
00:02:47,400 --> 00:02:50,850
we've already defined that at the top. It's the stock name right here.

46
00:02:51,510 --> 00:02:56,370
So let's see how we can fetch yesterday's closing stock price. To start

47
00:02:56,370 --> 00:02:59,220
we of course need our trusty requests module.

48
00:03:01,120 --> 00:03:03,010
And using requests

49
00:03:03,040 --> 00:03:08,040
we're going to fetch from the stock endpoint.

50
00:03:08,860 --> 00:03:10,960
So you can see that in their example,

51
00:03:11,020 --> 00:03:15,970
the end point is everything up to and not including the question

52
00:03:15,970 --> 00:03:18,670
mark. This should be all pretty familiar to you,

53
00:03:18,760 --> 00:03:22,600
but you could also just type in simply the one that I've already included.

54
00:03:22,720 --> 00:03:27,610
This stock endpoint. Now next we're going to need to add some parameters.

55
00:03:27,790 --> 00:03:32,790
So let's create something called our stock_params and let's see what we need

56
00:03:33,190 --> 00:03:35,920
to add. So we have to add the function

57
00:03:36,220 --> 00:03:39,640
which is equal to time series daily. I'm going to copy that

58
00:03:39,640 --> 00:03:41,020
so I don't make any typos.

59
00:03:41,560 --> 00:03:46,560
So let's create a Python dictionary and this is going to be the key and value.

60
00:03:47,710 --> 00:03:48,160
Next,

61
00:03:48,160 --> 00:03:52,870
we have to add our symbol and that is going to be the name of our stock or

62
00:03:52,870 --> 00:03:53,703
equity.

63
00:03:55,780 --> 00:03:59,830
The symbol, in this case, is simply going to be our stock name TSLA

64
00:04:00,210 --> 00:04:01,043
Right.

65
00:04:03,420 --> 00:04:07,500
And stock symbols tend to have a maximum number of four letters.

66
00:04:07,500 --> 00:04:12,500
So that's why you see it abbreviated like Snap for Snapchat or TSLA for Tesla.

67
00:04:14,220 --> 00:04:17,790
Now the final required parameter is our API key.

68
00:04:18,300 --> 00:04:22,800
The whole thing is not going to work unless we authenticate ourselves using the

69
00:04:22,800 --> 00:04:24,420
API key that we got just now,

70
00:04:24,690 --> 00:04:27,810
which is saved as our STOCK_API_KEY.

71
00:04:28,860 --> 00:04:33,270
Now that we've got all three parameters in there, well, we can add it to our

72
00:04:33,270 --> 00:04:37,170
request method, and it will just add it as the stock_params.

73
00:04:37,710 --> 00:04:42,710
Now let's save this as the response and let's print out what the response

74
00:04:43,680 --> 00:04:47,040
actually look like when it's in the format of a JSON.

75
00:04:48,420 --> 00:04:53,400
So let's run this code and I'm getting a error that says indentation error

76
00:04:53,490 --> 00:04:57,390
unexpected indent. And that's because my stock_params here

77
00:04:57,390 --> 00:05:02,390
really should actually be within the same indentation as the starting point

78
00:05:04,950 --> 00:05:08,400
like this. So be careful that when you have a to-do that's

79
00:05:08,400 --> 00:05:12,540
indented every line that you start afterward is going to be indented as

80
00:05:12,540 --> 00:05:13,373
well.

81
00:05:13,920 --> 00:05:18,920
So it probably helps to just unindent everything so that they will start at the

82
00:05:19,260 --> 00:05:22,530
margin here. So let's try that again.

83
00:05:25,140 --> 00:05:28,800
And this time it looks like we're getting back actual data in the format of

84
00:05:28,800 --> 00:05:33,030
a JSON. Let's view it using our JSON viewer.

85
00:05:34,830 --> 00:05:36,870
You can see that we've got some metadata

86
00:05:36,900 --> 00:05:41,880
which basically just describes what we told the API. For example,

87
00:05:41,880 --> 00:05:46,880
we wanted the Tesla symbol and we wanted the daily price.

88
00:05:47,370 --> 00:05:51,660
But what we're actually interested is in the time series daily data.

89
00:05:52,110 --> 00:05:56,280
So you can see it's giving us the data for all of the dates prior to today.

90
00:05:56,400 --> 00:05:58,640
So currently it's 22nd of July

91
00:05:58,940 --> 00:06:03,940
so we can see the complete data for the 21st of July and the 20th of July. Right

92
00:06:05,090 --> 00:06:09,590
now, what we're interested in is yesterday's closing stock price.

93
00:06:10,190 --> 00:06:14,600
So this is yesterday and this is the closing stock price.

94
00:06:14,930 --> 00:06:19,130
So it's 1,568. How do we get hold of it? Well,

95
00:06:19,130 --> 00:06:22,970
we have to step through a number of keys in order to get to this particular

96
00:06:22,970 --> 00:06:25,130
value. First,

97
00:06:25,160 --> 00:06:30,160
we need to pass in our time series daily and notice how this has brackets and

98
00:06:30,830 --> 00:06:35,810
everything. We have to include all of the spaces, all the brackets in our key.

99
00:06:36,380 --> 00:06:40,880
Let's say that our data is equal to the response.

100
00:06:40,910 --> 00:06:44,870
json, let's go ahead and add a square bracket,

101
00:06:45,170 --> 00:06:47,270
and then we can provide the key

102
00:06:47,660 --> 00:06:50,600
which I'm actually just going to copy from down here.

103
00:06:51,440 --> 00:06:53,510
So I don't make any typos and

104
00:06:53,540 --> 00:06:58,190
make sure that I preserve all of the spaces and all of the symbols.

105
00:06:58,430 --> 00:07:03,430
So basically everything in between the quotation marks like this.

106
00:07:04,220 --> 00:07:06,680
Now, if we print our data,

107
00:07:07,910 --> 00:07:11,480
you can see it's stepped into that particular dictionary

108
00:07:11,810 --> 00:07:16,730
and it's now giving us all of this data. But this is not a list.

109
00:07:16,790 --> 00:07:20,390
As you can see, the data starts out with a curly bracket. In fact,

110
00:07:20,390 --> 00:07:24,260
it's a really big dictionary with the key being the date.

111
00:07:25,670 --> 00:07:28,940
Now, while I can tap into yesterday's data

112
00:07:28,970 --> 00:07:32,390
by simply providing this as the key,

113
00:07:32,780 --> 00:07:36,740
this is going to be pretty hard-coded because once I get to tomorrow

114
00:07:36,740 --> 00:07:41,450
when it becomes the 23rd, then yesterday's date is going to change.

115
00:07:41,960 --> 00:07:46,960
So we have to figure out another way of getting hold of that first value.

116
00:07:47,960 --> 00:07:52,960
How about turning this large dictionary into a list where we get each of the

117
00:07:55,940 --> 00:08:00,940
pieces of data that's associated with each of the keys? To do that,

118
00:08:01,070 --> 00:08:03,380
we're going to need to do a bit of a list comprehension.

119
00:08:03,770 --> 00:08:05,810
So I'm going to create a variable called data_list

120
00:08:06,020 --> 00:08:09,650
and then I'm going to use my list comprehension keywords.

121
00:08:09,650 --> 00:08:12,860
So we did list comprehensions a while back,

122
00:08:12,890 --> 00:08:16,280
but this might be a good time to remind yourself of how to use it.

123
00:08:17,060 --> 00:08:21,200
It's going to be new item for item in list.

124
00:08:21,680 --> 00:08:25,670
Our list is actually not a list. It's in fact a dictionary.

125
00:08:26,120 --> 00:08:29,960
So we're going to tap into our data, which is the dictionary,

126
00:08:30,350 --> 00:08:33,860
and we're going to call the items method on it.

127
00:08:34,490 --> 00:08:37,549
And this is going to give us not just the item,

128
00:08:37,640 --> 00:08:40,250
but the key and the value.

129
00:08:40,730 --> 00:08:45,080
So we're actually only interested in the values. So we can put that in here

130
00:08:45,440 --> 00:08:50,440
so that each item in this new list is composed of only the values and not the

131
00:08:51,290 --> 00:08:54,830
keys. Now, if I print this data list,

132
00:08:56,340 --> 00:09:00,510
you can see I've now got a list denoted by the square brackets

133
00:09:00,900 --> 00:09:05,900
and each item in the list is a dictionary with each day's data. Using that I can

134
00:09:08,850 --> 00:09:10,110
get hold of yesterday_

135
00:09:10,110 --> 00:09:15,110
data by simply tapping to our data list and getting the object at index zero.

136
00:09:17,280 --> 00:09:19,410
Now, once I've got yesterday's data,

137
00:09:19,470 --> 00:09:24,470
then it's pretty simple to get yesterday's closing price,

138
00:09:26,640 --> 00:09:31,320
because that is simply stored under this particular key. Now again,

139
00:09:31,350 --> 00:09:36,060
notice how the entire key is this. It's everything in between the quotation

140
00:09:36,060 --> 00:09:39,420
marks, including the number and the dot and all of that.

141
00:09:39,810 --> 00:09:43,380
So this is a bit of a strange way of formatting your keys, to be honest.

142
00:09:43,440 --> 00:09:47,610
I haven't seen many APIs that do this because it's a little bit confusing to

143
00:09:47,610 --> 00:09:50,760
have spaces and dots and numbers,

144
00:09:50,970 --> 00:09:53,370
but that's just the way that they decided to do it.

145
00:09:53,400 --> 00:09:56,130
So in order to get the closing price,

146
00:09:56,160 --> 00:10:01,140
we can tap into yesterday's data, add a set of square brackets, pass in this

147
00:10:01,140 --> 00:10:05,880
particular key. So now if we print out yesterday's closing price,

148
00:10:06,000 --> 00:10:11,000
you can see we've narrowed down on the exact price of the Tesla stock at

149
00:10:11,730 --> 00:10:13,110
yesterday market close.

150
00:10:15,230 --> 00:10:15,680
Right?

151
00:10:15,680 --> 00:10:19,670
That's to-do number one completed. We can delete the to-do.

152
00:10:20,570 --> 00:10:20,960
Now

153
00:10:20,960 --> 00:10:25,910
the next one is to get the day before yesterday's closing stock price.

154
00:10:26,270 --> 00:10:28,670
So now that we've actually got this data list,

155
00:10:28,760 --> 00:10:33,760
it's pretty simple to do that; day_before_yesterday_

156
00:10:34,490 --> 00:10:39,490
data is going to be equal to the data list at index one

157
00:10:40,190 --> 00:10:44,840
because remember everything in the list is ordered going backward from today's

158
00:10:44,840 --> 00:10:48,650
date. So that is going to be the day before yesterday's data.

159
00:10:51,380 --> 00:10:54,830
And then the day before yesterday's closing price,

160
00:10:54,890 --> 00:10:56,420
we can fetch that in the same way

161
00:10:56,420 --> 00:11:00,380
by passing in that key 4.space close.

162
00:11:01,130 --> 00:11:03,200
And just to confirm everything works,

163
00:11:03,260 --> 00:11:05,960
let's print out both of the closing prices

164
00:11:06,260 --> 00:11:10,790
so you can see that yesterday it closed at $1,568

165
00:11:11,120 --> 00:11:14,780
and then the day before it closed at $1,643.

166
00:11:15,020 --> 00:11:18,740
So that's actually quite a big drop, almost a hundred dollars.

167
00:11:20,600 --> 00:11:22,580
That's to-do number two done.

168
00:11:22,580 --> 00:11:27,580
We've managed to get the day before yesterday's closing stock price. To-do number

169
00:11:27,980 --> 00:11:28,813
three

170
00:11:28,940 --> 00:11:33,050
involves finding the positive difference between step one and two,

171
00:11:33,350 --> 00:11:37,370
so yesterday's closing price and the day before yesterday's closing price.

172
00:11:38,090 --> 00:11:39,320
And it gives us a hint.

173
00:11:40,790 --> 00:11:44,360
Now the hint takes us to the Python abs function,

174
00:11:44,450 --> 00:11:46,220
which we've also seen before.

175
00:11:46,880 --> 00:11:50,300
It's a function that returns the absolute value of a number.

176
00:11:50,300 --> 00:11:54,700
So it basically just gets rid of any negative sign in front of a number.

177
00:11:55,630 --> 00:11:59,860
And this is what we need in order to complete to-do number three.

178
00:11:59,880 --> 00:12:00,713
Right?

179
00:12:02,130 --> 00:12:07,130
The difference is going to be yesterday's closing price subtract by the day

180
00:12:08,670 --> 00:12:12,180
before yesterday's closing price. Now,

181
00:12:12,240 --> 00:12:16,800
because these two are strings, if we actually run the code right now,

182
00:12:16,830 --> 00:12:18,510
you're going to get an error.

183
00:12:19,380 --> 00:12:23,310
And the error tells you that you can't subtract between a string and a string.

184
00:12:23,910 --> 00:12:26,250
We know that these numbers look like this,

185
00:12:26,310 --> 00:12:31,310
so we can quite easily convert them into a floating point numbers just by

186
00:12:31,350 --> 00:12:35,850
wrapping the float function around them. If we print that out,

187
00:12:35,880 --> 00:12:39,960
you can see that it's actually going to be a negative number.

188
00:12:40,500 --> 00:12:42,780
And the reason is because this is yesterday's price,

189
00:12:42,810 --> 00:12:44,940
this is the day before yesterday's price.

190
00:12:45,330 --> 00:12:48,660
The price actually went down by $74.

191
00:12:49,290 --> 00:12:53,700
What if we want to get the positive difference between one and two?

192
00:12:55,770 --> 00:12:56,160
Well,

193
00:12:56,160 --> 00:13:01,050
this is where the abs or absolute value function comes in handy.

194
00:13:01,260 --> 00:13:05,490
If we wrap all of that around the absolute function, now, if I run it,

195
00:13:05,640 --> 00:13:08,970
you can see we're getting a positive value instead.

196
00:13:10,590 --> 00:13:12,600
Now that's step three completed.

197
00:13:12,720 --> 00:13:17,370
We found the positive difference between yesterday's closing price and the day

198
00:13:17,370 --> 00:13:19,470
before yesterday's closing price.

199
00:13:20,610 --> 00:13:22,770
Now let's tackle step number four.

200
00:13:23,400 --> 00:13:27,570
We want to be able to work out the percentage difference in price between the

201
00:13:27,570 --> 00:13:31,980
closing price yesterday and the closing price the day before yesterday.

202
00:13:33,390 --> 00:13:38,390
The diff_percent is going to be the difference divided by the closing price

203
00:13:40,920 --> 00:13:41,753
yesterday.

204
00:13:43,830 --> 00:13:46,860
And in order to turn this into a percentage,

205
00:13:46,920 --> 00:13:51,210
we're going to have to multiply it by 100. But again,

206
00:13:51,240 --> 00:13:56,040
notice that this is a floating point number because it was calculated here,

207
00:13:56,370 --> 00:13:58,290
but this is still the string.

208
00:13:58,770 --> 00:14:02,670
So we're going to have to turn that into a float for this calculation to

209
00:14:02,670 --> 00:14:06,510
actually go through. And once it does that, we're going to print it out.

210
00:14:08,370 --> 00:14:12,870
We've got about a 4.75% difference.

211
00:14:14,220 --> 00:14:17,700
Now we can tackle to-do number five,

212
00:14:18,150 --> 00:14:22,350
which is if the percentage here is greater than five,

213
00:14:22,680 --> 00:14:27,600
then we're going to print get news. This is pretty simple.

214
00:14:27,630 --> 00:14:31,650
All we have to do is check if the diff_percent is greater than five,

215
00:14:32,130 --> 00:14:36,810
in which case we're going to print get news. Now,

216
00:14:36,810 --> 00:14:39,630
depending on the time when you're completing this project,

217
00:14:39,960 --> 00:14:43,290
the stock price of Tesla may not show a difference

218
00:14:43,320 --> 00:14:47,880
that's greater than five. Because in order to continue testing our code

219
00:14:47,910 --> 00:14:50,670
we're going to need this to be true,

220
00:14:51,110 --> 00:14:54,530
then we can change this number to a smaller number,

221
00:14:54,560 --> 00:14:57,890
just so that we can actually get it to work. So my case,

222
00:14:57,890 --> 00:15:02,150
the difference is 4.75. So I'm actually just going to change this down to 4.

223
00:15:04,130 --> 00:15:06,380
And now if I run all of this,

224
00:15:06,710 --> 00:15:09,680
then you can see it's going to print get news.

225
00:15:10,190 --> 00:15:12,350
So that is what we're going to do on the next lesson

226
00:15:12,620 --> 00:15:14,450
in the second part of the solution.

