A Guide on R quantmod Package: How to Get Started?

How to Get Started with R quantmod Package?

“The quantmod package for R is designed to assist the quantitative trader in the development, testing, and deployment of statistically based trading models.”

It is a rapid prototyping environment where enthusiasts can explore various technical indicators with minimum effort. It offers charting facilities that are not available elsewhere in R. Quantmod package makes modeling easier and analysis simple. This article is intended to present some functions of quantmod using sample market data.  The features of quantmod are presented in three sections, downloading data, charting, technical indicators and other functions.

Without much ado, we will see the usage of quantmod package.

Downloading data

Once the quantmod package is installed and the library is loaded, run the following command to get the data of Apple Inc. stock into the R console.

To see the starting point of the data, type the following command.

Visualize the charts

The beauty of quantmod lies in its ability to visualize the charts. Type the following command.

chartSeries(AAPL, TA=NULL)

As one can see, the x-axis shows the time period, while the y-axis shows price range for the AAPL stock. In the command above we set TA=”Null”. It means do not include any Technical Analysis parameter. The following command produces the same graph along with volume parameter.


The noting difference between this graph and the previous one is the representation of AAPL’s volume. Rest of the syntax is meant to decorate the chart appearance.

We shall choose the closing price for reference and calculate various technical indicators based on it. Following command selects the closing price of Apple Inc.


Plotting histogram is simple.


hist(NSEI[,4], main = "Apple Close")

hist(NSEI[,4], main = "Apple Close", breaks =25)

Technical indicators


Similarly, other technical indicators can be calculated. Following is the list of technical indicators that quantmod supports.

We will have a look at the data handling features of quantmod. We saw earlier that the apple data downloaded has the following structure.

Useful functions

Quantmod provides functions to explore features of the data frame. The following command shows that the object type holding apple data is xts and zoo.

One would want to explore whether the data extracted contains the open price, volume etc. Have a look at the following commands.

The output is TRUE implying that the data object contains open, high, low and close.

Next Step

If you’re new to this and not able to grab all the technical aspects of this article, you may like to take a look at a few articles explaining basic concepts like designing a quant trading strategy in R. You can also take a look at a basic example of trading strategy coded in R.

Once you’ve successfully learned these basics you can test your skills at our interactive self-paced 10 hours long datacamp course ‘Model a Quantitative Trading Strategy in R

Leave a Reply

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