library(quantmod) library(PerformanceAnalytics) library(magrittr) library('lubridate') library(dplyr) ticker = c("QQQ", "DIA", "SPY", "TLT", "VNQ", "IAU", "VWO") getSymbols(ticker, from= today()-90, to = today(),warnings = FALSE, auto.assign = TRUE, src="yahoo") prices = do.call(cbind, lapply(ticker, function(x) Ad(get(x)))) rets = Return.calculate(prices) %>% na.omit() #head(rets) #head(prices) lambda <- 0.94 result <- data.frame() for( n in 1:ncol(prices)) { tmp1 <- data.frame(price=prices[,n]) %>% mutate(seq=row_number()) colnames(tmp1) <- c("price", "seq") tmp1$ewma <- NA tmp1$diff <- NA for(i in tmp1$seq){ if (i > 1){ ewma <- lambda*tmp1[i,]$price +(1-lambda)*ewma end <- tmp1[i,]$price tmp1[i,]$diff <- abs(end - yesterday_price) } else{ ewma <- tmp1[i,]$price yesterday_price <- tmp1[i,]$price start <- tmp1[i,]$price } tmp1$ewma[i] <- ewma } change <- end - start volatility <- sum(na.omit(tmp1$diff)) efficiency_ratio <- change / volatility result <- rbind(data.frame(ticker = ticker[n], efficiency_ratio), result) } arrange(result, desc(efficiency_ratio)) charts.PerformanceSummary(rets)