[R-SIG-Finance] quantmod, getSymbols, csv

Jeff Ryan jeff.a.ryan at gmail.com
Thu Jul 17 19:14:08 CEST 2008


Something like:

> getSymbols.John
function (Symbols, env, dir = "", return.class = "xts", extension = "csv",
    ...)
{
    importDefaults("getSymbols.csv")
    this.env <- environment()
    for (var in names(list(...))) {
        assign(var, list(...)[[var]], this.env)
    }
    default.return.class <- return.class
    default.dir <- dir
    default.extension <- extension
    if (missing(verbose))
        verbose <- FALSE
    if (missing(auto.assign))
        auto.assign <- TRUE
    for (i in 1:length(Symbols)) {
        return.class <- getSymbolLookup()[[Symbols[[i]]]]$return.class
        return.class <- ifelse(is.null(return.class), default.return.class,
            return.class)
        dir <- getSymbolLookup()[[Symbols[[i]]]]$dir
        dir <- ifelse(is.null(dir), default.dir, dir)
        extension <- getSymbolLookup()[[Symbols[[i]]]]$extension
        extension <- ifelse(is.null(extension), default.extension,
            extension)
        if (verbose)
            cat("loading ", Symbols[[i]], ".....")
        if (dir == "") {
            sym.file <- paste(Symbols[[i]], extension, sep = ".")
        }
        else {
            sym.file <- file.path(dir, paste(Symbols[[i]], extension,
                sep = "."))
        }
        if (!file.exists(sym.file)) {
            cat("\nfile ", paste(Symbols[[i]], "csv", sep = "."),
                " does not exist ", "in ", dir, "....skipping\n")
            next
        }
        fr <- read.csv(sym.file, header=TRUE) #added header=TRUE
        if (verbose)
            cat("done.\n")
        fr <- xts(matrix(fr$price, dimnames=list(index(x),'price')),
as.Date(fr$date),   # added for getSymbols.John
            src = "csv", updated = Sys.time()) # added for getSymbols.John

# removed colnames call from original getSymbols.csv

        fr <- convert.time.series(fr = fr, return.class = return.class)
        Symbols[[i]] <- toupper(gsub("\\^", "", Symbols[[i]]))
        if (auto.assign)
            assign(Symbols[[i]], fr, env)
    }
    if (auto.assign)
        return(Symbols)
    return(fr)
}
<environment: namespace:quantmod>

> getSymbols('CREFglobaleq', src='John')
[1] "CREFGLOBALEQ"
> CREFGLOBALEQ
             price
1992-05-01 25.8429
1992-05-04 25.9774
1992-05-05 25.9964
1992-05-06 26.2335
1992-05-07 26.3368

I am also planning on making a skeleton function/functionality much
like newTA does for the charts - so hand modifications will be less
important.

Jeff

On Thu, Jul 17, 2008 at 12:05 PM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
> Hi John,
>
> getSymbols.csv is not very robust at this time.  What needs to be done
> is a clean generalization of the call, for cases that can't be covered
> (like the date in the 3rd col, and superfluous data in cols 1 and 4,
> if I am reading your data correctly).
>
> I suspect something to identify the timestamp column, and the columns
> you would like to include would make this more useful.  The other
> option is to create your own custom getSymbols.John function, so
> getSymbols('FILE', src="John") would do exactly what you want.  Simply
> adding the below in the appropriate places to the getSymbols.csv
> source would be all that is required.
>
> For now:
>
> x <- read.csv('CREFglobeq.csv',header=TRUE)
> x <- xts(matrix(x$price,dimnames=list(index(x),'price')),as.Date(x$date))
>
>             price
> 1992-05-01 25.8429
> 1992-05-04 25.9774
> 1992-05-05 25.9964
> 1992-05-06 26.2335
> 1992-05-07 26.3368
>
> xts can be replaced by zoo, or any other time-series constructor you like.
>
> Jeff
>
>
> On Thu, Jul 17, 2008 at 11:10 AM, John P. Burkett <burkett at uri.edu> wrote:
>> Making a first attempt use quantmod's getSymbols to load data from a .csv
>> file, I have encountered a puzzling error message.
>> The response to
>> getSymbols("CREFglobaleq", from="1992-05-01", to="2008-06-30", src="csv")
>> is
>> Error in fromchar(x) :
>>  character string is not in a standard unambiguous format
>>
>> My basic problem is ignorance about what is acceptable as "a standard
>> unambiguous format."  Explanations or references to documentation would be
>> greatly appreciated.
>>
>> The first few rows of my CREFglobaleq.csv file read as follows:
>> fund     ,  price  ,  date      ,  id
>> CREFglob , 25.8429 , 1992-05-01 , 199205
>> CREFglob , 25.9774 , 1992-05-04 , 199205
>> CREFglob , 25.9964 , 1992-05-05 , 199205
>> CREFglob , 26.2335 , 1992-05-06 , 199205
>> CREFglob , 26.3368 , 1992-05-07 , 199205
>>
>> Best regards,
>> John
>>
>>
>>
>> --
>> John P. Burkett
>> Department of Environmental and Natural Resource Economics
>> and Department of Economics
>> University of Rhode Island
>> Kingston, RI 02881-0808
>> USA
>>
>> phone (401) 874-9195
>>
>> _______________________________________________
>> R-SIG-Finance at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only.
>> -- If you want to post, subscribe first.
>>
>
>
>
> --
> Jeffrey Ryan
> jeffrey.ryan at insightalgo.com
>
> ia: insight algorithmics
> www.insightalgo.com
>



-- 
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com



More information about the R-SIG-Finance mailing list