Site icon R-bloggers

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.

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 their blog: R-Chart.

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.