Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##########################################
- ## List of securities (Yahoo tickers)
- ## thertrader@gmail.com - Nov. 2015
- ##########################################
- theInstruments = c("^GSPC",
- "SPY",
- "QQQ",
- "DDM",
- "EFA",
- "EEM",
- "EWJ")
- ##########################################
- ## Daily prices from Yahoo
- ## thertrader@gmail.com - Nov. 2015
- ##########################################
- library(quantmod)
- startDate = "2000-01-01"
- thePath = "D:\\daily\\data\\"
- source(paste(thePath,"code\\listOfInstruments.r",sep=""))
- for (ii in theInstruments){
- print(ii)
- data = getSymbols(Symbols = ii,
- src = "yahoo",
- from = startDate,
- auto.assign = FALSE)
- colnames(data) = c("open","high","low","close","volume","adj.")
- write.zoo(data,paste(thePath,ii,".csv",sep=""),sep=",",row.names=FALSE)
- }
- ##########################################
- ## Update data files
- ## thertrader@gmail.com - Nov. 2015
- ##########################################
- library(quantmod)
- lookback = 60
- startDate = Sys.Date() - lookback
- thePath = "D:\\daily\\data\\"
- 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 = statDate,
- 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")
- Create a batch file (updateDailyPrices.bat)
- ##########################################
- ## Another important part of the job is creating a batch file that automates the updating process above
- ## (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
- ## (the path has to be amended with the reader’s setup). Note that I added an output file (updateLog.txt) to track the execution.
- ##########################################
- cd ../..
- 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