Descargando los precios de las acciones que componen el índice Standard & Poor’s 500.

Ra <-  tq_get(sp500$symbol ,get  = "stock.prices",
           from = "2020-01-01",
           to   = "2023-12-31")

Precios <- Ra %>% 
  select(symbol, adjusted, date)

Diagrama de dispersión del VaR de las acciones que componen el índice Standard & Poor’s 500.

riesgo <- Precios %>% 
  group_by(symbol) %>%
   tq_transmute(adjusted, periodReturn,col_rename = "Rb") %>%
    tq_performance(Ra = Rb, performance_fun = VaR) 


  plot.i <- ggplot(riesgo,aes(x = symbol,y = VaR, fill= symbol)) + 
  geom_point(colour = "cornflowerblue") + xlab("symbol") +
  ylab("Valor en Riesgo") +
  theme_update(plot.title = element_text(hjust = 0.5)) +
  ggtitle("Diagrama de puntos") 
  
  ggplotly(plot.i)

Distribución del VaR del índice Standard & Poor’s 500.

his <- ggplot(riesgo,aes(x = VaR, fill= symbol)) + 
  geom_histogram(bins = 30) + xlab("Valor en Riesgo") +
  theme_update(plot.title = element_text(hjust = 0.5)) +
  ggtitle("Histograma") 

ggplotly(his)