# QuantMod Basics – Stock Data Download and Manipulation

In this quick tutorial I will show you how to use the quantmod library to download historical data, plot it, add a technical indicator (Bollinger Bands) and do some basic manipulation with date ranges and intersecting data sets.

The tutorial will let you make images such as:

This is a very quick tutorial designed to get you started / make you aware of certain packages.

Onto the code:

``` ?View Code RSPLUSinstall.packages("quantmod") #Install the quantmod library library("quantmod") #Load the quantmod Library stockData <- new.env() #Make a new environment for quantmod to store data in   startDate = as.Date("2008-01-13") #Specify period of time we are interested in endDate = as.Date("2012-01-12")   tickers <- c("ARM","CSR") #Define the tickers we are interested in   #Download the stock history (for all tickers) getSymbols(tickers, env = stockData, src = "yahoo", from = startDate, to = endDate)   #Use head to show first six rows of matrix head(stockData\$ARM)   #Lets look at the just the closing prices Cl(stockData\$ARM)   #Lets plot the data chartSeries(stockData\$ARM)   #Lets add some bollinger bands to the plot (with period 50 & width 2 standard deviations) ?addBBands #Make R display the help documentation so we know what variables to pass to the function addBBands(n=50, sd=2)   #Lets get the technical indicator values saved into a variable #Note must give it a single time series (I gave it the close price in this example) indicatorValuesBBands <- BBands(Cl(stockData\$ARM),n=50, sd=2)   #Lets examine only a 1 month period of data armSubset<- window(stockData\$ARM, start = as.Date("2010-02-15"), end = as.Date("2010-03-15")) armSubset #Lets see the data   #Lets extract a 1 month period of data for CSR but starting midway through the arm data csrSubset<- window(stockData\$CSR, start = as.Date("2010-02-25"), end = as.Date("2010-03-25")) csrSubset #Lets see the data   #Now we want to get the intersection of the two subsets of data #this will gives us all the sets of data where the dates match #Its important to match the date series to stop spurious analysis of non-synchronised data #All=FALSE specifies the intersection as in don't include all dates in the merge armcsrIntersection <- merge(armSubset, csrSubset, all = FALSE) subset(armcsrIntersection,select = c("ARM.Open","CSR.Open")) #Select the open columns and display ```

Further material can be found at:

## 7 thoughts on “QuantMod Basics – Stock Data Download and Manipulation”

1. Symbol locale –
The example does not work for me as I get a 404 error at the symbol download, and I see these symbols are LSE/UK, not US, based.
The code works if I add “.L” (ie ARM.L) to the two symbol names (all instances).
Do you have some workspace fix that does this for you, or maybe selects a UK Yahoo site as the source?
I’ve also noticed the merge() function, maybe that could be used to sort out the “S&P VWAP Trend Follow” problem?

2. When I try to execute Cl(stockData\$ARM)
I get an error:

Fehler in Cl(stockData\$ARM) :
subscript out of bounds: no column name containing “Close”

Did the structure of quantmod change?

3. At the first time it was all ok. Than I noticed the following error:
> chartSeries(stockData\$GOOG)
Error in .External.graphics(C_layout, num.rows, num.cols, mat, as.integer(num.figures), :
invalid graphics state
In addition: Warning messages:
1: In download.file(paste(yahoo.URL, “s=”, Symbols.name, “&a=”, from.m, :
downloaded length 54559 != reported length 200
2: In download.file(paste(yahoo.URL, “s=”, Symbols.name, “&a=”, from.m, :
downloaded length 50574 != reported length 200
Why?
Thanks.

4. Great site!!!!!
I ran the code but the result was this:
“Error in 1:ncol(x) : argument of length 0
In addition: Warning messages:
1: In download.file(paste(yahoo.URL, “s=”, Symbols.name, “&a=”, from.m, :
downloaded length 13543 != reported length 200
2: In download.file(paste(yahoo.URL, “s=”, Symbols.name, “&a=”, from.m, :
downloaded length 12544 != reported length 200″
Why?