7

I am trying to load multiple symbols using a csv file rather than downloading from Yahoo. The original code works great and uses

load.packages('quantmod')
tickers = spl('TLT,IWM,GLD')
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data,
           auto.assign = T)

when I try using the code below, however, it results in "subscript out of bounds" errors later in the script:

load.packages('quantmod')
tickers = spl('TLT,IWM,GLD')
data <- new.env()
getSymbols(tickers, src="csv", dir= "C:/Users/Admiral/Downloads/",
           env = data, auto.assign = T)

Anyone have thoughts why the second code set wont work? To test I've just downloaded csv data from Yahoo and saved locally (windows). I dont get the subscript errors if I just use one csv file. I've also tried the code below but get the same errors later in the script:

setSymbolLookup(tickers=list(src="csv", dir= "C:/Users/Admiral/Downloads/"))
getSymbols(tickers, auto.assign = T, from = '1980-01-01', env=data)
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
AdmiralF
  • 113
  • 2
  • 8
  • That looks like systematic investor code. Run `rm(index.xts)` then try your code again. (http://stackoverflow.com/questions/12124683/r-index-or-index-xts-changes-the-values-of-date-of-a-time-series-why/12125165#12125165) – GSee Sep 02 '12 at 19:09
  • Yes, it is SI code - I tried your suggestion though and it didnt seem to have any effect. – AdmiralF Sep 02 '12 at 19:17
  • If you want us to find out why getSymbols.csv doesn't work for you, then you'll have to show us some of your CSV files. Or, maybe you could just show how you created them. – GSee Sep 02 '12 at 19:22
  • The csv files are named by symbol and contain the data as downloaded from Yahoo, i.e. in file "IWM": Date Open High Low Close Volume Adj Close 8/28/2012 125.75 126.36 125.59 125.84 3412600 125.84 8/27/2012 125.34 125.8 125.27 125.54 3906100 125.54 8/24/2012 125.47 125.56 124.67 124.81 5001400 124.81 8/23/2012 124.74 125.14 124.65 124.93 7139100 124.93 – AdmiralF Sep 02 '12 at 19:45
  • Ah. I see your problem. The Date must be formatted "%Y-%m-%d", but yours is "%m/%d/%Y". You might be better off using `read.zoo` from the **zoo** package which is much more flexible than `getSymbols.csv` – GSee Sep 02 '12 at 19:59

1 Answers1

3

I would do this using the FinancialInstrument package

require('quantmod')
require('FinancialInstrument')
tickers <- c("TLT", "IWM", "GLD")
data <- new.env()
getSymbols(tickers, src = 'yahoo', from = '1980-01-01', env = data)

# Now save the data in a directory
tmpdir <- tempdir()
saveSymbols.common(tickers, tmpdir, env=data)

#remove the data    
rm(list=tickers, pos=data)
ls(data) # see that there is nothing there
# Now load the data back from disk
getSymbols(tickers, src='FI', dir=tmpdir, env=data, split_method='common')
ls(data)

If you want to use getSymbols.csv, your data has must have the Date and 6 columns (OHLCVA)

#write data to csv files on disk
for (i in seq_along(tickers)) {      
  write.zoo(get(tickers[i], pos=data), file=paste0(tmpdir, "/", tickers[i], ".csv"), sep=",")
}
rm(list=tickers, pos=data) #remove from memory
getSymbols(tickers, src='csv', dir=tmpdir)#, env=data)  #load from csv files
GSee
  • 48,880
  • 13
  • 125
  • 145
  • although, that saves it as `rda` files, not CSVs – GSee Sep 02 '12 at 19:21
  • Thanks for the help. The above works, but can I modify the rda file prior to running the script (ie change closing price)? I am trying to use modified data rather than the Yahoo data. – AdmiralF Sep 02 '12 at 19:38
  • Yes, or you can modify them before you save them. – GSee Sep 02 '12 at 19:45
  • OK great. What I am trying to do is make the SI code "tradeable" by loading intraday data (at say half hour before market close) for "today" to use in the script rather than waiting for closing data that is only available several hours post market close. Any thoughts on how I can use your code to load the "current" close price for "todays" data? – AdmiralF Sep 02 '12 at 19:57
  • 1
    You can use `getQuote` to get a recent quote from yahoo. Then you can use `rbind.xts` to add that data to your `xts` objects. Something like this: http://thread.gmane.org/gmane.comp.lang.r.finance/8748/focus=8770 – GSee Sep 02 '12 at 20:00
  • What in Pasta's creation is saveSymbols.common? That "common" is confusing me. – Gabriel Fair Apr 24 '14 at 03:12
  • @GabrielFair I didn't come up with the terminology -- `saveSymbols.days` and `saveSymbols.common` save data in the way that `getSymbols.FI` (which came first) expects. `saveSymbols.days` splits the objects into days and saves one file for each day for each symbol. `saveSymbols.common` creates a single file (with data for all days) for each symbol. – GSee Apr 24 '14 at 03:31