#Importar los datos El activo es TSLA (Tesla)
tsla_data <- getSymbols("TSLA", from = "2021-03-16",
to = "2024-03-16",
auto.assign = FALSE)
TBill_data <- getSymbols("TLT", from = "2021-03-16",
to = "2024-03-16",
auto.assign = FALSE)
ret_tsla <- Delt(tsla_data$TSLA.Adjusted)
ret_tbill <- Delt(TBill_data$TLT.Adjusted)
rets <- cbind(ret_tsla, ret_tbill)
names(rets) <- c("TSLA", "TBILL")
head(rets)
rets[1, ] <- c(0, 0)
gross_ret <- 1 + rets
cum_rets <- cumprod(gross_ret)
head(cum_rets)
plot(cum_rets$TSLA,
main = "Value of 1 USD invested")
lines(cum_rets$TBILL, col = "blue", lwd = 3)
tail(cum_rets, 2)
tail((cum_rets - 1) * 100, 2)
head(cum_rets)
plot(rets$TSLA, col = "red",
main = "Retornos TSLA y T-Bills")
lines(rets$TBILL, col = "pink", lwd = 5)
plot(rets$TBILL, col = "pink",
main = "Retornos TBILL")
head(rets)
returns <- rets[-1, ]
sd_tsla <- sd(returns$TSLA)
sd_tsla
sd_tbill <- sd(returns$TBILL)
sd_tbill
##Cálculo de la desviación estándar
typeof(index(returns))
str(index(returns))
##Desviación cada año- Total
sd_2021 <- apply(returns["2021"], 2, sd)
sd_2021
sd_2022 <- apply(returns["2022"], 2, sd)
sd_2022
sd_2023 <- apply(returns["2023"], 2, sd)
sd_2023
sd_2024 <- apply(returns["2024"], 2, sd)
sd_2024
##atos de desviación diarios
sd_todo <- rbind(sd_2021, sd_2022, sd_2023, sd_2024)
head(sd_todo)
sd_anual <- sqrt(252) * sd_todo
sd_anual
barplot(t(sd_anual), beside = TRUE,
col = c("blue", "red"),
main = "Desv. Estandar anualizada TSLA y T-Bill",
legend.text = c("TSLA", "T-Bill"))
##Histogramas
hist(returns$TSLA, breaks = 50)
hist(returns$TBILL, breaks = 5)