#title UPRO-TMF EWMA 역변동성 전략

작성 중...
작성 중...
작성 중...
작성 중...

t0은 그냥 그 시점의 값
EWMA(t) = a * x(t) + (1-a) * EWMA(t-1)


{{{#!html
<!-- 페이지에 아래를 포함시킨 후 latex을 사용하면 된다. -->
<script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}
      });
</script>
<script src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
}}}

==== EWMA Volatility ====
 * {{{EWMA: Exponentially Weighted Moving Average}}}
 * λ = 0.94 --> JP모건에서 일별 람다는 이렇게 하라고 한다. 열심히 계산해 봤겠지 뭐.

{{{#!html
<div style="width:0; float:left;">
$$ \sigma_t^2 = (1-\lambda)\cdot\gamma_t-1^2 + \lambda\cdot\sigma_t-1^2 $$
$$ where, $$
$$ \ \ \sigma : 표준편차 $$
$$ \ \ \lambda: 가중치 $$
$$ \ \ \gamma : 수익률 $$
$$ \ \ t : 시점 $$
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
}}}

==== 구현 ====
{{{
#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)))


}}}