library(quantmod)
## Warning: package 'quantmod' was built under R version 4.2.3
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 4.2.3
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.2.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
data_slb = getSymbols("SLB", from ="2017-12-31",
to = "2020-01-01",
auto.assign = F)
data_Tbill = getSymbols("BIL", from ="2017-12-31",
to = "2020-01-01",
auto.assign = F)
En este caso se traen los datos de TBill y Schlumberger Limited
head(data_slb)
## SLB.Open SLB.High SLB.Low SLB.Close SLB.Volume SLB.Adjusted
## 2018-01-02 68.07 69.83 67.60 69.52 8678200 58.11513
## 2018-01-03 69.97 71.64 69.13 71.33 9529200 59.62820
## 2018-01-04 71.76 73.19 71.26 73.10 12252300 61.10781
## 2018-01-05 72.95 73.68 72.25 73.60 8552700 61.52580
## 2018-01-08 73.35 74.92 72.71 74.80 9778900 62.52893
## 2018-01-09 74.78 75.30 74.56 74.90 9370300 62.61252
head(data_Tbill)
## BIL.Open BIL.High BIL.Low BIL.Close BIL.Volume BIL.Adjusted
## 2018-01-02 91.41 91.43 91.41 91.41 790000 81.80923
## 2018-01-03 91.43 91.43 91.42 91.43 218300 81.82709
## 2018-01-04 91.44 91.44 91.42 91.44 1098100 81.83607
## 2018-01-05 91.43 91.44 91.43 91.44 247600 81.83607
## 2018-01-08 91.43 91.45 91.43 91.45 199800 81.84500
## 2018-01-09 91.43 91.45 91.43 91.45 185900 81.84500
Se calculan los retornos con la diferencia porcentual
ret_Slb = Delt(data_slb$SLB.Adjusted)
ret_Tbill=Delt(data_Tbill$BIL.Adjusted)
rets = cbind(ret_Slb, ret_Tbill)
Se observa el comportamiento de los retornos
head(rets)
## Delt.1.arithmetic Delt.1.arithmetic.1
## 2018-01-02 NA NA
## 2018-01-03 0.026035652 0.0002183178
## 2018-01-04 0.024814003 0.0001098344
## 2018-01-05 0.006840170 0.0000000000
## 2018-01-08 0.016304186 0.0001090765
## 2018-01-09 0.001336906 0.0000000000
names(rets) = c("SLB", "TBILL")
head(rets,6)
## SLB TBILL
## 2018-01-02 NA NA
## 2018-01-03 0.026035652 0.0002183178
## 2018-01-04 0.024814003 0.0001098344
## 2018-01-05 0.006840170 0.0000000000
## 2018-01-08 0.016304186 0.0001090765
## 2018-01-09 0.001336906 0.0000000000
se le suma 1 para ver los retornos acumulados Donde los retornos acumulados son:
\[ 1 +r_{total}=\prod_{i=1}^T (1+r) \]
rets[1,] = c(0,0)
gross_rets= 1 + rets
head(gross_rets,3)
## SLB TBILL
## 2018-01-02 1.000000 1.000000
## 2018-01-03 1.026036 1.000218
## 2018-01-04 1.024814 1.000110
cum_rets = cumprod(gross_rets)
head(cum_rets)
## SLB TBILL
## 2018-01-02 1.000000 1.000000
## 2018-01-03 1.026036 1.000218
## 2018-01-04 1.051496 1.000328
## 2018-01-05 1.058688 1.000328
## 2018-01-08 1.075949 1.000437
## 2018-01-09 1.077388 1.000437
tail(cum_rets)
## SLB TBILL
## 2019-12-23 0.6408304 1.037705
## 2019-12-24 0.6386308 1.037818
## 2019-12-26 0.6295187 1.037932
## 2019-12-27 0.6284190 1.037818
## 2019-12-30 0.6347033 1.037932
## 2019-12-31 0.6315612 1.037932
plot(cum_rets)
Se puede ver los retornos de los activos:
plot(rets$SLB, col = "red",
main="retornos SLB y Tbills")
lines(rets$TBILL, col = "darkgreen", lwd=4)
y se observa la volatilidad de SLB y el ETF estable.
Se observan los retornos del activo mas volatil:
plot(cum_rets$SLB, col ="blue")
¿como medir el riesgo? Markowitz (1950) desviacion standar y la varianza muestral,donde:
$$
^2= _{i=1}^T(r_i - {r})^2
\[ \] sd= = $$ ###El activo con mas riesgo tiene una mayor desviacion standar.
returns=rets[-1,]
sd_slb=sd(returns$SPY)
sd_slb
## [1] NA
sd_bill = sd(returns$TBILL)
sd_bill
## [1] 0.0001182067
El activo mas reisgoso es SLB con una sd=0.01887704 , en cambio Tbill tiene una sd=0.0001182195
El riesgo cambia en el tiempo se tiene que anualizar
\[ \sqrt{T} \sigma_T \]
sd_2017 = apply(X =returns["2017"],MARGIN=2,FUN= sd)
sd_2017
## numeric(0)
sd_2018 = apply(X =returns["2018"],MARGIN=2,FUN= sd)
sd_2018
## SLB TBILL
## 0.0160610846 0.0001171382
sd_2019 = apply(X =returns["2019"],MARGIN=2,FUN= sd)
sd_2019
## SLB TBILL
## 0.0212166301 0.0001192197
sd_2020 = apply(X =returns["2020"],MARGIN=2,FUN= sd)
sd_2020
## numeric(0)
Combinar rows
sd_all = rbind(sd_2020, sd_2017
,sd_2018,sd_2019)
T= 252 dias de transaccion.
sd_all_anual = sqrt(252)*sd_all
sd_all_anual
## SLB TBILL
## sd_2018 0.2549618 0.001859512
## sd_2019 0.3368036 0.001892554
barplot(t(sd_all_anual),beside = T
, col = c("blue","red")
,main = "Desn.Est.anualizada",
legend.text = c("SLB","T-Bills")
)