1
$\begingroup$

I am trying to update daily prices from yahoo via below R code, but code is not working properly and i am not getting any error as such. One can see the reference code at following link.

http://www.thertrader.com/

library(quantmod)
lookback = 60
startDate = Sys.Date() - lookback
thePath = "F:\\Portfolio Management\\"
theFiles = list.files(path=thePath,pattern=".csv")

for (ii in theFiles){
  data = read.csv(paste(thePath,ii,sep=""))
  data = xts(data[,c("open","high","low","close","volume","adj.")],
             order.by = as.Date(data[,"Index"],format="%Y-%m-%d"))
  lastHistoricalDate = index(data[nrow(data),])

  recent = getSymbols(Symbols = substr(ii,1,nchar(ii)-4), 
                      src = "yahoo", 
                      from = startDate, 
                      auto.assign = FALSE)
  colnames(recent) = c("open","high","low","close","volume","adj.")

  pos = match(as.Date(lastHistoricalDate,format="%Y-%m-%d"),index(recent))

  if (!is.na(pos)){ 
    if (pos == nrow(recent))
      print("File already up-to-date")

    if (pos < nrow(recent)){
      dt = NULL
      dt = rbind(data,recent[(pos+1):nrow(recent),])
      write.zoo(dt,paste(thePath,ii,sep=""),sep=",",row.names=FALSE) 
    }
  }

  if (is.na(pos))
    print("Error: dates do not match")

ERROR in line no. 7

for (ii in theFiles){

$\endgroup$
  • $\begingroup$ doesn't solve your current issue but have you tried Quandl for this data? I do NOT work for or have any financial interactions with Quandl as a company but do use the API for a few projects and have used it commercially in the past and it seems to be the best of the APIs. It also gives you access to a wide range of other financial and economic data; I'm currently writing code to calculate trade weighted exchange rates using it. It's R API is good (and in CRAN) but the codes to choose what to download aren't intuitive. $\endgroup$ – MD-Tech May 18 '16 at 16:51
  • $\begingroup$ Thanks for the comment, given information is very useful for me, I was not aware about Quandl. Any idea how i can download the weights of any index components (NIFTY50). I need those weights everyday. Please help. $\endgroup$ – Atul Agarawal May 18 '16 at 22:07
  • $\begingroup$ depending on how the index is weighted, which you can find out from their website or investopedia, you can calculate the weightings so you don't need to download them. Many indices change weighting and composition infrequently, however. $\endgroup$ – MD-Tech May 19 '16 at 8:03
  • $\begingroup$ Thanks, I am new to R...Are you there on gmail or any instant Chat, If you don't mind will you be able to chat with me..I have few question on R and Portfolio Optimization. $\endgroup$ – Atul Agarawal May 19 '16 at 8:05
  • $\begingroup$ I'm in work currently so only available intermittently, check out the R questions both here and on stack overflow for more on R. I am not even close to being an estimate on portfolio optimization, however, and would be more of a liability than a help on that score. I mostly work with risk and trading compliance. $\endgroup$ – MD-Tech May 19 '16 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.