2025-03-15

Libraries used for the app

  • shiny: Used to create the interactive web application, allowing users to select stock indices and display dynamic charts.
  • plotly: Generates interactive and visually appealing financial charts, enabling zooming, panning, and hover functionality.
  • quantmod: Fetches real-time and historical stock market data from Yahoo Finance, essential for financial analysis.
  • RColorBrewer: Provides a predefined set of visually appealing color palettes, ensuring distinct colors for different stock indices.

quantmod and RColorBrewer

Extract data from Yahoo Finance with quantmod

stockData <- reactive({
  tmp <- getSymbols(input$symbol, src = "yahoo", auto.assign = FALSE)
  colnames(tmp) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
  tmp
})

Define color palette with RColorBrewer

colors <- brewer.pal(n = length(symbols), name = "Dark2")
color_map <- setNames(colors, symbols)

plotly

Generation of the interactive graph with plotly

plot_ly(
  data_df,
  x = ~Date,
  y = ~Close,
  type = "scatter",
  mode = "lines",
  line = list(color = color_map[[input$symbol]], width = 2) # color palette RColorBrewer
) %>%
  layout(
    title = list(text = paste("  ", "  "), x = 0.05),
    xaxis = list(rangeslider = list(visible = TRUE))
  )

plotly

  • rangeselector: Adds buttons to quickly change the time horizon (6 months, 1 year, 5 years, or the entire period).
  • rangeslider: Activates the slider bar that allows the user to manually adjust the date range.

plotly

layout(
  xaxis = list(
    title = "Date",
    showgrid = TRUE,
    gridcolor = "#e5e5e5",
    gridwidth = 1,
    zeroline = FALSE,
    linecolor = "black",
    tickformat = "%b %Y",
    rangeselector = list( 
      buttons = list(
        list(count = 6, label = "6m", step = "month", stepmode = "backward"),
        list(count = 1, label = "1y", step = "year", stepmode = "backward"),
        list(count = 5, label = "5y", step = "year", stepmode = "backward"),
        list(step = "all", label = "All Time")
      )
    ), rangeslider = list(visible = TRUE)))

App display