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

Share on Facebook0Tweet about this on Twitter0Share on LinkedIn12Share on Google+0

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 is 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 library is loaded, run the following command to get the data of apple stock into thr 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, y axis shows price range for apple 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.

barChart(AAPL)

The noting difference between this graph and the previous one is the representation of volume of the apple stock traded. Rest of the syntax is meant to decorate the chart appearance.

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

plot(Apple_closeprice)

Plotting histogram is simple.

hist(AAPL[,4])

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

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

Technical indicators

addMACD()
addBBands()
addCCI()
addADX()
addCMF()

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 has open price, volume etc. Have a look at the following commands.

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

Share on Facebook0Tweet about this on Twitter0Share on LinkedIn12Share on Google+0

Leave a Reply

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