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

David Winsemius dwinsemius at comcast.net
Sun Mar 13 07:48:32 CET 2011


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 <algotr8der at gmail.com>  
> 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



More information about the R-help mailing list