Downloading Yahoo stock prices in R

后端 未结 8 625
野趣味
野趣味 2020-12-12 13:46

This is a newbie question in R. I am downloading yahoo finance monthly stock price data using R where the ticker names are read from a text file. I am using a loop to read

相关标签:
8条回答
  • 2020-12-12 14:01

    I do it like this, because I need to have the historic pricelist and a daily update file in order to run other packages:

    library(fImport)
    
    fecha1<-"03/01/2009"
    fecha2<-"02/02/2010"
    
    Sys.time()
    
    y <- format(Sys.time(), "%y")    
    m <- format(Sys.time(), "%m")    
    d <- format(Sys.time(), "%d")
    fecha3 <- paste(c(m,"/",d,"/","20",y), collapse="")
    
    write.table(yahooSeries("GCI", from=fecha1, to=fecha2), file = "GCI.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)
    write.table(yahooSeries("GCI", from=fecha2, to=fecha3), file = "GCIupdate.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)
    
    GCI <- read.table("GCI.txt") 
    GCI1 <- read.table("GCIupdate.txt")
    GCI <- rbind(GCI1, GCI)
    GCI <- unique(GCI)
    
    write.table(GCI, file = "GCI.txt", sep="\t", quote = FALSE, eol="\r\n", row.names = TRUE)
    
    0 讨论(0)
  • 2020-12-12 14:01

    Slightly modified from the above solutions... (thanks Shane and Stotastic)

     symbols <- c("MSFT", "C", "MMM")
    
     # 1. retrieve data
    
     for(i in seq_along(symbols)) {
       URL <- paste0("http://ichart.finance.yahoo.com/table.csv?s=", symbols[i])
       dat <- read.csv(URL)
       dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
       assign(paste0(symbols[i]," _data"), dat)
       dat <- NULL
     }
    
    0 讨论(0)
  • 2020-12-12 14:02

    This also a little late...If you want to grab data with just R's base functions without dealing with any add-on packages, just use the function read.csv(URL), where the URL is a string pointing to the right place at Yahoo. The data will be pulled in as a dataframe, and you will need to convert the 'Date' from a string to a Date type in order for any plots to look nice. Simple code snippet is below.

    URL <- "http://ichart.finance.yahoo.com/table.csv?s=SPY"
    dat <- read.csv(URL)
    dat$Date <- as.Date(dat$Date, "%Y-%m-%d")
    

    Using R's base functions may give you more control over the data manipulation.

    0 讨论(0)
  • 2020-12-12 14:13

    Unfortunately, URL "ichart.finance.yahoo.com" is dead and not working now. As I know, Yahoo closed it and it seems it will not be opened.

    Several days ago I found nice alternative (https://eodhistoricaldata.com/) with an API very similar to Yahoo Finance.

    Basically, for R-script described above you just need to change this part:

    URL <- paste0("ichart.finance.yahoo.com/table.csv?s=", symbols[i])
    

    to this:

    URL <- paste0("eodhistoricaldata.com/api/table.csv?s=", symbols[i])
    

    Then add an API key and it will work in the same way as before. I saved a lot of time for my R-scripts on it.

    0 讨论(0)
  • 2020-12-12 14:18

    If your ultimate goal is to get the data.frame of three columns of closing prices, then the new package tidyquant may be better suited for this.

    library(tidyquant)
    
    symbols <- c("MSFT", "C", "VIA/B", "MMM")
    
    # Download data in tidy format. 
    # Will remove VIA/B and warn you.
    data <- tq_get(symbols)
    
    # Ticker symbols as column names for closing prices
    data %>% 
        select(.symbol, date, close) %>% 
        spread(key = .symbol, value = close)
    

    This will scale to any number of stocks, so the file of 1000 tickers should work just fine!

    0 讨论(0)
  • 2020-12-12 14:26

    Your best bet is to use quantmod and store the results as a time series (in this case, it will be xts):

    library(quantmod)
    library(plyr)
    symbols <- c("MSFT","C","VIA/B","MMM")
    
    #1
    l_ply(symbols, function(sym) try(getSymbols(sym))) 
    symbols <- symbols[symbols %in% ls()]
    
    #2
    sym.list <- llply(symbols, get) 
    
    #3
    data <- xts()
    for(i in seq_along(symbols)) {
        symbol <- symbols[i]
        data <- merge(data, get(symbol)[,paste(symbol, "Close", sep=".")])
    }
    
    0 讨论(0)
提交回复
热议问题