algotr8der
2011-Mar-12 20:08 UTC
[R] pass character vector in instrument field of get.hist.quote function
I am new to R so I apologize if my question is trivial. I have not been able to figure out whether what I want to do is even possible. I have a data frame of stock ticker symbols which I store into R space from a txt file as follows: tickers <- read.csv("stocks.txt", header=FALSE, sep=",") tickers <- tickers[1] / the tickers are stored in the first column> tickersV1 1 BCSI 2 WBSN 3 NTAP 4 FFIV 5 SU> is.vector(tickers)[1] FALSE> > is.data.frame(tickers)[1] TRUE I tried to do the following without success. stockdata <- data.frame() stockdata <- coredata(get.hist.quote(instruments=tickers, start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo")) Error in get.hist.quote(instruments = tickers, start = "2011-01-01", end "2011-01-31", : unused argument(s) (instruments = tickers) So I coerced the data stored in 'tickers' to be a character as follows:> as.character(tickers)Then I tried to execute get.hist.quote as above with the same erro. stockdata <- coredata(get.hist.quote(instruments=tickers, start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo")) Error in get.hist.quote(instruments = tickers, start = "2011-01-01", end "2011-01-31", : unused argument(s) (instruments = tickers) I dont want to use loops because of inefficiencies. I know in Matlab I can take advantage of the matrix/vector environment by passing a variable as a vector. I think there should be a clean way to do what I want to do. I have 1500+ tickers and I dont want to cycle through each of them in a loop. I would greatly appreciate some guidance. -- View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3350779.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Mar-12 21:46 UTC
[R] pass character vector in instrument field of get.hist.quote function
On Mar 12, 2011, at 3:08 PM, algotr8der wrote:> I am new to R so I apologize if my question is trivial. I have not > been able > to figure out whether what I want to do is even possible. > > I have a data frame of stock ticker symbols which I store into R > space from > a txt file as follows: > > tickers <- read.csv("stocks.txt", header=FALSE, sep=",") > tickers <- tickers[1] / the tickers are stored in the first column > >> tickers > V1 > 1 BCSI > 2 WBSN > 3 NTAP > 4 FFIV > 5 SU > >> is.vector(tickers) > [1] FALSE >> >> is.data.frame(tickers) > [1] TRUE > > I tried to do the following without success. > > stockdata <- data.frame() > stockdata <- coredata(get.hist.quote(instruments=tickers, > start="2011-01-01", end="2011-01-31", quote=c("Close"), > provider="yahoo")) > > Error in get.hist.quote(instruments = tickers, start = "2011-01-01", > end > "2011-01-31", : > unused argument(s) (instruments = tickers) > > So I coerced the data stored in 'tickers' to be a character as > follows: > >> as.character(tickers)No, you did not. You need to make an assignment of any result, AND you need to reference the column name: tickers$V1 <- as.character(tickers$V1) You should also have reference the package from which get.hist.quote was being used. -- david.> > Then I tried to execute get.hist.quote as above with the same erro. > > stockdata <- coredata(get.hist.quote(instruments=tickers, > start="2011-01-01", end="2011-01-31", quote=c("Close"), > provider="yahoo")) > > Error in get.hist.quote(instruments = tickers, start = "2011-01-01", > end > "2011-01-31", : > unused argument(s) (instruments = tickers) > > I dont want to use loops because of inefficiencies. I know in Matlab > I can > take advantage of the matrix/vector environment by passing a > variable as a > vector. I think there should be a clean way to do what I want to do. > I have > 1500+ tickers and I dont want to cycle through each of them in a loop. > > I would greatly appreciate some guidance. > > -- > View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3350779.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
algotr8der
2011-Mar-13 05:18 UTC
[R] pass character vector in instrument field of get.hist.quote function
Thanks David for the reply. I just tried the following with the same result:>library(tseries) >tickers <- read.csv("testticker.txt", header=FALSE, sep=",") >tickers <- tickers[1]V1 1 XOM 2 COP 3 PBR-A 4 FFIV 5 SU 6 PBR-B> tickers$V1 <- as.character(tickers$V1) > tickers$V1[1] "XOM" "COP" "PBR-A" "FFIV" "SU" "PBR-B"> symbols <- tickers$V1 > symbols[1] "XOM" "COP" "PBR-A" "FFIV" "SU" "PBR-B"> stockdata <- data.frame() > stockdata <- coredata(get.hist.quote(instruments=symbols, > start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo"))Error in get.hist.quote(instruments = symbols, start = "2011-01-01", end "2011-01-31", : unused argument(s) (instruments = symbols) -- View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3351331.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Apr-07 01:13 UTC
[R] pass character vector in instrument field of get.hist.quote function
On Apr 6, 2011, at 12:06 PM, algotr8der wrote:> 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: > >> tickers > [1] "SPY" "DIA" "IWM" "SMH" "OIH" "XLY" "XLP" "XLE" "XLI" "XLB" > "XLK" "XLU" > "XLV" > [14] "QQQ" >> str(tickers) > 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) # this only needs to be called once. > getSymbols(tickers, from=start, to=end) > } > > > 1) Call lapply by itself > >> lapply(tickers,myX,start="2001-03-01", end="2011-03-11") > >> lapply(tickers,myX,start="2001-03-01", end="2011-03-11") > [[1]] > [1] "SPY" > > [[2]] > [1] "DIA" >only needed to use three for demonstration purposes> > [[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.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 -- David Winsemius, MD West Hartford, CT