-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp_pair_stockETF.R
More file actions
executable file
·126 lines (96 loc) · 3.78 KB
/
Copy pathapp_pair_stockETF.R
File metadata and controls
executable file
·126 lines (96 loc) · 3.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
##############################
# This is a shiny app which plots the daily, daytime, and
# overnight returns of a stock pair with a fixed beta.
#
# Just press the "Run App" button on upper right of this panel.
##############################
## Below is the setup code that runs only once when the shiny app is started
## Load packages
library(HighFreq)
library(shiny)
library(dygraphs)
## Load daily S&P500 stock prices and returns
# load(file="/Users/jerzy/Develop/lecture_slides/data/sp500_prices.RData")
# load(file="/Users/jerzy/Develop/lecture_slides/data/sp500_returns.RData")
# symetfs <- sort(colnames(pricestock))
# symstock <- "AAPL"
#
# symbolsetf <- colnames(rutils::etfenv$prices)
# symetf <- "XLK"
## Load the S&P500 OHLC stock prices
if (!("sp500env" %in% ls()))
load("/Users/jerzy/Develop/lecture_slides/data/sp500.RData")
symstocks <- names(sp500env)
symetfs <- rutils::etfenv$symbolv
symstock <- "AAPL"
symetf <- "XLK"
# captiont <- paste("Stat-arb Portfolio Strategy app_statarb_strat.R")
## End setup code
## Create elements of the user interface
uifun <- shiny::fluidPage(
titlePanel("Returns of a Stock Pair With Fixed Beta"),
# create single row with four slider inputs
fluidRow(
# Input stock symbol
column(width=1, selectInput("symstock", label="Stock", choices=symstocks, selected=symstock)),
# Input ETF symbol
column(width=1, selectInput("symetf", label="ETF", choices=symetfs, selected=symetf)),
# Input beta parameter
column(width=2, sliderInput("betac", label="beta:", min=0.0, max=2.0, value=1.0, step=0.1)),
), # end fluidRow
# create output plot panel
dygraphs::dygraphOutput("dyplot", width="90%", height="600px")
# mainPanel(dygraphs::dygraphOutput("dyplot"), width=12)
) # end fluidPage interface
## Define the server code
servfun <- shiny::shinyServer(function(input, output) {
# Create an empty list of reactive values.
values <- reactiveValues()
# Rerun the strategy
retv <- shiny::reactive({
cat("Recalculating strategy", "\n")
# Get model parameters from input argument
symstock <- input$symstock
symetf <- input$symetf
# lagg <- input$lagg
# coeff <- as.numeric(input$coeff)
# Get the stock prices for symstock
ohlc <- get(symstock, sp500env)
openp <- log(quantmod::Op(ohlc))
closep <- log(quantmod::Cl(ohlc))
ret1 <- rutils::diffit(closep) # Daily returns
retd1 <- (closep - openp) # Daytime returns
colnames(retd1) <- "daytime1"
reton1 <- (openp - rutils::lagit(closep, lagg=1, pad_zeros=FALSE)) # Overnight returns
colnames(reton1) <- "overnight1"
# Get the stock prices for symetf
ohlc <- get(symetf, rutils::etfenv)
openp <- log(quantmod::Op(ohlc))
closep <- log(quantmod::Cl(ohlc))
ret2 <- rutils::diffit(closep) # Daily returns
retd2 <- (closep - openp) # Daytime returns
colnames(retd2) <- "daytime2"
reton2 <- (openp - rutils::lagit(closep, lagg=1, pad_zeros=FALSE)) # Overnight returns
colnames(reton2) <- "overnight2"
# retv <- na.omit(reton1 - input$betac*reton2)
# retv <- retd1 - input$betac*retd2
retv <- ret1 - input$betac*ret2
colnames(retv) <- "pair"
sharper <- sqrt(252)*mean(retv)/sd(retv[retv<0])
values$sharper <- round(sharper, 3)
retv
}) # end reactive code
# Return the dygraph plot to output argument
output$dyplot <- dygraphs::renderDygraph({
# symstock <- "XLK"
# symetf <- "VTI"
retv <- retv()
colnamev <- colnames(retv)
captiont <- paste("Sharpe =", values$sharper)
dygraphs::dygraph(cumsum(retv), main=captiont) %>%
dyOptions(colors="blue", strokeWidth=2) %>%
dyLegend(show="never", width=300)
}) # end output plot
}) # end server code
## Return a Shiny app object
shiny::shinyApp(ui=uifun, server=servfun)