Download Stock Data in R
7K views
May 16, 2024
In this video we discuss how to download stock data in R. We use quantmod package and we use the getSymbols function. 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 this video we are going to discuss how do we download stock prices data in R
0:07
So when we work with stock prices data we would have to download them
0:10
One way of downloading them is to download them from Yahoo Finance
0:15
So there is an easy way of doing that and that is called QuantMode package
0:20
So if you haven't installed the QuantMode package you can install it using this line
0:24
of code. Now because I have already installed it so I am not going to execute this code but I
0:29
am going to load the library QuantMode library and this library is loaded
0:34
So the function that we use is called GetSymbols and what it does is it goes to the Yahoo Finance
0:40
download the data from there and fetch it in R environment in R
0:47
So the first thing that I need to specify is the symbol that I want to download
0:51
So you would have to specify the ticker that you want to do. You can search for tickers from Yahoo Finance, get the stickers and you would have to specify
1:02
the ticker over here. Then the second parameter is from which date you want to download the data and tell what date
1:10
So this becomes the range of the data that you want to download. If I press control enter this is going to take a few moments depending on the internet
1:19
speed that I have and the size of the data. We get the data it is an XTS object if you are familiar with XTS object if not you can
1:29
look into our videos on XTS object. It is an extendable time series we have an index that is the date and we have all the
1:36
variables in columns. So it is in wide format and we have the open data we have the high price low price the
1:44
closing price adjusted price and the volume. Now we can plot this I am just going to plot this to give you an idea
1:53
I have plotted the closing price from this XTS object. Now in this case I have specified the start and ending date within this getSymbol function
2:04
but I can define the start and end date separately and then pass that start date and end date
2:12
as an object in my function and I can also specify the source from where I want to download
2:18
so usually I would work with Yahoo finance so I would specify Yahoo if you do not specify
2:23
Yahoo then it is by default going to download that from Yahoo finance
2:29
Now I can get other symbols also for example I can download Microsoft and in this case
2:34
instead of giving this specific date I want to download last year's data
2:39
So what I have done is take the today's date and minus 365 that is one year before today's
2:47
date start one year before one year from today one year in the past and till today's date
2:59
Again I have specified this new parameter which is called auto assign which I have set
3:04
to false by default it is true and what it does is that if you set it to false it is
3:11
not going to download the data in our environment it is just going to download it and show it
3:17
to us in the console. By default it is true so that's why it always get us this object over here
3:25
So if I press ctrl enter now remember this today's function is coming from the tidyverse
3:33
library so I haven't loaded tidyverse library so I would just load the tidyverse library
3:41
and then I execute this command so it would be executed and it would show the data in
3:48
the console rather than giving me an object in my environment. I can also download the indexes this is the S&P 500 index again you can get this ticker
4:01
from Yahoo finance what ticker stands what are the ticker for S&P 500 and etc
4:08
Lastly how do we download multiple symbols so I have defined a ticker object over here
4:13
that would contain these three stocks remember I just have AAPL from this one I want to download
4:21
all these I would just pass this ticker into my get symbol function and that would download
4:27
all the data from I mean from this start date till this end date for all the three symbols
4:36
we get AAPL which we already had we didn't had Microsoft so that is also downloaded and
4:42
the SPY ticker that is now also downloaded. So I hope this video was useful do subscribe to this channel stay tuned to this channel
4:50
and hit the bell icon thanks for watching this video
#Computers & Electronics