Advertisement
Guest User

Untitled

a guest
Dec 28th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. ##########################################
  2. ## List of securities (Yahoo tickers)
  3. ## thertrader@gmail.com - Nov. 2015
  4. ##########################################
  5. theInstruments = c("^GSPC",
  6. "SPY",
  7. "QQQ",
  8. "DDM",
  9. "EFA",
  10. "EEM",
  11. "EWJ")
  12. ##########################################
  13. ## Daily prices from Yahoo
  14. ## thertrader@gmail.com - Nov. 2015
  15. ##########################################
  16. library(quantmod)
  17.  
  18. startDate = "2000-01-01"
  19. thePath = "D:\\daily\\data\\"
  20. source(paste(thePath,"code\\listOfInstruments.r",sep=""))
  21.  
  22. for (ii in theInstruments){
  23. print(ii)
  24. data = getSymbols(Symbols = ii,
  25. src = "yahoo",
  26. from = startDate,
  27. auto.assign = FALSE)
  28. colnames(data) = c("open","high","low","close","volume","adj.")
  29. write.zoo(data,paste(thePath,ii,".csv",sep=""),sep=",",row.names=FALSE)
  30. }
  31. ##########################################
  32. ## Update data files
  33. ## thertrader@gmail.com - Nov. 2015
  34. ##########################################
  35. library(quantmod)
  36.  
  37. lookback = 60
  38. startDate = Sys.Date() - lookback
  39. thePath = "D:\\daily\\data\\"
  40. theFiles = list.files(path=thePath,pattern=".csv")
  41.  
  42. for (ii in theFiles){
  43. data = read.csv(paste(thePath,ii,sep=""))
  44. data = xts(data[,c("open","high","low","close","volume","adj.")],
  45. order.by = as.Date(data[,"Index"],format="%Y-%m-%d"))
  46. lastHistoricalDate = index(data[nrow(data),])
  47.  
  48. recent = getSymbols(Symbols = substr(ii,1,nchar(ii)-4),
  49. src = "yahoo",
  50. from = statDate,
  51. auto.assign = FALSE)
  52. colnames(recent) = c("open","high","low","close","volume","adj.")
  53.  
  54. pos = match(as.Date(lastHistoricalDate,format="%Y-%m-%d"),index(recent))
  55.  
  56. if (!is.na(pos)){
  57. if (pos == nrow(recent))
  58. print("File already up-to-date")
  59.  
  60. if (pos < nrow(recent)){
  61. dt = NULL
  62. dt = rbind(data,recent[(pos+1):nrow(recent),])
  63. write.zoo(dt,paste(thePath,ii,sep=""),sep=",",row.names=FALSE)
  64. }
  65. }
  66.  
  67. if (is.na(pos))
  68. print("Error: dates do not match")
  69.  
  70. Create a batch file (updateDailyPrices.bat)
  71. ##########################################
  72. ## Another important part of the job is creating a batch file that automates the updating process above
  73. ## (I’m a Windows user). This avoids opening R/RStudio and run the code from there. The code below is placed on a .bat file
  74. ## (the path has to be amended with the reader’s setup). Note that I added an output file (updateLog.txt) to track the execution.
  75. ##########################################
  76.  
  77. cd ../..
  78. C:\progra~1\R\R-3.1.2\bin\R.exe CMD BATCH --vanilla --slave "D:\daily\d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement