Download Financial Data in R
5K views
May 16, 2024
To work with financial data we first need to download it. There is a package called quantmod that can be used to download financial data in R. Website: thedatahall.com As an Amazon Associate, I earn from qualifying purchases.
View Video Transcript
0:00
Welcome to the Data Hall YouTube channel
0:02
In today's video, we are going to look into how do we download stock prices data in R
0:07
So to download stock prices data in R, we use the quant mode package
0:13
So if you haven't installed this package, you can install it using this line of code
0:17
I have already installed it. So I'm not going to reinstall it. We are going to just load this quant mode library
0:24
And then we are going to look into this function, which is called get symbols
0:30
Remember, the S is a capital S. So, Gat symbols, what it does is it asks us for three main parameters
0:38
First, it asks us for the symbol, the ticker that you want to download
0:43
So for example, we want to download the data for the Apple stock
0:47
Then the time period from where you do, you want the data to start
0:52
So for example, we want the data to start from 1st of January 2010 till 31st of
1:00
December 2023. If I press control enter it would take if a couple of seconds and the data
1:08
would be downloaded Now do remember that this data would be downloaded in an XTS object XTS object is a kind of an object where we have the dates in the index the row index right
1:23
So we have the open price, high price, low closing volume and the adjusted price of the Apple stock
1:30
So each time it would download an XTS object of its own, right
1:34
We would have a video on XTS object and how to work with XTS object in a couple of days
1:41
And we can also plot this data. So let's say I am going to plot the closing price
1:48
just to give you an idea of how the closing price are going from this time period
1:52
Now we can define the starting and ending date in a vector and then pass that date as a parameter
2:03
in this get symbol function, right? And we can also specify this source
2:09
So by default the source is Yahoo, but you can explicitly specify it or if you want to change the source
2:15
and you want to download it from somewhere else, then you can also download it from the other source
2:21
Now, there is a function called Today. It is coming from the Tidewurst Library within Tidewurst Library there is a library for working with dates but I just going to load the Tidiverse library
2:41
And once we load this library, we have this today's function. And what today would do is it would give us today's date
2:49
So it is 7th of May, 2024. So we want to download, say, one year of data
2:57
starting from today. So what we do is minus 365 and that is one year of data, maybe more than one year of data because, yeah
3:06
So, and we also assign an auto assign over here. Let me execute this
3:12
Now we are going to download the Microsoft data. And what this auto assign does is instead of creating an object over here, it just presents the data in the console, right
3:26
So remember, it starts. from 8th of May 2022 till the 6th of May 2024 so it is exactly the one year because
3:39
it is 24 is a leap year so we should have given 365 365 instead of 365 so auto sign do not create an object so if we didn had also auto sign equal to false rather it should be equal to true If we do that then it would download the data instead of presenting it in the console it would create an object of its own You can also download the index So SSSP 500 index is represented by this sticker and we can download that And we can also create a vector of all the all the tickers that we want to download So we want to download after
4:15
Microsoft and SPY. And although we have it over here, these two over here
4:22
in our environment, but let's just say if we didn't have them, we can assign and we can create a ticker
4:27
and then pass that ticker instead of a single. Let's say you have a lot of tickers that you want to download
4:35
Then in that case, downloading that individually would be a difficult task to perform
4:42
Therefore, we can pass a vector. into it and that would download all these
4:48
So let me define this vector first. And that would download all those and those stickers one by one and we would have an object
4:57
for each of them. Right. So I hope this was useful. Do subscribe to this channel
5:02
Do hit the bell icon. And thanks for watching this video
#Finance
#Investing