Description: This meetup is for anyone interested in learning and sharing knowledge about scraping data from Yahoo Finance using R. Yahoo Finance provides a wealth of financial data that can be used for research, analysis, and investment purposes. In this meetup, we will discuss the basics of web scraping, explore the structure of Yahoo Finance pages, and walk through the process of scraping data from Yahoo Finance and analyse the data using R and its libraries such as ggplot2, quantmod, and forecast.
Anyone who is interested in learning about web scraping and its application to financial data, from beginners to experienced data analysts and investors. This meetup is open to all skill levels.
Requirements: Participants should bring their laptops to the online event. Basic knowledge of R programming is recommended, but not required. Internet access will be required to access Yahoo Finance pages during the live coding session.
Quantmod is an R package that provides a suite of tools for quantitative financial modeling and analysis. It enables users to access and manipulate financial data from various sources, including Yahoo Finance. In this tutorial, we will walk through the steps of using quantmod to retrieve and analyze Yahoo Finance data.
To start using quantmod and other free libraries, we need to load the package into R by running the following command:
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## ######################### Warning from 'xts' package ##########################
## # #
## # The dplyr lag() function breaks how base R's lag() function is supposed to #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or #
## # source() into this session won't work correctly. #
## # #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop #
## # dplyr from breaking base R's lag() function. #
## # #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning. #
## # #
## ###############################################################################
##
## Attaching package: 'xts'
## The following objects are masked from 'package:dplyr':
##
## first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
The first step in using quantmod to retrieve Yahoo Finance data is to specify the ticker symbol for the stock you want to analyze. For example, if you want to retrieve data for Tyson Foods and the Froster Farms, the ticker symbol are TSN and FSTR, seprarately.
Once you have the ticker symbol, you can use the getSymbols() function to retrieve the data. This function downloads data from various sources, including Yahoo Finance, and returns it as an object that can be manipulated in R.
To retrieve data for TSN and FSTR, run the following command:
getSymbols('TSN', src = 'yahoo',
from = "2010-01-01", to = Sys.Date())
## [1] "TSN"
Stock1 <- data.frame(
TSN,
date = as.Date(rownames(data.frame(TSN)))
)
getSymbols('FSTR', src = 'yahoo',
from = "2010-01-01", to = Sys.Date())
## [1] "FSTR"
Stock2 <- data.frame(
FSTR,
date = as.Date(rownames(data.frame(FSTR)))
)
head(Stock2)
## FSTR.Open FSTR.High FSTR.Low FSTR.Close FSTR.Volume FSTR.Adjusted
## 2010-01-04 30.10 30.75 30.10 30.51 27000 29.64728
## 2010-01-05 30.45 30.45 29.51 29.55 47100 28.71442
## 2010-01-06 29.60 29.88 29.27 29.41 29800 28.57839
## 2010-01-07 29.40 29.75 28.90 29.63 17200 28.79216
## 2010-01-08 29.53 29.89 29.38 29.86 14500 29.01566
## 2010-01-11 29.93 29.94 29.41 29.47 20500 28.63668
## date
## 2010-01-04 2010-01-04
## 2010-01-05 2010-01-05
## 2010-01-06 2010-01-06
## 2010-01-07 2010-01-07
## 2010-01-08 2010-01-08
## 2010-01-11 2010-01-11
This will download the daily historical data for these two stocks.
Note that we specified the start date using the from argument and the end date using the to argument. We set the end date to Sys.Date(), which retrieves data up to the current date.
Once you have retrieved the data, you can use various functions to explore and manipulate it. Here are a few examples:
To get a summary of the data, run the summary() function. ### Summary Statistics To get the first six rows of the data, run the head() function.
Now let’s dive deeper!
## Stock1 Stock2
## [1,] 9.051446 29.64728
## [2,] 9.221535 28.71442
## [3,] 9.687414 28.57839
## [4,] 9.857497 28.79216
## [5,] 9.835313 29.01566
## [6,] 9.702206 28.63668
## [1] 0.3053044 0.3211465 0.3389769 0.3423674 0.3389657 0.3388034
#Now let’s run an event analysis and a time series analysis (under development)
## [1] "TSN"
## [1] "SPY"
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 2010-01-04 112.37 113.39 111.51 113.33 118944600 85.02795
## 2010-01-05 113.26 113.68 112.85 113.63 111579900 85.25303
## 2010-01-06 113.52 113.99 113.43 113.71 116074400 85.31306
## 2010-01-07 113.50 114.33 113.18 114.19 131091100 85.67320
## 2010-01-08 113.89 114.62 113.66 114.57 126402800 85.95827
## 2010-01-11 115.08 115.13 114.24 114.73 106375700 86.07831
## date
## 2010-01-04 2010-01-04
## 2010-01-05 2010-01-05
## 2010-01-06 2010-01-06
## 2010-01-07 2010-01-07
## 2010-01-08 2010-01-08
## 2010-01-11 2010-01-11
## TSN.Open TSN.High TSN.Low TSN.Close TSN.Volume TSN.Adjusted date
## 1 12.27 12.30 12.15 12.24 3355000 9.051447 2010-01-04
## 2 12.21 12.49 12.21 12.47 3781300 9.221535 2010-01-05
## 3 12.79 13.12 12.58 13.10 6810500 9.687415 2010-01-06
## 4 13.10 13.42 13.01 13.33 5979200 9.857501 2010-01-07
## 5 13.29 13.37 13.12 13.30 3999300 9.835313 2010-01-08
## 6 13.29 13.40 13.04 13.12 2875700 9.702203 2010-01-11
## SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 1 112.37 113.39 111.51 113.33 118944600 85.02795
## 2 113.26 113.68 112.85 113.63 111579900 85.25303
## 3 113.52 113.99 113.43 113.71 116074400 85.31306
## 4 113.50 114.33 113.18 114.19 131091100 85.67320
## 5 113.89 114.62 113.66 114.57 126402800 85.95827
## 6 115.08 115.13 114.24 114.73 106375700 86.07831
##
## Call:
## lm(formula = TSN.Adjusted ~ SPY.Adjusted, data = time_series)
##
## Residuals:
## Min 1Q Median 3Q Max
## -30.307 -13.667 -2.187 12.114 34.394
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 20.05152 0.45064 44.50 <2e-16 ***
## SPY.Adjusted 0.08832 0.00144 61.35 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 14.44 on 4037 degrees of freedom
## Multiple R-squared: 0.4825, Adjusted R-squared: 0.4824
## F-statistic: 3764 on 1 and 4037 DF, p-value: < 2.2e-16
References: Intro to the quantmod package. https://www.quantmod.com/ Using R for Time Series Analysis https://a-little-book-of-r-for-time-series.readthedocs.io/en/latest/src/timeseries.html