-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp_range.R
More file actions
100 lines (73 loc) · 2.58 KB
/
Copy pathapp_range.R
File metadata and controls
100 lines (73 loc) · 2.58 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
##############################
# This is a shiny app for calculating the average rescaled
# range of daily prices for a pair of stocks, as a function
# of the beta weight parameter.
## Below is the setup code that runs once when the shiny app is started
# Load R packages
library(HighFreq)
library(shiny)
library(dygraphs)
## Model and data setup
# Load the intraday minute prices
load(paste0("/Users/jerzy/Develop/data/SPY_minute_202425.RData"))
pricetarg <- pricel
load(paste0("/Users/jerzy/Develop/data/XLK_minute_202425.RData"))
priceref <- pricel
priceref <- do.call(rbind, priceref)
pricetarg <- do.call(rbind, pricetarg)
# Calculate the symbol names
symboltarg <- rutils::get_name(colnames(pricetarg))
symbolref <- rutils::get_name(colnames(priceref))
symbolpair <- paste0(symboltarg, "/", symbolref)
captiont <- paste0("Range For ", symbolpair)
## End setup code
## Create elements of the user interface
uifun <- shiny::fluidPage(
titlePanel(captiont),
fluidRow(
# Input the beta parameter
column(width=2, sliderInput("betac", label="beta:", min=0.0, max=3.0, value=2.0, step=0.1)),
), # end fluidRow
# Create output plot panel
dygraphs::dygraphOutput("dyplot", width="90%", height="600px")
) # end fluidPage interface
## Define the server code
servfun <- function(input, output) {
# Create an empty list of reactive values.
values <- reactiveValues()
# Recalculate the range
pricev <- shiny::reactive({
cat("Recalculating the range for", symbolpair, "\n")
# Calculate the pair prices
pricev <- pricetarg - input$betac*priceref
colnames(pricev) <- "Pair"
# Calculate the range of daily prices for VTI
ohlc <- xts::to.daily(pricev)
openp <- quantmod::Op(ohlc)
closep <- quantmod::Cl(ohlc)
retd <- (closep - openp)
colnames(retd) <- "daytime"
highp <- quantmod::Hi(ohlc)
lowp <- quantmod::Lo(ohlc)
hilo <- highp - lowp
# Actual value of the rescaled range is:
rangev <- mean(hilo)/sd(retd)
values$rangev <- round(rangev, 3)
# cat("range =", rangev, "\n")
pricev
}) # end Recalculate the range
# Plot the pair
output$dyplot <- dygraphs::renderDygraph({
# Get the PnLs
pricev <- pricev()
colnamev <- colnames(pricev)
# Get the range
rangev <- values$rangev
# cat("range =", rangev, "\n")
captiont <- paste0(captiont, " / range = ", rangev)
# Return to the output argument a dygraph plot
dygraphs::dygraph(pricev, main=captiont)
}) # end output plot
} # end server code
## Return a Shiny app object
shiny::shinyApp(ui=uifun, server=servfun)