Now I'm going to walk you through the solution.
And I want to make sure that you've actually given this a go before you look
through the solution, because you can watch me type in code all day long,
and it's still not going to get you where you want to be.
So make sure you've actually practiced and you've struggled.
Even if you get stuck,
it's still better than just watching the videos and seeing the code written.
You have to write code to get good at writing code. All right,
enough motivation.
Now I'm going to take the normal starting point to guide you through step by
step. Now, notice how in the starting project I've included a bunch of to-dos.
And one of the nice things about PyCharm is you can view all of the
to-dos a found in the file and you can delete them as you check them off,
and then you'll be able to see what is remaining. And when you click on it,
it takes you directly to that particular to-do's location, which is really neat.
Let's start from the top.
We want to be able to use the alphavantage.co's APIs in order to get
yesterday's closing stock price. So that's the first thing.
How do we do that? Well, Alpha vantage is a stock market API
and what we have to do is first set up an API key.
You can select yourself as a student and then your organization if you have one
and then enter your email. Now, once you filled in the form, you can click on
get a free API key
and you should see your API key show up below.
Make sure that you copy this API key and save it in your project.
So I'm going to head over here and right at the top,
I'm going to add my STOCK_API_KEY as a constant like this.
That way
I won't forget it because it's actually quite hard to navigate back to this page
and get your API key. Now, the next step is of course,
the digging through the documentation. Now,
for those of you guys who chose the hard and extra hard versions,
I didn't really point you towards which part of the API documentation is most
relevant, because I hope that if you had chosen to give yourself a challenge,
you would appreciate having the opportunity to dig through all of the different
things you can do with the API. Now, in our case,
we actually want is the daily data.
So this API will return the daily data
which will give us the open price, the highest price of the day,
the lowest price of the day, and also the closing price at the day
which is what we're mostly interested in. There's a couple of API parameters
which are required; the function
which in this case is a time series daily, and also the symbol
which is the name of the equity of your choice. In our case,
we've already defined that at the top. It's the stock name right here.
So let's see how we can fetch yesterday's closing stock price. To start
we of course need our trusty requests module.
And using requests
we're going to fetch from the stock endpoint.
So you can see that in their example,
the end point is everything up to and not including the question
mark. This should be all pretty familiar to you,
but you could also just type in simply the one that I've already included.
This stock endpoint. Now next we're going to need to add some parameters.
So let's create something called our stock_params and let's see what we need
to add. So we have to add the function
which is equal to time series daily. I'm going to copy that
so I don't make any typos.
So let's create a Python dictionary and this is going to be the key and value.
Next,
we have to add our symbol and that is going to be the name of our stock or
equity.
The symbol, in this case, is simply going to be our stock name TSLA
Right.
And stock symbols tend to have a maximum number of four letters.
So that's why you see it abbreviated like Snap for Snapchat or TSLA for Tesla.
Now the final required parameter is our API key.
The whole thing is not going to work unless we authenticate ourselves using the
API key that we got just now,
which is saved as our STOCK_API_KEY.
Now that we've got all three parameters in there, well, we can add it to our
request method, and it will just add it as the stock_params.
Now let's save this as the response and let's print out what the response
actually look like when it's in the format of a JSON.
So let's run this code and I'm getting a error that says indentation error
unexpected indent. And that's because my stock_params here
really should actually be within the same indentation as the starting point
like this. So be careful that when you have a to-do that's
indented every line that you start afterward is going to be indented as
well.
So it probably helps to just unindent everything so that they will start at the
margin here. So let's try that again.
And this time it looks like we're getting back actual data in the format of
a JSON. Let's view it using our JSON viewer.
You can see that we've got some metadata
which basically just describes what we told the API. For example,
we wanted the Tesla symbol and we wanted the daily price.
But what we're actually interested is in the time series daily data.
So you can see it's giving us the data for all of the dates prior to today.
So currently it's 22nd of July
so we can see the complete data for the 21st of July and the 20th of July. Right
now, what we're interested in is yesterday's closing stock price.
So this is yesterday and this is the closing stock price.
So it's 1,568. How do we get hold of it? Well,
we have to step through a number of keys in order to get to this particular
value. First,
we need to pass in our time series daily and notice how this has brackets and
everything. We have to include all of the spaces, all the brackets in our key.
Let's say that our data is equal to the response.
json, let's go ahead and add a square bracket,
and then we can provide the key
which I'm actually just going to copy from down here.
So I don't make any typos and
make sure that I preserve all of the spaces and all of the symbols.
So basically everything in between the quotation marks like this.
Now, if we print our data,
you can see it's stepped into that particular dictionary
and it's now giving us all of this data. But this is not a list.
As you can see, the data starts out with a curly bracket. In fact,
it's a really big dictionary with the key being the date.
Now, while I can tap into yesterday's data
by simply providing this as the key,
this is going to be pretty hard-coded because once I get to tomorrow
when it becomes the 23rd, then yesterday's date is going to change.
So we have to figure out another way of getting hold of that first value.
How about turning this large dictionary into a list where we get each of the
pieces of data that's associated with each of the keys? To do that,
we're going to need to do a bit of a list comprehension.
So I'm going to create a variable called data_list
and then I'm going to use my list comprehension keywords.
So we did list comprehensions a while back,
but this might be a good time to remind yourself of how to use it.
It's going to be new item for item in list.
Our list is actually not a list. It's in fact a dictionary.
So we're going to tap into our data, which is the dictionary,
and we're going to call the items method on it.
And this is going to give us not just the item,
but the key and the value.
So we're actually only interested in the values. So we can put that in here
so that each item in this new list is composed of only the values and not the
keys. Now, if I print this data list,
you can see I've now got a list denoted by the square brackets
and each item in the list is a dictionary with each day's data. Using that I can
get hold of yesterday_
data by simply tapping to our data list and getting the object at index zero.
Now, once I've got yesterday's data,
then it's pretty simple to get yesterday's closing price,
because that is simply stored under this particular key. Now again,
notice how the entire key is this. It's everything in between the quotation
marks, including the number and the dot and all of that.
So this is a bit of a strange way of formatting your keys, to be honest.
I haven't seen many APIs that do this because it's a little bit confusing to
have spaces and dots and numbers,
but that's just the way that they decided to do it.
So in order to get the closing price,
we can tap into yesterday's data, add a set of square brackets, pass in this
particular key. So now if we print out yesterday's closing price,
you can see we've narrowed down on the exact price of the Tesla stock at
yesterday market close.
Right?
That's to-do number one completed. We can delete the to-do.
Now
the next one is to get the day before yesterday's closing stock price.
So now that we've actually got this data list,
it's pretty simple to do that; day_before_yesterday_
data is going to be equal to the data list at index one
because remember everything in the list is ordered going backward from today's
date. So that is going to be the day before yesterday's data.
And then the day before yesterday's closing price,
we can fetch that in the same way
by passing in that key 4.space close.
And just to confirm everything works,
let's print out both of the closing prices
so you can see that yesterday it closed at $1,568
and then the day before it closed at $1,643.
So that's actually quite a big drop, almost a hundred dollars.
That's to-do number two done.
We've managed to get the day before yesterday's closing stock price. To-do number
three
involves finding the positive difference between step one and two,
so yesterday's closing price and the day before yesterday's closing price.
And it gives us a hint.
Now the hint takes us to the Python abs function,
which we've also seen before.
It's a function that returns the absolute value of a number.
So it basically just gets rid of any negative sign in front of a number.
And this is what we need in order to complete to-do number three.
Right?
The difference is going to be yesterday's closing price subtract by the day
before yesterday's closing price. Now,
because these two are strings, if we actually run the code right now,
you're going to get an error.
And the error tells you that you can't subtract between a string and a string.
We know that these numbers look like this,
so we can quite easily convert them into a floating point numbers just by
wrapping the float function around them. If we print that out,
you can see that it's actually going to be a negative number.
And the reason is because this is yesterday's price,
this is the day before yesterday's price.
The price actually went down by $74.
What if we want to get the positive difference between one and two?
Well,
this is where the abs or absolute value function comes in handy.
If we wrap all of that around the absolute function, now, if I run it,
you can see we're getting a positive value instead.
Now that's step three completed.
We found the positive difference between yesterday's closing price and the day
before yesterday's closing price.
Now let's tackle step number four.
We want to be able to work out the percentage difference in price between the
closing price yesterday and the closing price the day before yesterday.
The diff_percent is going to be the difference divided by the closing price
yesterday.
And in order to turn this into a percentage,
we're going to have to multiply it by 100. But again,
notice that this is a floating point number because it was calculated here,
but this is still the string.
So we're going to have to turn that into a float for this calculation to
actually go through. And once it does that, we're going to print it out.
We've got about a 4.75% difference.
Now we can tackle to-do number five,
which is if the percentage here is greater than five,
then we're going to print get news. This is pretty simple.
All we have to do is check if the diff_percent is greater than five,
in which case we're going to print get news. Now,
depending on the time when you're completing this project,
the stock price of Tesla may not show a difference
that's greater than five. Because in order to continue testing our code
we're going to need this to be true,
then we can change this number to a smaller number,
just so that we can actually get it to work. So my case,
the difference is 4.75. So I'm actually just going to change this down to 4.
And now if I run all of this,
then you can see it's going to print get news.
So that is what we're going to do on the next lesson
in the second part of the solution.