|
| 1 | +library(dplyr) |
| 2 | +library(dygraphs) |
| 3 | +library(plotly) |
| 4 | +library(PerformanceAnalytics) |
| 5 | +library(shiny) |
| 6 | +library(shinydashboard) |
| 7 | +library(xts) |
| 8 | +library(zoo) |
| 9 | + |
| 10 | +returns <- readRDS("returns.rds") |
| 11 | +portfolio_choices <- c( |
| 12 | + "Conservative" = "conservative_portfolio_returns", |
| 13 | + "Balanced" = "balanced_portfolio_returns", |
| 14 | + "Aggressive" = "aggressive_portfolio_returns" |
| 15 | +) |
| 16 | + |
| 17 | +ui <- dashboardPage( |
| 18 | + dashboardHeader(title = "Portfolio Dashboard"), |
| 19 | + dashboardSidebar( |
| 20 | + selectInput( |
| 21 | + "portfolio", |
| 22 | + "Choose a portfolio", |
| 23 | + choices = portfolio_choices, |
| 24 | + selected = "balanced_portfolio_returns" |
| 25 | + ), |
| 26 | + dateInput( |
| 27 | + inputId = "date", |
| 28 | + label = "Starting Date", |
| 29 | + value = "2010-01-01", |
| 30 | + format = "yyyy-mm-dd" |
| 31 | + ), |
| 32 | + sliderInput("mar", "Min Acceptable Rate", min = 0, max = 0.1, value = 0.008, step = 0.001), |
| 33 | + numericInput("window", "Rolling Window", min = 6, max = 36, value = 12) |
| 34 | + ), |
| 35 | + dashboardBody( |
| 36 | + fluidRow( |
| 37 | + box(title = "Rolling Sortino", width = 12, |
| 38 | + plotlyOutput("time_series") |
| 39 | + ) |
| 40 | + ), |
| 41 | + fluidRow( |
| 42 | + box(title = "Scatterplot", width = 4, |
| 43 | + plotlyOutput("scatterplot", height = 250) |
| 44 | + ), |
| 45 | + box(title = "Histogram", width = 4, |
| 46 | + plotlyOutput("histogram", height = 250) |
| 47 | + ), |
| 48 | + box(title = "Density", width = 4, |
| 49 | + plotlyOutput("density", height = 250) |
| 50 | + ) |
| 51 | + ) |
| 52 | + ) |
| 53 | +) |
| 54 | + |
| 55 | +server <- function(input, output) { |
| 56 | + |
| 57 | + rate_limit_sec <- 2 |
| 58 | + |
| 59 | + portfolio_selected <- throttle(reactive({ |
| 60 | + req(input$portfolio, input$date) |
| 61 | + |
| 62 | + returns[[input$portfolio]] %>% |
| 63 | + as_tibble() %>% |
| 64 | + #collect() %>% |
| 65 | + mutate(date = as.Date(date)) %>% |
| 66 | + filter(date >= input$date) |
| 67 | + |
| 68 | + }), rate_limit_sec * 1000) |
| 69 | + |
| 70 | + rolling_sortino <- reactive({ |
| 71 | + req(input$mar) |
| 72 | + req(input$window) |
| 73 | + |
| 74 | + portfolio_selected()$returns %>% |
| 75 | + xts::xts(order.by = portfolio_selected()$date) %>% |
| 76 | + rollapply(input$window, function(x) SortinoRatio(x, MAR = input$mar)) %>% |
| 77 | + `colnames<-`("24-rolling") |
| 78 | + }) |
| 79 | + |
| 80 | + sortino_byhand <- reactive({ |
| 81 | + portfolio_selected() %>% |
| 82 | + mutate(ratio = mean(returns - input$mar) / sqrt(sum(pmin(returns - input$mar, 0)^2) / nrow(.))) %>% |
| 83 | + # Add two new columns to help with ggplot. |
| 84 | + mutate(status = ifelse(returns < input$mar, "down", "up")) |
| 85 | + }) |
| 86 | + |
| 87 | + output$time_series <- renderPlotly({ |
| 88 | + plot_ly() %>% |
| 89 | + add_lines(x = index(rolling_sortino()), y = as.numeric(rolling_sortino())) %>% |
| 90 | + layout( |
| 91 | + hovermode = "x", |
| 92 | + xaxis = list( |
| 93 | + rangeslider = list(visible = TRUE), |
| 94 | + rangeselector = list( |
| 95 | + x = 0, y = 1, xanchor = 'left', yanchor = "top", font = list(size = 9), |
| 96 | + buttons = list( |
| 97 | + list(count = 1, label = 'RESET', step = 'all'), |
| 98 | + list(count = 1, label = '1 YR', step = 'year', stepmode = 'backward'), |
| 99 | + list(count = 3, label = '3 MO', step = 'month', stepmode = 'backward'), |
| 100 | + list(count = 1, label = '1 MO', step = 'month', stepmode = 'backward') |
| 101 | + ) |
| 102 | + ) |
| 103 | + ) |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + output$scatterplot <- renderPlotly({ |
| 108 | + portfolio_scatter <- ggplot(sortino_byhand(), aes(x = date, y = returns, color = status) )+ |
| 109 | + geom_point() + |
| 110 | + geom_vline(xintercept = as.numeric(as.Date("2016-11-30")), color = "blue") + |
| 111 | + geom_hline(yintercept = input$mar, color = "purple", linetype = "dotted") + |
| 112 | + scale_color_manual(values = c("tomato", "chartreuse3")) + |
| 113 | + theme(legend.position = "none") + ylab("percent monthly returns") |
| 114 | + |
| 115 | + ggplotly(portfolio_scatter) %>% |
| 116 | + add_annotations( |
| 117 | + text = "Trump", x = as.numeric(as.Date("2016-11-30")), |
| 118 | + y = -.05, xshift = -10, textangle = -90, showarrow = FALSE |
| 119 | + ) |
| 120 | + }) |
| 121 | + |
| 122 | + output$histogram <- renderPlotly({ |
| 123 | + p <- ggplot(sortino_byhand(), aes(x = returns)) + |
| 124 | + geom_histogram(alpha = 0.25, binwidth = .01, fill = "cornflowerblue") + |
| 125 | + geom_vline(xintercept = input$mar, color = "green") |
| 126 | + ggplotly(p) %>% |
| 127 | + add_annotations(text = "MAR", x = input$mar, y = 10, xshift = 10, showarrow = FALSE, textangle = -90) |
| 128 | + }) |
| 129 | + |
| 130 | + output$density <- renderPlotly({ |
| 131 | + sortino_density_plot <- ggplot(sortino_byhand(), aes(x = returns)) + |
| 132 | + stat_density(geom = "line", size = 1, color = "cornflowerblue") |
| 133 | + |
| 134 | + shaded_area_data <- ggplot_build(sortino_density_plot)$data[[1]] %>% |
| 135 | + filter(x < input$mar) |
| 136 | + |
| 137 | + sortino_density_plot <- |
| 138 | + sortino_density_plot + |
| 139 | + geom_area(data = shaded_area_data, aes(x = x, y = y), fill = "pink", alpha = 0.5) + |
| 140 | + geom_segment( |
| 141 | + data = shaded_area_data, aes(x = input$mar, y = 0, xend = input$mar, yend = y), |
| 142 | + color = "red", linetype = "dotted" |
| 143 | + ) |
| 144 | + |
| 145 | + ggplotly(sortino_density_plot) %>% |
| 146 | + add_annotations( |
| 147 | + x = input$mar, y = 5, text = paste("MAR =", input$mar, sep = ""), textangle = -90 |
| 148 | + ) %>% |
| 149 | + add_annotations( |
| 150 | + x = (input$mar - .02), y = .1, text = "Downside", |
| 151 | + xshift = -20, yshift = 10, showarrow = FALSE |
| 152 | + ) |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +shinyApp(ui = ui, server = server) |
0 commit comments