library(tseries) library(timeDate) library(timeSeries) library(zoo) library(xts) library(its) library(PerformanceAnalytics) library(quantmod) library(gridExtra) findMarketDate = function( date ) { while(! isBizday(x = as.timeDate(date), holidays=holidayNYSE(as.numeric(format(date, "%Y"))))) { date = date - 1 } return(date) } symbolInRange = function(sym, startDate, endDate, period="w") { print(sym) okSym = NULL symClose.z = NULL try( (symClose.z = get.hist.quote(instrument=sym, start=startDate, end=endDate, quote="AdjClose", provider="yahoo", compression=period, retclass="zoo", quiet=T)), silent = TRUE ) if (! is.null(symClose.z)) { adjEnd = as.Date(endDate) - 7 dates = index(symClose.z) if ((min(dates) <= as.Date(startDate)) && (max(dates) >= adjEnd)) { okSym = sym } } return( okSym ) } # http://finance.yahoo.com/d/quotes.csv?s=AAPL+GOOG+MSFT&f=sn # # For a key to the query components, see # # http://www.codeproject.com/Articles/37550/Stock-quote-and-chart-from-Yahoo-in-C # getCompanyNames = function(symlist, win = 8) { urlRoot = "http://finance.yahoo.com/d/quotes" query = "?s=" symName = "&f=sn&e=.csv" range = seq(from=win, to=length(symlist), by=win) last = ifelse(range[length(range)] == length(symlist), length(range) - 1, length(range)) start = c(1, range[1:last] + 1) end = c(range[1:last], length(symlist)) table = c() for (i in 1:length(start)) { symblock = symlist[start[i]:end[i]] andedBlock = capture.output(cat(symblock, sep="+")) url = capture.output(cat(urlRoot, query, andedBlock, symName, sep="")) data = read.csv(url, header=FALSE) table = rbind(table, data) } if (length(table) > 0) { colnames(table) = c("sym", "name") } return(table) } dataFileRoot = "/home/iank/Documents/etf_portfolios/portfolio_2014" etfDataFile = "etf_list_etf_dot_com.csv" etfDataPath = paste(dataFileRoot, etfDataFile, sep="/") etfRawData = read.csv(file=etfDataPath ) symbols = as.vector(etfRawData[,"Ticker"]) startDate.d = as.Date("2007-04-04") startDate.d = findMarketDate( startDate.d ) # endDate.d = startDate.d + 30 endDate.d = findMarketDate( Sys.Date() ) startDate = as.character( startDate.d ) endDate = as.character( endDate.d ) window = 20 range = seq(from=window, to=length(symbols), by=window) last = ifelse(range[length(range)] == length(symbols), length(range) - 1, length(range)) start = c(1, range[1:last] + 1) end = c(range[1:last], length(symbols)) symlist = c() for (i in 1:length(start)) { symBlock = symbols[start[i]:end[i]] okSyms = unlist(sapply( symBlock, FUN=symbolInRange, startDate=startDate, endDate=endDate)) symlist = c(symlist, okSyms) } etfListFile = "etf_list.txt" etfListPath = paste(dataFileRoot, etfListFile, sep="/") if (length(symbols) > 0) { write.table(symbols, file=etfListPath) } etfSymbolFile = "etf_symbols.csv" etfDataPath = paste(dataFileRoot, etfSymbolFile, sep="/") if (length(symlist) > 10) { ix = which(symbols %in% symlist) names = as.vector(etfRawData[ix,"Fund.Name"]) etfData = cbind(symlist, names) colnames(etfData) = c("sym", "name") write.csv(etfData, file=etfDataPath, row.names=F ) } else { cat("There few, if any, symbols in the filtered list") }