#install.packages("RiskPortfolios") library(tidyquant) library(MTS) library(scales) library(qpcR) library(TTR) options("getSymbols.warning4.0"=FALSE) options("getSymbols.yahoo.warning"=FALSE) # Downloading Apple price using quantmod ewma.func <- function(rets, lambda) { sig.p <- 0 sig.s <- vapply(rets, function(r) sig.p <<- sig.p*lambda + (r^2)*(1 - lambda), 0) return(sqrt(sig.s)) } cash <- 0.4 from_dt <- as.character(today()-20) to_dt <- today() tickers = c("UPRO", "TMF") getSymbols(tickers, from = from_dt, to = to_dt,warnings = FALSE, auto.assign = TRUE) #tail(UPRO) #tail(TMF) UPRO_sd <- sd(ewma.func(as.numeric(UPRO$UPRO.Close), 0.94)) TMF_sd <- sd(ewma.func(as.numeric(TMF$TMF.Close), 0.94)) p1 <- TMF_sd/(UPRO_sd+TMF_sd) p2 <- UPRO_sd/(UPRO_sd+TMF_sd) p1 <- round(p1*(1-cash),2) p2 <- round(p2*(1-cash),2) p3 <- 1 - (p1 + p2) print(paste("UPRO=",percent(p1),"TMF=",percent(p2), "蠍=",percent(p3)))