Different Ways to Obtain and Manipulate Stock Data In R Using quantmod

R offers many perks to people analyzing different areas of Finance. One of the biggest perks is a Quantitative Finance library called quantmod. In order to install quantmod, navigate to the console and enter install.packages(“quantmod”). If you have any questions on the documentation, simply type in the console: ?quantmod┬áin order to view the modules documentation straight in R Studio. This is beautiful, isn’t it? No browser use and internet perusing necessary!.

Let’s start by only working with a single asset. If you would rather follow a video tutorial for this segment, it is located below.

To start, we need to import the library in the R Script we created. From there, we can use the getSymbols function in order to obtain data for a single asset. We will also set a variable which will contain our start date for the time series. The only arguments we are inputting to the getSymbols function include the ticker symbol for Amazon, start date, and auto.assign argument. When auto.assign is set to true (T), a new object will be created in the environment with the symbol names. If auto.assign is set to FALSE the data we are essentially assigning the results ourselves to the variable we created, which will be outputted in the environment. There are many arguments for the getSymbols function, check the documentation for more information This is all we will need at the bare minimum.

Keep in mind the Yahoo API is pretty much defunct. The quantmod library has managed to patch it but it seems like the data has lost a lot of what people used it for in the first place. Check the documentation for more information.

In this case, we will use google finance and only import the close column, which is the fourth column in the dataset.

Check out global environment window at the top right corner of your screen. Notice the object you created prior. Notice next to the object you see: “An xts object….”; this means we have created an extensible time series object. These are extraordinarily useful when working with time series data such as stock prices. The quantmod library will load the stock data into the specified environment as an xts object automatically.

If we click on the spreadsheet icon in the global environment window, we can see our time series data.

We can now calculate returns over different time frames with multiple functions within the quantmod library. Let’s examine each. We can use the periodReturn function to calculate returns over different frequencies. These can include daily, weekly or monthly returns. We can also change the return type. We can choose between arithmetic (discrete) and log (continuous) return types. The rule of thumb in finance is to use log returns as they are time sensitive. We can also examine returns for a specific year or period range also by specifying including the subset argument. The returns will also be outputted as an xts object in the specified environment.

Now that we have the data loaded, let’s graph it! The graphing utilities of R are an awesome perk and superior to most other languages in my opinion. We can use the chartSeries function to accomplish this. The output is displayed below for the close price of Amazon, as well as a price-volume chart had we imported all of the data initially. The second chart is a candlestick chart by default as we deal with open, high, low, close and volume data.

We can work with return data also and display it graphically. Let’s create a histogram of the log returns we calculated. Creating histograms is ridiculously simply in R. The breaks argument is our bin size in this case.


We can also chart technical indicators quite easily with quantmod. This is done via the TA argument in the chartSeries function. We can overlay multiple indicators or use only one if desired. We can change the theme being used here also to allow us to better see the indicators. We will output a candlestick chart (OHLC) with the Stochastic Momentum Index overlayed. In the second chart, will overlay multiple indicators including volume, Bollinger Bands, and the Relative Strength Index.


Now, let’s say we want to work with multiple assets (a portfolio). We can apply the same concepts we used prior in an iterative format. Just a heads up, we will need to use the dplyer library here in order to perform special functions on our data frame of combined stock data. Again, if you want to follow along with a video tutorial, it is posted below.

To start, we need to create a vector of tickers. A vector is essentially a list. The -c denotes the combination of the tickers in the vector. Next, we will set a variable which will hold the results of the combination of all of the time series data. We will set its value to null initially and populate it via a for loop. Next, we will apply a function to the data frame to remove any dates with no data corresponding to it. This will be applied throughout the entire data frame apply(). We can do this with either the prices object we created or the returns object we will create in the future. We also want to clean up the column names a bit from Amazon.return to just the ticker value corresponding to the series. Let’s check our output graphically with plot() in R. For the sake of simplicity, plot() will be used here albeit the attractiveness of the graphs with no parameters is minimal.

We can normalize this price data quite easily. Let’s also set different colors for the different assets also.

Finally, we can calculate periodic returns for our entire data set. We can do this in a few different ways. Let’s start out with the simple approach. We can use the ROC function in the quantmod library which will calculate either the log or arithmetic returns of the portfolioPrices xts object. Also, another alternative to calculating returns other than the presented methods would be using the PerformanceAnalytics library. The next option we can use is the periodReturn function within the quantmod library. An extension of this can be used either with the dailyReturn(), weeklyReturn(), or monthlyReturn() functions also. However, there is a problem using periodReturn function and extensions of it with our prices object. Check out the output below comparatively for each method.



periodReturn and dailyReturn:

Notice that the last method returned only one column in the xts object. That is on an aggregate basis in terms of returns for the portfolio. We would rather have multiple columns of returns for different frequencies when using the periodReturn function or similar. We can do that by executing the following code:

What we are doing here is chaining two functions together with the dplyer library. We are using a forward pipe operator to do this (%>%). We need to chain the getSymbols function and period return function in order to get the desired output. After this is done, we have to merge all columns together in order to complete our returns object. Again, we can remove dates that only have closing prices for all tickers amongst all dates in this step if desired. We will clean up the column names once again, and finally, write the xts object to a CSV file. This is a nifty capability built into R where we could analyze a dataset in another application such as Excel.

If by all means, you wanted to avoid using chain operators and the dplyer library, the code to work around that would look something like this.

Let’s examine the output of the monthly returns for each asset:

Mission accomplished. R makes it too easy!

About the author


Hi, I'm Frank. I have a passion for coding and extend it primarily within the realm of Finance.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *