## R Markdown
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(zoo) library(xts) library(quantmod)
data_apple = getSymbols(“AAPL”, from = “2014-12-31”, to = “2020-01-01”, auto.assign = FALSE) data_tbill = getSymbols(“SHY”, from = “2014-12-31”, to = “2020-01-01”, auto.assign = FALSE)
ret_apple = deltat(data_apple\(AAPL.Adjusted) ret_tbill = Delt(data_tbill\)SHY.Adjusted)
rets = cbind(ret_apple, ret_tbill)
head(rets, 2) names(rets) = c(“Apple”, “TBill”) head(rets, 6)
rets[1, ] = c(0, 0) gross_ret = 1 + rets head(rets, 3) head(gross_ret, 3)
cum_rets = cumprod(gross_ret) head(cum_rets) tail(cum_rets)
plot(cum_rets[, “Apple”], col = “blue”, ylim = c(0, 3), main = “Retornos acumulados de Apple y T-Bills”) lines(cum_rets[, “TBill”], col = “red”, lwd = 3)
returns = rets[-1, ]
sd_apple = sd(returns[, “Apple”]) sd_apple
sd_tbill = sd(returns[, “TBill”]) sd_tbill
sd_apple / sd_tbill
hist(returns[, “Apple”], breaks = 50) sd_apple * 3
pnorm(1) - pnorm(-1) pnorm(2) - pnorm(-2)
sd_2015 = apply(returns[“2015”, , drop = FALSE], MARGIN = 2, FUN = sd) sd_2015
sd_2016 = apply(returns[“2016”, , drop = FALSE], MARGIN = 2, FUN = sd) sd_2016
sd_2017 = apply(returns[“2017”, , drop = FALSE], MARGIN = 2, FUN = sd) sd_2017
sd_all = rbind(sd_2015, sd_2016, sd_2017) sd_all
nrow(returns[“2019”])
sd_all_annual = sqrt(252) * sd_all
barplot(t(sd_all_annual), beside = TRUE, col = c(“blue”, “red”), main = “Desv Est. Anualizada”, legend.text = c(“Apple”, “T-bills”))
install.packages(“rmarkdown”)
options(repos = c(CRAN = “https://cloud.r-project.org”)) install.packages(“rmarkdown”)
install.packages(“rmarkdown”) contrib.url(repos, “Riesgos financieros/”)
options(repos = c(CRAN = “https://cloud.r-project.org”)) install.packages(“rmarkdown”)
rmarkdown::render("RMarkdown-Sebastian-Avellaneda.spin.Rmd")
```
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.