Question

I am new to R and have just started to use it. I am currently experimenting with the quantmod package.

The quantmod package seems to do most of what I want to do, however, I dont want to use the getSymbols() function to fetch data into R. Instead, I want to use my own data - stored as csv files on my local disc.

I want to be able to slurp the data from my CSV files for use with quantmod. I came accros this article, which shows how to do read CSV files for use with quantmod, but I don't like it for at least 2 reasons:

  1. It writes a new (reformatted) CSV file to disc before loading into quantmod. I would much rather do any necessary munging in memory, using R.

  2. The CSV file has column headers. My data dosen't have column headers. Instead the fields are at predetermined fixed column positions (matches the 'standard' format adopted by Yahoo Finance data tables).

I haven't managed to work out the data type returned by the getSymbols() function. I expected it to return a data frame, yet when I checked its class, it was identified as a character vector - which I found very suprising (and frankly, don't believe, since I am able to plot a barChart from the data contained in the variable):

yhoo <- getSymbols("YHOO",src="google")
class(yhoo)
[1] "character"
> yhoo
[1] "YHOO"

I would be grateful if someone could show how to write a small R function (most likely a wrapper around read.csv) that will read data from my CSV file and return it as an R object (data frame?) for use with quantmod.

Here is some pseudocode explaining what I want to do:

# in case I need some funcs here for creating data type returned by function
library(quantmod) 

loadCSVDataFile <- function(full_pathname){
    csvdata <- read.csv(full_pathname, header=FALSE,sep=",")
    dates <- csvdata[,1]
    op <- csvdata[,2]
    hi <- csvdata[,3]
    lo <- csvdata[,4]
    cl <- csvdata[,5]
    vol <- csvdata[,6]
    oi <- csvdata[,7]

    # Now combine columns into a data type that matches that returned by the
    # getSymbols() ....
    # return(dataset)
}

[[Update]]

I have STLL not managed to get this to work, using the answers given so far ...:

> gbpusd <- as.xts(read.zoo('/path/to/gbpusd.csv', header=FALSE))
> class (gbpusd)
[1] "xts" "zoo"
> barChart(gbpusd)
Error in `[.xts`(x, xsubset) : subscript out of bounds

> gbpusd2 <- getSymbols.csv('gbpusd',,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',.GlobalEnv,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> 
> gbpusd2 <- getSymbols.csv('gbpusd','.GlobalEnv','/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments
> 
> gbpusd2 <- getSymbols.csv('gbpusd',env,'/path/to/')
Error in missing(verbose) : 'missing' can only be used for arguments

What am I doing wrong?

Was it helpful?

Solution

I can make it work, but you have to determine which parameters are needed for your setup.

library(quantmod)

# create sample data
getSymbols("SPY")
write.zoo(SPY, file="SPY.csv", sep=",")

# set symbol lookup
setSymbolLookup(SPY=list(src="csv",format="%Y-%m-%d"))
# call getSymbols(.csv) with auto.assign=FALSE
spy <- getSymbols("SPY", auto.assign=FALSE)
barChart(spy)

OTHER TIPS

The common idiom is:

yhoo = as.xts(read.zoo("yhoo.csv",header=T))

If you want to use a quantmod function, then you can tell getSymbols() to use a csv file. See http://www.quantmod.com/documentation/getSymbols.csv.html I.e.

getSymbols('yhoo',src='csv')

(I've followed your lowercase convention, but remember the filenames will be case-sensitive; directory defaults to current directory and can be specified explicitly with the dir parameter, see ?getSymbols.csv)

If you include a ls() in your code, you'll find that you missed a variable:

yhoo <- getSymbols("YHOO", src = "google")
ls()
# [1] "yhoo" "YHOO"
class(yhoo)
# [1] "character"
class(YHOO)
# [1] "xts" "zoo"

So getSymbols() creates 2 variables: the character vector "yhoo" and "YHOO", an object of class extensible time series (xts) which extends the zoo class, for storing irregular time series data.

To see the documentation, use:

?xts # or ?zoo

In particular, the documentation for xts describes the as.xts() function, to convert from a matrix to xts. It should be straightforward from there to read in your own CSV files using read.csv or read.table and convert to xts objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top