Search code examples
rquantmod

R getsymbols from csv file


I am trying to use getSymbols (quantmod) package in R to download stock prices from a list of stocks that I have in a .csv file.

I have the .csv file imported into R but unsure on how to use getSymbols to read from a .csv file

So I have my list of stock symbols and I want getSymbols to download the price data for each symbol in the list.


Solution

  • The only difficulty I see is that getSymbols takes a character vector as inputs, not a factor. So you'll have to be careful and use stringsAsFactors = FALSE when reading your symbols from a file:

    csv <- read.csv(textConnection("
    
    SYMBOLS
    IBM
    GOOG
    YHOO
    
    "), stringsAsFactors = FALSE)
    
    library(quantmod)
    getSymbols(csv$SYMBOLS)
    # [1] "IBM"  "GOOG" "YHOO"
    

    Alternatively, if you already have your symbols in a factor named x, you can run getSymbols(as.character(x)).