Stock Analysis using R

June 26, 2010

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.


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)