Financial Market Data and Analysis in R

Whether you’re a seasoned investor or enterprising day trader, you need to be using the quantmod package in R for all your quantitative financial research and statistical analysis! This tutorial will provide an overview of the package and highlight the most important functions. I’ll be using GameStop (stock symbol: GME) as an example since it’s been all over the news recently.

Load the quantmod package


Use getQuote for immediate trading data


This function call retrieves trading data for the GameStop stock on a second-by-second timescale:

Trade Time          Last    Change    % Change  Open High  Low     Volume
2021-02-04 13:33:06 60.8006 -31.60941 -34.20561 91.19 91.5 57.2101 44493559

While this is the default data that is returned, the getGuote function has additional arguments that can fetch even more trading information, such as Bid and Ask price, Market Capitalization, and P/E Ratio:

getQuote("GME",what=yahooQF(c("Bid","Ask", "Market Capitalization", "P/E Ratio")))

The additional arguments that the getQuote function can take isn’t particularly well documented. The best list of arguments I’ve found is direct from the source code on GitHub, here.

Lastly, you can obtain trading data for multiple stocks simultaneously. Here I return data for GameStop, Exxon Mobil, and Apple in one function call:


The getSymbols function will be your best friend

from_date = '2021-01-21'
getSymbols("GME", from = from_date)

This is the most basic syntax for calling the getSymbols function. It will fetch daily price (open, high, low, close, and adjusted) and volume data for the symbol you pass in. Data is retrieved from Yahoo Finance by default, but you can specify Google Finance or several other data source options with the “src” function argument. The function will work for anything that has a symbol – stocks, mutual funds, market indexes and indicators, ETFs, etc.

You may notice that I am specifying a date using the “from” argument. The getSymbols function defaults to retrieving data starting on January 1st, 2007. It is highly unlikely that this is an important date for your financial analysis, so change it to something relevant. Here I am using a “from” date of January 21st, 2021 – right before the extreme GameStop stock price movement.

This getSymbols function call will also automatically create a variable in your current R session environment named “GME” (you can disable this by setting the function argument auto.assign to false). You now have all GME trading data, and can take a look at the first three rows of data like this:

> GME[1:3,]
           GME.Open GME.High GME.Low GME.Close GME.Volume GME.Adjusted
2007-01-03   27.555    27.57  26.670    27.450    3981200     18.48209
2007-01-04   27.615    28.05  27.410    27.705    4851600     18.65378
2007-01-05   27.705    28.03  27.655    27.745    2687800     18.68072

Plotting trading data

I tend to create plots with two rows and one column, which are very useful for looking at price and volume data simultaneously. Here is an example using daily stock price highs and trading volume:

Plots of daily stock price highs and trading volume for GameStop stock
Plot of daily stock price highs and trading volume for GameStop (GME).

Converting to weekly or monthly data

While the getSymbols function returns daily trading data, it is easy to convert to weekly or monthly (or other timescales) using the to.weekly and to.monthly functions. Here is an example of retrieving and displaying monthly data for the GameStop stock:

from_date = '2018-01-01'
getSymbols("GME", from = from_date)
GME_monthly = to.monthly(GME)
Plots of monthly stock price highs and trading volume for GameStop stock
Plot of monthly stock price highs and trading volume for GameStop (GME).

Was this tutorial of the quantmod package in R helpful and useful to your financial research? What other packages do you use in R for analyzing trading data? Let me know in the comments below!

Check out more at

Post content and images © 2021 – All rights reserved.

Leave a Reply