Skip to content

pass character vector in instrument field of get.hist.quote function

2 messages · S, David Winsemius

S
#
Hi Joshua,

THank you for showing me how to use getSymbols. I am trying to follow the
example you provided. However I am having some difficulty using the various
combination of functions you have used. I tried to execute one step at a
time as follows - 

I have a ticker vector that looks like the following:
[1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" "XLK" "XLU"
"XLV"
[14] "QQQ"
chr [1:14] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" ...

I wrote a function called myX to use in the lapply call. It has the
following code: 

myX <- function(tickers, start, end) {
require(quantmod) 
getSymbols(tickers, from=start, to=end)
}


1) Call lapply by itself
[[1]]
[1] "SPY"

[[2]]
[1] "DIA"

[[3]]
[1] "IWM"

[[4]]
[1] "SMH"

[[5]]
[1] "OIH"

[[6]]
[1] "XLY"

[[7]]
[1] "XLP"

[[8]]
[1] "XLE"

[[9]]
[1] "XLI"

[[10]]
[1] "XLB"

[[11]]
[1] "XLK"

[[12]]
[1] "XLU"

[[13]]
[1] "XLV"

[[14]]
[1] "QQQ"

So this works fine and I can inspect the value of any of the tickers i.e.
SPY. 

Now I want to extract the Closing prices. 

2) I did Cl(SPY) and this outputs the data in the Close column as expected.
However, I am not sure how to extract the Closing prices of each of the
elements inside the data structure returned by lapply, which I believe is a
list structure. I want to merge them into one object as you did but I cant
seem to follow.

Any guidance would be greatly appreciated.



--
View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3431118.html
Sent from the R help mailing list archive at Nabble.com.
#
On Apr 6, 2011, at 12:06 PM, algotr8der wrote:

            
only needed to use three for demonstration purposes
As pointed out by Joshua Ulrich this was crossposted on SO. Here is  
the code I posted there:

 > ClosePrices <- do.call(cbind, lapply(tickers,  function(x)  
Cl(get(x))))
 > head(ClosePrices)
            SPY.Close DIA.Close QQQ.Close
2001-03-01    124.60    104.68     48.80
2001-03-02    123.61    104.80     46.70
2001-03-05    124.74    105.57     47.55
2001-03-06    126.08    106.15     49.40
2001-03-07    126.98    107.45     49.42
2001-03-08    127.12    108.61     48.50