-
Notifications
You must be signed in to change notification settings - Fork 44
/
app.R
393 lines (331 loc) · 11.8 KB
/
app.R
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# import all necessary packages
library(shiny)
library(xts) # for xts objects
library(quantmod) # for the Yahoo finance imports
library(zoo) # for date rangling
library(TTR) # for stock analytic functions
library(htmlwidgets) # self-explanatory
library(data.table) # speeding up some matrix manipulation
library(rsconnect) # for publishing
library(plotly) # viz
library(plyr) # best practice to load plyr first and then dplyr
library(dplyr) # for pipes
library(tidyr) # for certain data transformation
library(shinythemes) # dashboard aesthetics
library(PerformanceAnalytics) # analytic functions
library(DT) # speeding up some matrix manipulation
library(formattable) # for fancy tables
library(shinydashboard) # self-explanatory
library(ggplot2) # for viz
library(reshape2) # data manipulaiton
library(scales) # time / date / axes scales
library(lubridate) # date manipulation
# source the Functions.R file, where all main functions are stored
source("Functions.R")
####################################
############## UI ##############
####################################
ui <- dashboardPage(
skin="blue",
dashboardHeader(
title="Asset Comparison Toolbox",
titleWidth = 300
),
dashboardSidebar(
width = 300,
sidebarMenu(
menuItem(
"Walkthrough of how the app works",
href="https://github.com/pmaji/crypto-asset-comparison-tool/blob/master/README.md",
# https://rstudio.github.io/shinydashboard/appearance.html#icons for more icons
icon=icon("github")
),
menuItem(
"Report a bug or make a request",
href="https://github.com/pmaji/crypto-asset-comparison-tool/issues",
icon=icon("comment")
),
menuItem(
"Examine the code behind the app",
href="https://github.com/pmaji/crypto-asset-comparison-tool",
icon=icon("code")
),
hr(),
div(
style="text-align:center",
"Use lowercase to input crypto assets.",
br(),
"Use uppercase for all others."
),
br(),
# UI options to pick the 1st asset to compare:
selectInput(inputId = "asset_1a",
label = "Select 1st asset of interest:",
choices = symbol_list,
selected = "eth",
multiple = FALSE,
selectize = TRUE),
# UI options to pick the 2nd asset to compare:
selectInput(inputId = "asset_2a",
label = "Select 2nd asset of interest:",
choices = symbol_list,
selected = "AMZN",
multiple = FALSE,
selectize = TRUE),
hr(),
div(
style="text-align:center",
"1st date = date of initial investment",
br(),
"2nd date = last date in time series examined"
),
dateRangeInput("port_dates1a",
label = "Choose date range of interest:",
# default date shows past half year
start = Sys.Date()-183,
end = Sys.Date()-3)
)
),
dashboardBody(
# 1st row of boxes
fluidRow(
box(
title="Portfolio Performance Chart",
# status sets color (primary, success, info, warning, danger)
status="primary",
solidHeader = TRUE,
plotlyOutput(outputId = "portfolio_perf_chart"),
height=500,
width=8
),
box(
title = "Portfolio Performance Inputs",
status= "primary",
solidHeader = TRUE,
h5("This box focuses on portfolio value, i.e., how much an initial investment of the amount specified below (in USD) would be worth over time, given price fluctuations."),
textInput(
inputId = "initial_investment",
label = "Enter your initial investment amount ($):",
value = "1000"),
hr(),
h5("The slider below modifies the", a(href = "https://stats.stackexchange.com/questions/2002/how-do-i-decide-what-span-to-use-in-loess-regression-in-r", "smoothing parameter"), "used in the", a(href = "https://en.wikipedia.org/wiki/Local_regression", "LOESS function"), "that produces the lines on the scatterplot."),
sliderInput(
inputId = "port_loess_param",
label = "Smoothing parameter for portfolio chart:",
min = 0.1,
max = 2,
value = .33,
step = 0.01,
animate = FALSE
),
hr(),
h5("The table below provides metrics by which we can compare the portfolios. For each column, the asset that performed best by that metric is colored green."),
height = 500,
width = 4
)
),
# 2nd row of boxes
fluidRow(
box(
title="Portfolio Performance Summary Table",
status="primary",
solidHeader = TRUE,
formattableOutput("port_summary_table"),
height = 175,
width = 12
)
),
# 3rd row of boxes
fluidRow(
box(
title="Investment Returns Chart",
status="success",
solidHeader = TRUE,
plotlyOutput("asset_returns_chart"),
height=500,
width=8
),
box(
title = "Investment Returns Inputs",
status= "success",
solidHeader = TRUE,
h5("This box focuses on", a(href = "https://www.investopedia.com/terms/r/rateofreturn.asp", "rate of return"), "calculated at the level of detail specified below."),
selectInput(
"period",
label = "Period over which to calculate returns:",
choices = c("daily", "weekly", "monthly", "quaterly", "yearly"),
selected = "weekly"
),
hr(),
h5("The default smoothing parameter for the returns chart is slightly higher than the default for the portfolio performance chart given that returns data are generally aggregated."),
br(),
sliderInput(
inputId = "asset_loess_param",
label = "Smoothing parameter for returns chart:",
min = 0.1,
max = 2,
value = .75,
step = 0.01,
animate = FALSE),
br(),
height = 500,
width = 4
)
),
# 4th row of boxes
fluidRow(
box(
title="Variance-Adjusted Returns Chart",
status="warning",
solidHeader = TRUE,
plotlyOutput("portfolio_sharpe_chart"),
height=500,
width=8
),
box(
title = "Variance-Adjusted Returns Inputs",
status= "warning",
solidHeader = TRUE,
h5("This box focuses more specifically on the average rate of return in excess of the", a(href = "https://www.investopedia.com/terms/r/risk-freerate.asp", "risk free rate"), "per unit of volatility, as captured by variations of the", a(href = "https://en.wikipedia.org/wiki/Sharpe_ratio", "Sharpe Ratio."), "Be sure your chosen risk free rate matches up with your selected time period. Default is 30 basis points for the weekly risk free rate."),
br(),
br(),
# risk free rates at link below; for monthly risk-free rate use 1 month treasury
# https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Pages/TextView.aspx?data=yield
sliderInput(
inputId = "Rf",
label = "Choose risk free rate (as decimal):",
min = -0.10,
max = 0.10,
value = (0.01/4),
step = 0.001,
animate = FALSE
),
hr(),
h5("The confidence level chosen is used in the", a(href = "https://cran.r-project.org/web/packages/SharpeR/vignettes/SharpeRatio.pdf", "Sharpe Ratio calculations.")),
sliderInput(
inputId = "p",
label = "Choose confidence level (as decimal):",
min = 0.1,
max = 0.99,
value = 0.95,
step = 0.01,
animate = FALSE
),
br(),
height = 500,
width = 4
)
)
)
)
####################################
############ SERVER ############
####################################
server <- function(input, output, session) {
# utility functions to be used within the server; this enables us to use a textinput for our portfolio values
exists_as_number <- function(item) {
!is.null(item) && !is.na(item) && is.numeric(item)
}
# data-creation reactives (i.e. everything that doesn't directly feed an output)
# first is the main data pull which will fire whenever the primary inputs (asset_1a, asset_2a, initial_investment, or port_dates1a change)
react_base_data <- reactive({
if (exists_as_number(as.numeric(input$initial_investment)) == TRUE) {
# creates the dataset to feed the viz
return(
get_pair_data(
asset_1 = input$asset_1a,
asset_2 = input$asset_2a,
port_start_date = input$port_dates1a[1],
port_end_date = input$port_dates1a[2],
initial_investment = (as.numeric(input$initial_investment))
)
)
} else {
return(
get_pair_data(
asset_1 = input$asset_1a,
asset_2 = input$asset_2a,
port_start_date = input$port_dates1a[1],
port_end_date = input$port_dates1a[2],
initial_investment = (0)
)
)
}
})
react_port_summary_table <- reactive({
# reshapes the data for a summary view
return(
build_summary_table(portfolio_data = react_base_data())
)
})
react_asset_returns_list <- reactive({
return(
get_portfolio_returns(
portfolio_data = react_base_data(),
period = input$period
)
)
})
simple_formatter <- function(){
formatter("span",
style = x ~ style(
display = "inline-block",
direction = "rtl",
"border-radius" = "4px",
"padding-right" = "2px",
"background-color" = csscolor("darkslategray"),
width = percent(proportion(x)),
color = csscolor(gradient(x, "red", "green"))
))
}
react_formattable <- reactive({
return(
formattable(react_port_summary_table(),
list(
"Asset Portfolio Max Worth" = simple_formatter(),
"Asset Portfolio Latest Worth" = simple_formatter(),
"Asset Portfolio Absolute Profit" = simple_formatter(),
"Asset Portfolio Rate of Return" = simple_formatter()
)
)
)
})
# Now the reactives for the actual vizualizations
output$portfolio_perf_chart <-
debounce(
renderPlotly({
data <- react_base_data()
build_portfolio_perf_chart(data, port_loess_param = input$port_loess_param)
}),
millis = 2000) # sets wait time for debounce
output$port_summary_table <-
debounce(
renderFormattable({
# css color names taken from http://www.crockford.com/wrrrld/color.html
react_formattable()
}), millis = 2000) # sets wait time for debounce
output$portfolio_sharpe_chart <-
debounce(
renderPlotly(
{
# building the sharpe ratio viz
get_sharpe_ratio_plot(
asset_returns_list = react_asset_returns_list(),
Rf = input$Rf,
p = input$p
)
}
), millis = 2000)
output$asset_returns_chart <-
debounce(
renderPlotly(
{
# building the retrns ratio viz
build_asset_returns_plot(
asset_returns_list = react_asset_returns_list(),
asset_loess_param = input$asset_loess_param)
}
), millis = 2000)
}
# Run the application
shinyApp(ui = ui, server = server)