Stock Analysis using R

June 26, 2010
By

(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.

library('quantmod')


getSymbols("AAPL")
chartSeries(AAPL, subset='last 3 months')
addBBands()

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.

getSymbols(c("ORCL","IBM"))


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

AAPL['2010-06-01::2010-06-26']


You can also merge data to view comparisons.


head(as.xts(merge(ORCL,IBM)))


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.

Trend
IndicatorTTR Namequantmod Name
Welles Wilder's Directional Movement IndicatorADXaddADX
Double Exponential Moving Average DEMAaddDEMA
Exponential Moving Average EMAaddEMA
Simple Moving AverageSMAaddSMA
Parabolic Stop and ReverseSARaddSAR
Exponential Volume Weighted Moving AverageEVWMAaddEVWMA
Moving Average Convergence DivergenceMACDaddMACD
Triple Smoothed Exponential OscillatorTRIXaddTRIX
Weighted Moving AverageWMAaddWMA
ZLEMAZLEMAaddZLEMA


Volatility
IndicatorTTR Namequantmod Name
Average True RangeATRaddATR
Bollinger BandsBBandsaddBBands
Price Envelope N/AaddEnvelope

Momentum
IndicatorTTR Namequantmod Name
Commodity Channel IndexCCIaddCCI
Chande Momentum OscillatorCMOaddCMO
Detrended Price OscillatorDPOaddDPO
momentumaddMomentum
Rate of ChangeROCaddROC
Relative Strength IndicatorRSIaddRSI
Stocastic Momentum IndexSMIaddSMI
Williams %RWPRaddWPR

Volume
IndicatorTTR Namequantmod Name
Chaiken Money FlowCMFaddCMF
VolumeN/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 his blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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: ,

One Response to Stock Analysis using R

  1. searchforarticl (Search For Articles) on June 26, 2010 at 9:02 pm

    Twitter Comment


    Stock Analysis using R | (Articles about R): An article about R: Want to do some quick, in depth technical analysi… [link to post]

    Posted using Chat Catcher