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

[This article was first published on R programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

“Thequantmodpackage forRis 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.

getSymbols(‘AAPL’)

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

head(AAPL) # You should see the following result.
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted2007-01-03 86.29 86.58 81.90 83.80 309579900 11.194492007-01-04 84.05 85.95 83.82 85.66 211815100 11.442952007-01-05 85.77 86.20 84.40 85.05 208685400 11.361472007-01-08 85.96 86.53 85.28 85.47 199276700 11.417572007-01-09 86.45 92.98 85.15 92.57 837324600 12.366032007-01-10 94.75 97.80 93.45 97.00 738220000 12.95782

Visualize the charts

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

chartSeries(AAPL, TA=NULL) # this should produce the following chart.

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

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)

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

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.

Apple_closeprice = Cl(AAPL) # We assign the closing price to a new variable called Apple_closeprice.plot(Apple_closeprice) # Plotting the close price

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

Plotting histogram is simple.

hist(AAPL[,4]) #This command plots the histogram of closing price of apple stock.

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

hist(NSEI[,4], main = "Apple Close") #The histogram of closing price of apple stock with the heading “Apple Close”

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

hist(NSEI[,4], main = "Apple Close", breaks =25) # Introducing more price ranges.

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

Technical indicators

chartSeries(AAPL)addMACD() # adds moving average convergence divergence signals to the apple stock price

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

addBBands()# Adds Bollinger bands to the apple stock price.

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

addCCI() # Add Commodity channel index.

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

addADX() #Add Directional Movement Indicator

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

addCMF() #Add Chaiken Money Flow

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

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

addCMO # Add Chaiken Money FlowaddDEMA # Add Double Exponential Moving AverageaddDPO # Add Detrended Price OscillatoraddEMA # Add Exponential Moving AverageaddEnvelope # Add Price EnvelopeaddEVWMA # Add Exponential Volume Weigthed Moving AverageaddMACD # Add Moving Average Convergence DivergenceaddMomentum # Add MomentumaddROC # Add Rate of ChangeaddRSI # Add Relative Strength IndicatoraddSAR # Add Parabolic Stop and ReverseaddSMA # Add Simple Moving AverageaddSMI # Add Stocastic Momentum IndexaddTRIX # Add Triple Smoothed Exponential OscillatoraddVo # Add VolumeaddWMA # Add Weighted Moving Average

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

AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted2007-01-03 86.29 86.58 81.90 83.80 309579900 11.194492007-01-04 84.05 85.95 83.82 85.66 211815100 11.442952007-01-05 85.77 86.20 84.40 85.05 208685400 11.361472007-01-08 85.96 86.53 85.28 85.47 199276700 11.417572007-01-09 86.45 92.98 85.15 92.57 837324600 12.366032007-01-10 94.75 97.80 93.45 97.00 738220000 12.95782

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.

class(AAPL)

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

is.OHLC(AAPL) # Checks whether the xts data object has the open,high, low and close price?

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

has.Vo(AAPL) # Checks whether the data object has volumeseriesHi(AAPL) # To check the highest point of price.Lag(Cl(AAPL)) #One period lag of the closing priceNext(OpCl(AAPL)) #The next periods open to close - today!AAPL ['2007'] #Fetches all Apple’s 2007 OHLCAAPL ['2008::'] # Apple data, from 2008 onwarddailyReturn(AAPL) # Returns by dayweeklyReturn(AAPL) # Returns by weekmonthlyReturn(AAPL) # month, indexed by yearmondaily,weekly,monthly,quarterly, and yearlyallReturns(AAPL) # note the plural

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

The post A Guide on R quantmod Package: How to Get Started? appeared first on .

Related

To leave a comment for the author, please follow the link and comment on their blog: R programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A Guide on R quantmod Package: How to Get Started? (2024)
Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated:

Views: 5677

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.