R Markdown

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.

Agenda:

Introduction to web scraping and its applications

Overview of R libraries such as ggplot2, quantmod, and forecast

Live coding session on scraping data from Yahoo Finance using R and its libraries

Tips and tricks for efficient web scraping and handling common issues

Perform very basic time series analysis

Discussion and Q&A session

Who should attend?

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.

Intro to Quantmod

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 ###################################
## # We noticed you have dplyr installed. 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 enter or source() into this session won't     #
## # work correctly.                                                             #
## #                                                                             #
## # All package code is unaffected because it is protected by the R namespace   #
## # mechanism.                                                                  #
## #                                                                             #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## # You can 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.                                #
## ################################### 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

Retrieving Yahoo Finance Data

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 NTDOY and ATVI, 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 NTDOY and ATVI, run the following command:

getSymbols('NTDOY', src = 'yahoo', 
           from = "2010-01-01", to = Sys.Date())
## [1] "NTDOY"
Stock1 <- data.frame(
  NTDOY,
  date = as.Date(rownames(data.frame(NTDOY)))
)

getSymbols('ATVI', src = 'yahoo', 
           from = "2010-01-01", to = Sys.Date())
## [1] "ATVI"
Stock2 <- data.frame(
  ATVI,
  date = as.Date(rownames(data.frame(ATVI)))
)
head(Stock2)
##            ATVI.Open ATVI.High ATVI.Low ATVI.Close ATVI.Volume ATVI.Adjusted
## 2010-01-04     11.23     11.45    11.15      11.30     9306600     10.018278
## 2010-01-05     11.24     11.36    11.21      11.32    11366100     10.036010
## 2010-01-06     11.26     11.38    11.21      11.26     6537800      9.982816
## 2010-01-07     11.10     11.19    10.93      10.99    15390800      9.743442
## 2010-01-08     10.97     11.05    10.88      10.90    11002500      9.663647
## 2010-01-11     10.91     10.98    10.83      10.89     8778400      9.654784
##                  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.

Exploring the Data

Once you have retrieved the data, you can use various functions to explore and manipulate it. Here are a few examples:

Summary Statistics

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,]  6.180 10.018278
## [2,]  6.294 10.036010
## [3,]  6.630  9.982816
## [4,]  6.844  9.743442
## [5,]  6.964  9.663647
## [6,]  6.950  9.654784
## [1] 0.6168725 0.6271417 0.6641413 0.7024212 0.7206389 0.7198504

#Now let’s run an event analysis and a time series analysis (under development)

## [1] "NTDOY"
## [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     88.11789
## 2010-01-05   113.26   113.68  112.85    113.63  111579900     88.35113
## 2010-01-06   113.52   113.99  113.43    113.71  116074400     88.41336
## 2010-01-07   113.50   114.33  113.18    114.19  131091100     88.78657
## 2010-01-08   113.89   114.62  113.66    114.57  126402800     89.08205
## 2010-01-11   115.08   115.13  114.24    114.73  106375700     89.20642
##                  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
##   NTDOY.Open NTDOY.High NTDOY.Low NTDOY.Close NTDOY.Volume NTDOY.Adjusted
## 1       6.05       6.19     6.050       6.180       527500          6.180
## 2       6.23       6.50     6.210       6.294      1053000          6.294
## 3       6.58       6.68     6.362       6.630      1839000          6.630
## 4       6.78       6.87     6.780       6.844      2083500          6.844
## 5       6.91       6.97     6.880       6.964      2209500          6.964
## 6       6.95       6.98     6.930       6.950      1171500          6.950
##         date SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
## 1 2010-01-04   112.37   113.39  111.51    113.33  118944600     88.11789
## 2 2010-01-05   113.26   113.68  112.85    113.63  111579900     88.35113
## 3 2010-01-06   113.52   113.99  113.43    113.71  116074400     88.41336
## 4 2010-01-07   113.50   114.33  113.18    114.19  131091100     88.78657
## 5 2010-01-08   113.89   114.62  113.66    114.57  126402800     89.08205
## 6 2010-01-11   115.08   115.13  114.24    114.73  106375700     89.20642
## 
## Call:
## lm(formula = NTDOY.Adjusted ~ SPY.Adjusted, data = time_series)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.4773 -1.7205 -0.5024  1.6074  5.4981 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.8728812  0.0851287   10.25   <2e-16 ***
## SPY.Adjusted 0.0280527  0.0003462   81.03   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.121 on 3328 degrees of freedom
## Multiple R-squared:  0.6636, Adjusted R-squared:  0.6635 
## F-statistic:  6566 on 1 and 3328 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