Aluno: Francisco de Assis Pereira Neto
Data: 27 de outubro de 2020
require(Quandl)
require(BatchGetSymbols)
require(dplyr)
require(DT)
library(lubridate)
if (!require(xts)) install.packages("xts")
library(xts)
library(PerformanceAnalytics)
if (!require(lubridate)) install.packages("lubridate")
library(ggplot2)
if (!require(PerformanceAnalytics)) install.packages("PerformanceAnalytics")
# set symbol and dates
my.symbol <- 'BCB/4390' # SELIC by month
first.date <- as.Date('2010-01-01')
last.date <- Sys.Date()
# get data!
my.df <- Quandl(code = my.symbol,
type='raw',
start_date = first.date,
end_date = last.date)
# check content
glimpse(my.df)
## Rows: 129
## Columns: 2
## $ Date <date> 2020-09-30, 2020-08-31, 2020-07-31, 2020-06-30, 2020-05-31, 20…
## $ Value <dbl> 0.16, 0.16, 0.19, 0.21, 0.24, 0.28, 0.34, 0.29, 0.38, 0.37, 0.3…
colnames(my.df)<- c("ref.date","TaxaSelic")
# set tickers
df.ibov <- GetIbovStocks()
my.tickers <- c('^BVSP','OIBR3.SA','CMIG4.SA','ABEV3.SA',
'BBAS3.SA','GOLL4.SA')
# set dates and other inputs
l.out <- BatchGetSymbols(tickers = my.tickers,
first.date = first.date,
last.date = last.date,
freq.data = 'monthly')
##
## Running BatchGetSymbols for:
## tickers =^BVSP, OIBR3.SA, CMIG4.SA, ABEV3.SA, BBAS3.SA, GOLL4.SA
## Downloading data for benchmark ticker
## ^GSPC | yahoo (1|1) | Found cache file | Need new data
## ^BVSP | yahoo (1|6) | Found cache file | Need new data - Got 96% of valid prices | OK!
## OIBR3.SA | yahoo (2|6) | Not Cached | Saving cache - Got 96% of valid prices | Feels good!
## CMIG4.SA | yahoo (3|6) | Not Cached | Saving cache - Got 96% of valid prices | Well done!
## ABEV3.SA | yahoo (4|6) | Not Cached | Saving cache - Got 96% of valid prices | Feels good!
## BBAS3.SA | yahoo (5|6) | Not Cached | Saving cache - Got 96% of valid prices | Youre doing good!
## GOLL4.SA | yahoo (6|6) | Not Cached | Saving cache - Got 96% of valid prices | Good stuff!
df.tickers <- l.out$df.tickers
datatable(my.df)
datatable(df.tickers)
Em primeiro lugar, para definição dos alfas para encontrar o modelo de Traynos-Black, é necessário listar as ações que o investidor irá comprar e o índice de mercado, no caso do Brasil, o índice Ibovespa. A etapa para definição desses valores é mostrada a seguir:
Tomando os ativos ‘^BVSP’,‘OIBR3.SA’,‘CMIG4.SA’,‘ABEV3.SA’,‘BBAS3.SA’,‘GOLL4.SA’, tem-se a seguinte metodologia de cálculo.
Parte 01) definição dos modelos de regressão linear para os ativos, tomando como a variável dependente o índice ibovespa (retorno do mercado)
require(tidyverse)
## Loading required package: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 3.0.4 ✓ purrr 0.3.4
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date() masks base::date()
## x dplyr::filter() masks stats::filter()
## x dplyr::first() masks xts::first()
## x readr::guess_encoding() masks rvest::guess_encoding()
## x lubridate::intersect() masks base::intersect()
## x dplyr::lag() masks stats::lag()
## x dplyr::last() masks xts::last()
## x purrr::pluck() masks rvest::pluck()
## x lubridate::setdiff() masks base::setdiff()
## x lubridate::union() masks base::union()
base01 <- na.omit(subset(df.tickers, ticker == my.tickers[2])[,c(2,9)])
base02 <-na.omit ( subset(df.tickers, ticker == my.tickers[3])[,c(2,9)])
base03 <- na.omit(subset(df.tickers, ticker == my.tickers[4])[,c(2,9)])
base04 <- na.omit(subset(df.tickers, ticker == my.tickers[5])[,c(2,9)])
base05 <- na.omit(subset(df.tickers, ticker == my.tickers[6])[,c(2,9)])
base06 <- na.omit(subset(df.tickers, ticker == my.tickers[1])[,c(2,9)])
my.tickers
## [1] "^BVSP" "OIBR3.SA" "CMIG4.SA" "ABEV3.SA" "BBAS3.SA" "GOLL4.SA"
basefinal <- inner_join(base01,base02, by = 'ref.date')
basefinal <- inner_join(basefinal,base03, by = 'ref.date')
basefinal <- inner_join(basefinal,base04, by = 'ref.date')
basefinal <- inner_join(basefinal,base05, by = 'ref.date')
basefinal <- inner_join(basefinal,base06, by = 'ref.date')
basefinal <- cbind.data.frame(month(basefinal$ref.date),year(basefinal$ref.date),basefinal)
colnames(basefinal)[1:3]<-c('Month','Year','Date')
my.df <- cbind.data.frame(month(my.df$ref.date),year(my.df$ref.date),my.df)
colnames(my.df)[1:3]<-c('Month','Year','Date')
basefinal <- basefinal %>% unite(MonthYear,Month,Year)
my.df <- my.df %>% unite(MonthYear,Month,Year)
basetodos <- inner_join(basefinal,my.df, by = "MonthYear")
basetodos <- basetodos[,-c(1,9)]
colnames(basetodos)<-c('Date','ROIBR3.SA','RCMIG4.SA',"ABEV3.SA","BBAS3.SA","GOLL4.SA","RBVSP",'Rf')
basetodos[,ncol(basetodos)] <- basetodos[,ncol(basetodos)]/100
my_returns = basetodos
head(my_returns,10)
## Date ROIBR3.SA RCMIG4.SA ABEV3.SA BBAS3.SA GOLL4.SA
## 1 2010-02-01 -0.281218012 -0.010202947 -0.009803830 0.003344036 -0.12889727
## 2 2010-03-01 -0.013939815 -0.033810684 -0.003961454 0.015854446 0.09995634
## 3 2010-04-01 -0.219582041 -0.016672116 -0.020449452 -0.006666889 -0.06632625
## 4 2010-05-03 -0.093146802 0.061307571 0.035880409 -0.008053298 0.01579636
## 5 2010-06-01 0.164054281 -0.127614209 0.037010640 -0.118717434 -0.10410372
## 6 2010-07-01 -0.041052555 0.054768960 0.035354304 0.019088698 0.03182260
## 7 2010-08-02 -0.003193688 0.006114964 0.067707059 0.164372845 0.18177576
## 8 2010-09-01 -0.030131707 0.073318388 0.021563910 -0.032910289 -0.07315143
## 9 2010-10-01 -0.005287220 -0.024115721 0.207736297 0.125705053 0.15784977
## 10 2010-11-01 0.084386133 0.088403995 0.098443543 0.029339561 0.11164334
## RBVSP Rf
## 1 -0.049582411 0.0059
## 2 0.009853993 0.0076
## 3 0.058130541 0.0067
## 4 -0.056469298 0.0075
## 5 -0.078636452 0.0079
## 6 -0.009783154 0.0086
## 7 0.118900647 0.0089
## 8 -0.021075062 0.0085
## 9 0.047053211 0.0081
## 10 0.018966524 0.0081
#convert the date into a date/time format
my_returns$Date <- as.POSIXlt(my_returns$Date, tz = "", format = "%Y-%d-%m")
#order my data according to the date
returns2<-my_returns[order(my_returns$Date),]
#create an xts dataset
All.dat2<-xts(returns2[,-1],order.by=returns2[,1],)
#generate the cumulative returns for each investment
Return.cumulative(All.dat2, geometric =TRUE)
## ROIBR3.SA RCMIG4.SA ABEV3.SA BBAS3.SA GOLL4.SA RBVSP
## Cumulative Return -0.991302 0.08446461 2.855117 0.8600383 -0.1398379 0.3291337
## Rf
## Cumulative Return 1.507761
#plot the cumulative return for each investment
chart.CumReturns(All.dat2, wealth.index =FALSE, geometric = TRUE, legend.loc = 'topleft')
## Traynor Black model
port01 <-TreynorRatio(All.dat2[,1:5],All.dat2[,6],All.dat2[,7])
(port01)
## ROIBR3.SA RCMIG4.SA ABEV3.SA BBAS3.SA GOLL4.SA
## Treynor Ratio: RBVSP -0.4688395 -0.07900879 0.1039306 -0.01971201 -0.04585916
wi <- c(-0.4688395, -0.07900879, 0.1039306,-0.01971201, -0.04585916)
wis <- wi/sum(wi)
Return.portfolio(All.dat2[,1:5], weights =wis )
## Warning: timezone of object (UTC) is different than current timezone ().
## portfolio.returns
## 2010-02-01 -2.698362e-01
## 2010-03-01 -7.062716e-03
## 2010-04-01 -2.035236e-01
## 2010-05-03 -7.721141e-02
## 2010-06-01 6.622806e-02
## 2010-07-01 -3.372686e-02
## 2010-08-02 7.041345e-03
## 2010-09-01 -3.236081e-02
## 2010-10-01 -7.102065e-02
## 2010-11-01 7.779206e-02
## 2010-12-01 -1.360621e-01
## 2011-01-03 -7.951229e-02
## 2011-02-01 2.269335e-01
## 2011-03-01 -8.424175e-02
## 2011-06-01 4.691977e-02
## 2011-07-01 -6.865711e-02
## 2011-08-01 -2.370599e-01
## 2011-09-01 -2.073421e-01
## 2011-10-03 -2.813297e-01
## 2011-11-01 1.976912e-01
## 2011-12-01 -1.782543e-01
## 2012-03-01 -3.836424e-02
## 2012-04-02 -4.147684e-01
## 2012-05-02 -1.852743e-01
## 2012-06-01 -3.972307e-01
## 2012-07-02 2.552707e-01
## 2012-08-01 3.714874e-01
## 2012-09-03 -7.307153e-01
## 2012-10-01 -2.078565e+00
## 2012-11-01 2.169154e+00
## 2012-12-03 1.214257e+00
## 2013-01-02 4.197432e-01
## 2013-02-01 7.875846e-02
## 2013-03-01 2.423035e-01
## 2013-04-01 -2.372500e-02
## 2013-05-02 2.411350e-02
## 2013-06-03 1.134643e-01
## 2013-07-01 1.687345e-01
## 2013-08-01 5.820876e-02
## 2013-09-02 -4.484040e-02
## 2013-10-01 4.407571e-02
## 2013-11-01 1.932546e-05
## 2013-12-02 1.083258e-01
## 2014-01-02 5.977171e-03
## 2014-02-03 -1.969564e-01
## 2014-03-05 1.737233e-01
## 2014-04-01 4.568218e-02
## 2014-05-02 -1.175062e-01
## 2014-06-02 6.848189e-02
## 2014-07-01 -2.232213e-02
## 2014-08-01 -7.000164e-02
## 2014-09-01 1.745635e-02
## 2014-10-01 7.540370e-02
## 2014-11-03 9.582705e-02
## 2014-12-01 -1.214650e-02
## 2015-01-02 5.471193e-02
## 2015-02-02 1.934116e-01
## 2015-03-02 5.360014e-02
## 2015-04-01 6.288411e-02
## 2015-05-04 -2.683077e-03
## 2015-06-01 -4.692352e-02
## 2015-07-01 9.239061e-02
## 2015-08-03 7.185701e-02
## 2015-09-01 -4.105220e-03
## 2015-10-01 8.039412e-02
## 2015-11-03 -1.959996e-02
## 2015-12-01 -3.007178e-02
## 2016-01-04 -6.955961e-02
## 2016-02-01 7.529554e-02
## 2016-03-01 -1.481943e-04
## 2016-04-01 1.812455e-03
## 2016-05-02 4.958298e-02
## 2016-06-01 1.361121e-02
## 2016-07-01 -3.700605e-02
## 2016-08-01 -4.608889e-02
## 2016-09-01 2.906669e-02
## 2016-10-03 7.210118e-03
## 2016-11-01 -9.440800e-02
## 2016-12-01 -7.226564e-02
## 2017-01-02 -1.925130e-02
## 2017-02-01 3.837003e-02
## 2017-03-01 -7.840130e-03
## 2017-04-03 1.681437e-02
## 2017-05-02 4.490856e-02
## 2017-06-01 5.269347e-02
## 2017-07-03 -1.551812e-02
## 2017-08-01 4.761135e-02
## 2017-09-01 1.492893e-02
## 2017-10-02 4.931842e-02
## 2017-11-01 6.221310e-03
## 2017-12-01 -2.607649e-03
## 2018-01-02 8.674733e-02
## 2018-02-01 1.268037e-03
## 2018-03-01 -7.069914e-03
## 2018-04-02 7.612328e-02
## 2018-05-02 -4.278175e-02
## 2018-06-01 -1.194529e-01
## 2018-07-02 -8.292478e-02
## 2018-08-01 5.445877e-02
## 2018-09-03 -2.004739e-02
## 2018-10-01 -2.394892e-03
## 2018-11-01 -2.736744e-01
## 2018-12-03 -1.301175e-02
## 2019-01-02 -1.022237e-01
## 2019-02-01 2.122068e-01
## 2019-03-01 -1.354802e-01
## 2019-04-01 2.224973e-02
## 2019-05-02 1.082295e-01
## 2019-06-03 -9.052515e-02
## 2019-07-01 -5.844579e-03
## 2019-08-01 2.087046e-01
## 2019-09-02 -1.049478e-01
## 2019-10-01 1.011444e-01
## 2019-11-01 -1.772107e-01
## 2019-12-02 9.479839e-02
## 2020-01-02 9.085373e-02
## 2020-02-03 -1.350114e-01
## 2020-03-02 -2.094609e-01
## 2020-04-01 2.111476e-02
## 2020-05-04 -3.052664e-02
## 2020-06-01 2.250630e-02
## 2020-07-01 1.532603e-01
## 2020-08-03 -9.031852e-02
## 2020-09-01 -1.198421e-01
plot(Return.portfolio(All.dat2[,1:5], weights =wis ))
chart.CumReturns(Return.portfolio(All.dat2[,1:5], weights =wis ), wealth.index =FALSE, geometric = TRUE, legend.loc = 'topleft')
SharpeRatio(All.dat2[,1:5],All.dat2[,7])
## ROIBR3.SA RCMIG4.SA ABEV3.SA BBAS3.SA
## StdDev Sharpe (Rf=0.7%, p=95%): -0.1337232 0.001854506 0.08452316 0.04035149
## VaR Sharpe (Rf=0.7%, p=95%): -0.1111071 0.001380752 0.05749685 0.02999561
## ES Sharpe (Rf=0.7%, p=95%): -0.1034118 0.001168979 0.04137372 0.02364255
## GOLL4.SA
## StdDev Sharpe (Rf=0.7%, p=95%): 0.06154860
## VaR Sharpe (Rf=0.7%, p=95%): 0.04518793
## ES Sharpe (Rf=0.7%, p=95%): 0.03650526
Para a seguinte análise, existe um pacote no R que já trás todos os resultados necessários.
VolatilitySkewness((All.dat2[,1:5]))
## ROIBR3.SA RCMIG4.SA ABEV3.SA
## VolatilitySkewness (MAR = 0%, stat= volatility) 1.230078 1.410451 1.618097
## BBAS3.SA GOLL4.SA
## VolatilitySkewness (MAR = 0%, stat= volatility) 1.544002 1.742142
wi <-c(1.230078,1.410451,1.618097,1.544002,1.742142)
wio <- wi/sum(wi)
plot(Return.portfolio(All.dat2[,1:5], weights =wio ))
chart.CumReturns(Return.portfolio(All.dat2[,1:5], weights =wio ), wealth.index =FALSE, geometric = TRUE, legend.loc = 'topleft')
É possível ver uma melhora nos retornos
“Você tem que descobrir se você, o CIO de um portfólio institucional, está investido em hedge fundos ou firmas de private equity que você está confiante que continuarão a ser capazes de produzir alfa para voce. Se sim, por que não obter mais exposição? Você pode querer ser criativo sobre como você faz naquela.”
O autor da setença quis mostrar a questão da gestão de portifólio usando também conhecimentos do gestor do fundo. Neste caso, tem-se uma gestão ativa do portifólio que é escolhido um ponto de partida e este é ajustado de acordo com os retornos que os investimentos vão trazendo ao fundo.
Em relação à frase anterior, eu dizia que é importante ter uma boa gestão de fundos e que o método ou os métodos disponíveis devem ser testados com a finalidade de buscar um aumento do desempenho do portifólio.