-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp_ema_quantmod.Rmd
executable file
·54 lines (46 loc) · 1.42 KB
/
app_ema_quantmod.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
---
title: "EWMA prices"
author_no_print: "Jerzy Pawlowski"
affiliation: NYU Tandon School of Engineering
date_no_print: '`r format(Sys.time(), "%m/%d/%Y")`'
email: jp3900@nyu.edu
output: html_document
runtime: shiny
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
library(HighFreq) # load package HighFreq
# select OHLC data
ohlc <- rutils::etfenv$VTI["/2011"]
# calculate close prices
closep <- Cl(ohlc)
# define lookback window and decay parameter
look_back <- 51
plot_theme <- chart_theme()
plot_theme$col$line.col <- c("orange", "blue")
# source("/Users/jerzy/Develop/R/scripts/ewma_model.R")
```
```{r ewma_model, echo=FALSE}
inputPanel(
sliderInput("lambda", label="lambda:",
min=0.01, max=0.2, value=0.1, step=0.01)
) # end inputPanel
renderPlot({
lambda <- input$lambda
# calculate EWMA prices
weightv <- exp(-lambda*1:look_back)
weightv <- weightv/sum(weightv)
ewmap <- filter(closep, filter=weightv, sides=1)
ewmap <- as.numeric(ewmap)
ewmap[1:(look_back-1)] <- ewmap[look_back]
ewmap <- xts(cbind(closep, ewmap), order.by=index(ohlc))
colnames(ewmap) <- c("VTI", "VTI EWMA")
# plot EWMA prices
# x11(width=12, height=9)
chobj <- chart_Series(ewmap, theme=plot_theme, name="EWMA prices")
plot(chobj)
legend("topleft", legend=colnames(ewmap),
inset=0.1, bg="white", lty=1, lwd=6,
col=plot_theme$col$line.col, bty="n")
}) # end renderPlot
```