Search code examples
rtime-seriesxtszooquantmod

How to update my xts environment with daily stock data?


I have loaded and saved my environment with more than 300 stocks historical data using getsymbols function. Now I am trying to update my historical dataset with the new daily data but it is not working. I tried using merge() and rbind() but it seems not working with environments. I appreciate your help with this issue !

Here is my code:

load.packages('quantmod')        
tickers = spl('A,AA,AAL,AAP,AAPL,ABT,ACN,ADBE,ADI,ADM,ADP,ADS,AEE,AEP')
getSymbols(tickers, src = 'yahoo', from = '2010-01-01' , env = data, auto.assign = TRUE)
save(data, file="myTickersData.rda")

Now, I am trying to append it with today's data without re-running it all again from 2010 as it will take a lot of time.

data.today = new.env()
getSymbols(tickers, src = 'yahoo', from = '2015-10-14' , env = data.today, auto.assign = T)
updated.data = merge(data,data.today)

I am getting this error:

Error in as.data.frame.default(x) :
cannot coerce class ""environment"" to a data.frame


Solution

  • Here's a function that should do most of what you need. Some important things it doesn't do:

    1. It doesn't check to ensure that you don't rbind a duplicate date to the historical data.
    2. It doesn't back-adjust all historical data if there's a split, dividend, etc between updates.

    To solve the second issue, you would need to re-pull all the data for the given symbol.

    updateData <- function(oldData, newData) {
      # ensure both arguments are environments
      stopifnot(is.environment(oldData) || is.environment(newData))
      # track symbols that aren't in the new environment
      unmatchedSym <- NULL
      # loop over all symbols in the historical data environment
      for (sym in ls(oldData)) {
        # get a copy of the symbol data
        oldSym <- get(sym, oldData)
        # try to get the new data
        newSym <- try(get(sym, newData))
        if (inherits(newSym, "try-error")) {
          unmatchedSym <- c(unmatchedSym, sym)
          next
        } else {
          # rbind old/new data
          # might want to check to ensure you're not adding a dupicate row
          combined <- rbind(oldSym, newSym)
          # update historical data environment
          assign(sym, combined, oldData)
        }
      }
      # return unmatched symbols (invisibly)
      invisible(unmatchedSym)
    }
    

    Here's a usage example:

    library(quantmod)
    oldData <- new.env()
    getSymbols("A;AA;AAL", env=oldData, to="2015-09-30")
    
    newData <- new.env()
    getSymbols("A;AA;AAL", env=newData, from="2015-10-01")
    
    updateData(oldData, newData)
    eapply(oldData, tail)
    # $AA
    #            AA.Open AA.High AA.Low AA.Close AA.Volume AA.Adjusted
    # 2015-10-08   10.85   11.11  10.63    11.01  34172000       11.01
    # 2015-10-09   10.67   10.92  10.25    10.26  78627900       10.26
    # 2015-10-12   10.28   10.29   9.97    10.08  31371900       10.08
    # 2015-10-13    9.96   10.22   9.86    10.03  36220300       10.03
    # 2015-10-14   10.09   10.11   9.91     9.95  42245400        9.95
    # 2015-10-15    9.93   10.00   9.63     9.70  49779200        9.70
    #
    # $A
    #           A.Open A.High A.Low A.Close A.Volume A.Adjusted
    # 2015-10-08  35.39  36.07 35.34   36.01  2597900      36.01
    # 2015-10-09  35.94  36.34 35.88   36.23  3230000      36.23
    # 2015-10-12  36.08  36.18 35.86   35.99  1258300      35.99
    # 2015-10-13  35.76  36.24 35.53   35.63  2379300      35.63
    # 2015-10-14  35.64  35.77 34.97   35.05  1644300      35.05
    # 2015-10-15  35.12  35.68 34.79   35.58  1427300      35.58
    #
    # $AAL
    #           AAL.Open AAL.High AAL.Low AAL.Close AAL.Volume AAL.Adjusted
    # 2015-10-08    39.21    40.03   38.70     39.74    8303200        39.74
    # 2015-10-09    40.27    42.49   40.25     42.42   16797000        42.42
    # 2015-10-12    42.59    43.88   42.55     43.84   11437200        43.84
    # 2015-10-13    42.70    43.97   42.52     43.41   13212600        43.41
    # 2015-10-14    44.00    44.50   43.23     43.80   11121500        43.80
    # 2015-10-15    44.10    44.48   43.50     44.46    8179500        44.46