How To Analyze Yahoo Finance Data With R

Analyzing Yahoo finance stock data with R is very straight forward.

We need following two packages.

  1. quantmod
  2. TTL

You can install above two packages in your R repl as shown below.

In [1]:
In [11]:

Ok now we can get the data from yahoo using getSymbols. Note auto.assign=False means, we want getSymbols to return back the data.

In [12]:
df_intc <- getSymbols('INTC',src='yahoo',auto.assign=FALSE)

Let us check the class of df_intc.

In [4]:
  1. 'xts'
  2. 'zoo'

xts is a extensible time series package for time series data. xts is extension of the zoo class. xts data format is a special R matrix. To learn more about xts and zoo. check out following link...

Let us the check the number of rows in our data.

In [5]:

Now let us look at the last two rows in our data set.

In [6]:
           INTC.Open INTC.High INTC.Low INTC.Close INTC.Volume INTC.Adjusted
2020-03-09     51.92     52.71    50.00      50.85    56197000         50.85
2020-03-10     52.76     54.00    50.43      53.98    36569000         53.98

To plot xts data, we can use matplotlib's plot method.

How to plot data from Yahoo finace with R

In [7]:
plot(df_intc$INTC.Close,main = 'Intel Stock Price')

quantmod has chart_series method which we can use also to plot in R.

In [8]:
chart_Series(df_intc$INTC.Close,name="Intel Stock Price")

How to draw candlestick Chart with R

To draw candlestick chart we will have to feed in all the columns to chartSeries package. Note chartSeries is different from chart_series.

In [9]:
chartSeries(df_intc,name="Intel Stock Price",theme = 'white')

We can also plot part of time series using subset option of chartSeries package in R. Let us say we want to plot only data from 2018 to 2020, we can specify it using subset='2018::2020'

In [10]:
chartSeries(df_intc,name="Intel Stock Price",theme = 'white',subset='2018::2020')