Stock Analysis using R

[This article was first published on R-Chart, 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.

Want to do some quick, in depth technical analysis of Apple stock price using R? Theres a package for that!

The Quantmod package allows you to develop, testing, and deploy of statistically based trading models.  It provides the infrastructure for downloading/importing data from a variety of locations, analyze that data and produce charts that help determine statistical trends.  I appreciated Digital Dude calling this package to my attention  in a recent comment.  I also noticed that Revolution Analytics had highlighted the package on its finance page.  Actually, I had come across quantmod a few months ago – and it instantly got me excited about the power of R.  To give you an idea of typical usage, the following creates a stock chart of the last three months of Apple stock data.


chartSeries(AAPL, subset=’last 3 months’)

The getSymbols function is used to retrieve stock data.  Data can originate in a number of locations.  In the example above, we are obtaining a single stock, Apple.  If you wanted to download several different stock quotes, you can do so in a single command.


Once you have retrieved stock data, you can focus on subsets of dates quickly.

You can also merge data to view comparisons.


The chartSeries command creates the plot pictured above.  It captures a large amount of information, the date, open and close price, and volume of trading for each day.  Finally, the addBBands() call adds Bollinger Bands to the chart.  Informally, this amounts to a line indicating moving average and two lines a standard deviation above and below this moving average. For the uninitiated, technical indicators (and overlays) can be broken up into four categories – Trend, Volatility, Momentum, and Volume.  Those available in Quantmod are listed below.

Indicator TTR Name quantmod Name
Welles Wilder’s Directional Movement Indicator ADX addADX
Double Exponential Moving Average DEMA addDEMA
Exponential Moving Average EMA addEMA
Simple Moving Average SMA addSMA
Parabolic Stop and Reverse SAR addSAR
Exponential Volume Weighted Moving Average EVWMA addEVWMA
Moving Average Convergence Divergence MACD addMACD
Triple Smoothed Exponential Oscillator TRIX addTRIX
Weighted Moving Average WMA addWMA

Indicator TTR Name quantmod Name
Average True Range ATR addATR
Bollinger Bands BBands addBBands
Price Envelope N/A addEnvelope

Indicator TTR Name quantmod Name
Commodity Channel Index CCI addCCI
Chande Momentum Oscillator CMO addCMO
Detrended Price Oscillator DPO addDPO
momentum addMomentum
Rate of Change ROC addROC
Relative Strength Indicator RSI addRSI
Stocastic Momentum Index SMI addSMI
Williams %R WPR addWPR

Indicator TTR Name quantmod Name
Chaiken Money Flow CMF addCMF
Volume N/A addVo

This really just scratches the surface of what is possible with quantmod.  For instance, see this post on using quantmod with gold related data.

Later posts will include other applications – there is simply too much to cover at one time.

To leave a comment for the author, please follow the link and comment on their blog: R-Chart. 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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)