getSymbols with csv in Quantmod R

2.4k views Asked by At

I am trying to upload a group of symbols into the package quantstrat using quantmod::getSymbols.

The symbols I am loading are not available on Yahoo (they are South African stocks), so I need to load them from a local directory and from .csv files.

My symbols file looks as follows:

head(symbols)

[1] "SHFJ" "FSRJ" "RDFJ" "GRTJ" "MTNJ" "SLMJ"....

My symbol price history are in separate csv files, and each contain a date column and OHLC columns, with a header only for the OHLC prices.

I use the function getSymbols.csv function as follows:

getSymbols.csv(symbols, env, dir="E:/data/CData_Files_NB/", return.class = "xts", extension="csv")

but I get the following error message

loading  SHFJ .....done.
Error in `colnames<-`(`*tmp*`, value = c("SHFJ.Open", "SHFJ.High","SHFJ.Low",  : length of 'dimnames' [2] not equal to array extent

I would much appreciate it if anyone could show me what I am doing wrong. I am not sure if there is another way to load stock prices into the quantstrat package.

2

There are 2 answers

3
Vincent On BEST ANSWER

getsymbols.csv expects six columns: Open, High, Low, Close, Volume, Adjusted

Your data does not have the "Adjusted" column; it has 5 column names instead of 6, which is causing the dimnames error you're seeing.

If you can modify the local data files, try adding an empty "Adjusted" column (easily done by opening the CSVs in LibreOffice or Excel,)

or try duplicating the getsymbols.csv() function and changing:

colnames(fr) <- paste(toupper(gsub("\\^", "", Symbols[[i]])), 
            c("Open", "High", "Low", "Close", "Volume", "Adjusted"), 
            sep = ".")

To

colnames(fr) <- paste(toupper(gsub("\\^", "", Symbols[[i]])), 
            c("Open", "High", "Low", "Close", "Volume"), 
            sep = ".")

In order to support your data.

0
sempedocles On

Try the following code:

library("quantmod")
Stocks<-c("JSE:APN","JSE:BAT","JSE:CPI","JSE:DSY","JSE:NPN","JSE:DST")
getSymbols(Stocks,src="google",auto.assign=TRUE)
get("JSE:NPN")

JSE:NPN - "Johannesburg Stock Exchange : Share code" (Naspers - NPN)

(The data is available on google finance)