|
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) 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. |
|
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. > > ______________________________________________ > [hidden email] 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 ______________________________________________ [hidden email] 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. |
|
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) |
|
On Mar 13, 2011, at 12:18 AM, algotr8der wrote: > 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) You might want to look more closely at: ?get.hist.quote Note the name of the first argument .... and then note that it does not say "names" but only a "name" would be accepted as a value to that argument. stockdata <- coredata(get.hist.quote(instrument=symbols, + start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo")) trying URL 'http://chart.yahoo.com/table.csv?s=XOM&a=0&b=01&c=2011&d=0&e=31&f=2011&g=d&q=q&y=0&z=XOM&x=.csv' Content type 'text/csv' length unknown opened URL . downloaded 1042 bytes time series starts 2011-01-03 Warning message: In download.file(url, destfile, method = method, quiet = quiet) : only first element of 'url' argument used (So this is telling you that get.hist.quote did not accept a vector of characters.) You should be thinking about what data structure you want to work with. A dataframe does not seem like a good choice to me. I would be thinking of a list. res <- lapply(symbols[1:5], get.hist.quote, start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo") names(res) <- symbols[1:5] The examples suggested that a 'merge' operation might also be used. Perhaps you should be looking for a vignette that shows how to assemble a "portfolio" of timeseries. -- David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
In reply to this post by algotr8der
Hi:
(1) The first argument to get.hist.quote() is instrument, not instruments. I concur with David that get.hist.quote() takes a single character string as an argument. (2) I tried running this with lapply() but got a download error on the last one: getStockData("PBR-B") trying URL ' http://chart.yahoo.com/table.csv?s=PBR-B&a=0&b=01&c=2011&d=0&e=31&f=2011&g=d&q=q&y=0&z=PBR-B&x=.csv ' Error in download.file(url, destfile, method = method, quiet = quiet) : cannot open URL ' http://chart.yahoo.com/table.csv?s=PBR-B&a=0&b=01&c=2011&d=0&e=31&f=2011&g=d&q=q&y=0&z=PBR-B&x=.csv ' In addition: Warning message: In download.file(url, destfile, method = method, quiet = quiet) : cannot open: HTTP status was '404 Not Found' If we take out that (last) symbol , convert the remaining symbols to a vector, create a simple download function and use lapply(), then... Tickers <- as.vector(unlist(tickers))[-6] # character vector getStockData <- function(symbols) { coredata(get.hist.quote(instrument = symbols, start="2011-01-01", end="2011-01-31", quote=c("Close"), provider="yahoo")) } stockdata <- lapply(Tickers, getStockData) # produces a list of five components names(stockdata) <- Tickers stockdata[[1]] $XOM Close [1,] 74.55 [2,] 74.90 [3,] 74.70 <snip> [18,] 79.88 [19,] 78.99 [20,] 80.68 HTH, Dennis On Sat, Mar 12, 2011 at 9:18 PM, algotr8der <[hidden email]> wrote: > 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. > > ______________________________________________ > [hidden email] 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. > [[alternative HTML version deleted]] ______________________________________________ [hidden email] 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. |
|
On Mar 13, 2011, at 1:28 AM, Dennis Murphy wrote: > Hi: > > (1) The first argument to get.hist.quote() is instrument, not > instruments. I > concur with David that get.hist.quote() > takes a single character string as an argument. > > (2) I tried running this with lapply() but got a download error on > the last > one: > > getStockData("PBR-B") > trying URL ' > http://chart.yahoo.com/table.csv?s=PBR-B&a=0&b=01&c=2011&d=0&e=31&f=2011&g=d&q=q&y=0&z=PBR-B&x=.csv > ' > Error in download.file(url, destfile, method = method, quiet = > quiet) : > cannot open URL ' > http://chart.yahoo.com/table.csv?s=PBR-B&a=0&b=01&c=2011&d=0&e=31&f=2011&g=d&q=q&y=0&z=PBR-B&x=.csv > ' > In addition: Warning message: > In download.file(url, destfile, method = method, quiet = quiet) : > cannot open: HTTP status was '404 Not Found' > > > If we take out that (last) symbol , convert the remaining symbols to a > vector, create a simple download function and use lapply(), then... > > Tickers <- as.vector(unlist(tickers))[-6] # character vector > getStockData <- function(symbols) { > coredata(get.hist.quote(instrument = symbols, Is there a particular reason to use `coredata` here? I got pretty much identical results with the method I illustrated without it, except that my approach preserved the dates, while yours removed the date rownames. That would not seem to be an improvement. > res[[1]] Close 2011-01-03 74.55 2011-01-04 74.90 2011-01-05 74.70 snipped 2011-01-28 78.99 2011-01-31 80.68 > start="2011-01-01", end="2011-01-31", > quote=c("Close"), provider="yahoo")) > } > stockdata <- lapply(Tickers, getStockData) # produces a list of > five > components > names(stockdata) <- Tickers > stockdata[[1]] > $XOM > Close > [1,] 74.55 > [2,] 74.90 > [3,] 74.70 > <snip> > [18,] 79.88 > [19,] 78.99 > [20,] 80.68 > > HTH, > Dennis > > > On Sat, Mar 12, 2011 at 9:18 PM, algotr8der <[hidden email]> > wrote: > >> 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) >> David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
Thank you all. Your solutions work. I suppose it doesnt matter whether I use coredata or not (at least for my current purposes). I have created a user-defined function and have saved it in a ".R" file in my current working directory. But now I'm not sure how to invoke this function. R complains that it cannot find the function.
If I copy and paste the function code into the R console and call it after that everything works fine. Is there a way to bring that function into memory by giving a path to its location? I imagine that I will have many user-defined functions written in their own '.R' files over time. Thank you all. |
|
On Mar 13, 2011, at 2:29 PM, algotr8der wrote: > Thank you all. Your solutions work. I suppose it doesnt matter > whether I use > coredata or not (at least for my current purposes). I have created a > user-defined function and have saved it in a ".R" file in my current > working > directory. But now I'm not sure how to invoke this function. R > complains > that it cannot find the function. > > If I copy and paste the function code into the R console and call it > after > that everything works fine. Is there a way to bring that function into > memory by giving a path to its location? ?source > I imagine that I will have many > user-defined functions written in their own '.R' files over time. > > Thank you all. David Winsemius, MD West Hartford, CT ______________________________________________ [hidden email] 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. |
|
In reply to this post by algotr8der
Sorry for the late reply. Here's another approach using
quantmod::getSymbols. It implicitly loops over a vector of tickers. require(quantmod) getSymbols(tickers[,1], from="2011-01-01", to="2011-01-31") # If you want to merge all the close prices into one object: ClosePrices <- do.call(merge, lapply(tickers[,1], function(x) Cl(get(x)))) head(ClosePrices) # BCSI.Close WBSN.Close NTAP.Close FFIV.Close SU.Close # 2011-01-03 30.50 20.36 57.41 134.33 38.82 # 2011-01-04 30.24 19.82 57.38 132.07 38.03 # 2011-01-05 31.36 19.90 57.87 137.29 38.40 # 2011-01-06 32.04 19.79 57.49 138.07 37.23 # 2011-01-07 31.95 19.77 57.20 138.35 37.30 # 2011-01-10 31.55 19.76 58.22 142.69 37.04 Hope that helps, -- Joshua Ulrich | FOSS Trading: www.fosstrading.com On Sun, Mar 13, 2011 at 1:29 PM, algotr8der <[hidden email]> wrote: > Thank you all. Your solutions work. I suppose it doesnt matter whether I use > coredata or not (at least for my current purposes). I have created a > user-defined function and have saved it in a ".R" file in my current working > directory. But now I'm not sure how to invoke this function. R complains > that it cannot find the function. > > If I copy and paste the function code into the R console and call it after > that everything works fine. Is there a way to bring that function into > memory by giving a path to its location? I imagine that I will have many > user-defined functions written in their own '.R' files over time. > > Thank you all. > > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3352344.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
|
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) 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" [[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. |
|
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" > > > [[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 ______________________________________________ [hidden email] 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. |
|
Hello gentlemen,
This is a great discussion. Thank you all for providing useful answers. I am also new to R, was working mostly with SAS before, and have a question regarding passing arguments to R functions that are wrapped in quotes. Using the previous example: getSymbols(tickers, from="start", to="end") Given that "start/end" are quoted, how can I pass a date xx-xx-xxxx to R when I am invoking a custom function, like: flex<- (start, end){ getSymbols(tickers, from="start", to="end") } flex(xx-xx-xxxx,xx-xx-xxxx) I have tried different combinations but I haven't been able to pass the correct date, R simply doesn't capture the value. The same issue was happening with tickers, although there is a way around the issue, the original problem remains....passing an argument that's quoted in the function. Any help would be greatly appreciated! Cheers! |
|
Depends on what you want to do with the date. If it is a character
string, then just quote it: flex("xx-xx-xxxx","xx-xx-xxxx") If you want it as a Date or POSIXct object, then convert it: flex(as.Date('2011-05-01"), as.Date('2011;05-31")) It all depends on how you are using it in your custom function. In either case you still enclose it in quotes. On Wed, May 11, 2011 at 5:44 PM, MatAra <[hidden email]> wrote: > Hello gentlemen, > > This is a great discussion. Thank you all for providing useful answers. > I am also new to R, was working mostly with SAS before, and have a question > regarding passing arguments to R functions that are wrapped in quotes. > > Using the previous example: > getSymbols(tickers, from="start", to="end") > > Given that "start/end" are quoted, how can I pass a date xx-xx-xxxx to R > when I am invoking a custom function, like: > > flex<- (start, end){ > getSymbols(tickers, from="start", to="end") > } > flex(xx-xx-xxxx,xx-xx-xxxx) > > I have tried different combinations but I haven't been able to pass the > correct date, R simply doesn't capture the value. The same issue was > happening with tickers, although there is a way around the issue, the > original problem remains....passing an argument that's quoted in the > function. > > Any help would be greatly appreciated! > Cheers! > > > > -- > View this message in context: http://r.789695.n4.nabble.com/pass-character-vector-in-instrument-field-of-get-hist-quote-function-tp3350779p3515936.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] 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. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? ______________________________________________ [hidden email] 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. |
|
Thanks a lot for your reply Jim.
I want to be able to run flex(xx-xx-xxxx,xx-xx-xxxx) at multiple date ranges, but couldn't make it work because I wasn't using the correct syntax. I will give it a try and post my findings (not too spectacular, of course). Thanks again, Mateo |
| Powered by Nabble | Edit this page |
