A Momentum Effect Screen for the Brazilian Stocks listed on B3

In the Smart-Beta literature, we have seminal contributions that originate from the following models: CAPM (Capital Asset Pricing Model giving the market risk factor) (Sharpe et al., 1964), Fama and French (1992-93) with the 3-Factors Model, Fama and French (2015) with the 5-Factors Model. In addition to the seminal works, we also have other factors that were added, such as the Momentum (or inertial effect) by Jegadeesh and Titman (1993), betting against Beta with Franzzini and Pedersen (2013) or the effect of Quality minus Junk from Azness, Franzzini and Pedersen (2017).

In today’s post we will focus on the momentum effect and in a practical form develop a script that can obtain the stocks data from companies listed on B3 and separate those that are in the tenth decile of the momentum effect.

The initial step is to clean workspace and load packages that we’ll use in the script:

library(readxl)             # to read .xls files.
library(PortfolioAnalytics) # Useful functions to acumulate stocks' returns.
library(xtsanalytics)       # Useful functions to download data from yahoo in data.frame format.
library(tidyquant)          # Useful functions to use to manipulate data.
library(dplyr)              # Useful functions to use to manipulate data.
library(data.table)         # Useful functions to use with dataframes.
library(tidyr)              # Useful functions to use to manipulate data.
library(ggplot2)            # Beautiful plots.
library(ggrepel)            # Useful to manipulate label in the plots.

rm(list=ls())

Now let’s read an “.xls” file extracted in October 2021 from [Economática]https://economatica.com/ with the list of all active companies (for trading) listed on [B3]{https://www.b3.com.br/pt_br/}.

# Reading .xls file...
Lista_Acoes_10_21 <- read_excel("D:/Google Drive/Documentos Julio/Rstudio/Aprendizado/Finanças/Nova pasta/Lista_Acoes_10_21.xlsx")

# Looking to the first 20 rows
head(Lista_Acoes_10_21, 20)
## # A tibble: 20 x 10
##     ...1 Nome    Classe `Bolsa / Fonte` `Tipo de Ativo~` `Ativo /\r\nCa~` Código
##    <dbl> <chr>   <chr>  <chr>           <chr>            <chr>            <chr> 
##  1     1 3r Pet~ ON     Bovespa         Ação             ativo            RRRP3 
##  2     2 Abc Br~ PN     Bovespa         Ação             ativo            ABCB4 
##  3     3 Aeris   ON     Bovespa         Ação             ativo            AERI3 
##  4     4 AES Br~ ON     Bovespa         Ação             ativo            AESB3 
##  5     5 Alians~ ON     Bovespa         Ação             ativo            ALSO3 
##  6     6 Alliar  ON     Bovespa         Ação             ativo            AALR3 
##  7     7 Allied  ON     Bovespa         Ação             ativo            ALLD3 
##  8     8 Alparg~ PN     Bovespa         Ação             ativo            ALPA4 
##  9     9 Alupar  UNT N2 Bovespa         Ação             ativo            ALUP11
## 10    10 Ambev ~ ON     Bovespa         Ação             ativo            ABEV3 
## 11    11 Ambipar ON     Bovespa         Ação             ativo            AMBP3 
## 12    12 Americ~ ON     Bovespa         Ação             ativo            AMER3 
## 13    13 Anima   ON     Bovespa         Ação             ativo            ANIM3 
## 14    14 Arezzo~ ON     Bovespa         Ação             ativo            ARZZ3 
## 15    15 Armac   ON     Bovespa         Ação             ativo            ARML3 
## 16    16 Assai   ON     Bovespa         Ação             ativo            ASAI3 
## 17    17 Azul    PN     Bovespa         Ação             ativo            AZUL4 
## 18    18 B3      ON     Bovespa         Ação             ativo            B3SA3 
## 19    19 Banco ~ ON     Bovespa         Ação             ativo            BIDI3 
## 20    20 Banco ~ PN     Bovespa         Ação             ativo            BIDI4 
## # ... with 3 more variables: `Ativo /\r\nCancelado...8` <chr>,
## #   `Tipo de Ativo...9` <chr>,
## #   `Media\r\nnulos = 0\r\ndo volume$\r\nem 1 mês\r\n(de 27Ago21\r\naté 26Set21)\r\nEm moeda orig\r\nem milhares` <dbl>

Now let’s select the dataframe information that we are interested in. In this case it is the “código” column and we need to concatenate the expression “.SA” to the label. After that, let’s download it through the functions compatible with yahoofinance.

# Tickers to Download...
Tickers <- paste0(Lista_Acoes_10_21$Código, '.SA')

# First and Last Dates.
Data_inicial <- "2020-01-01"
Data_final   <- Sys.Date()

# Dataframe downloaded with closing prices
Precos_Acoes   <- mget_symbols(Tickers, 
                               OHLC = "Ad",
                               from = Data_inicial)
## Getting data from yahoo...
## Downloading symbols: RRRP3.SA ABCB4.SA AERI3.SA AESB3.SA ALSO3.SA AALR3.SA ALLD3.SA ALPA4.SA ALUP11.SA ABEV3.SA AMBP3.SA AMER3.SA ANIM3.SA ARZZ3.SA ARML3.SA ASAI3.SA AZUL4.SA B3SA3.SA BIDI3.SA BIDI4.SA BIDI11.SA BPAN4.SA BRSR6.SA BBSE3.SA BMOB3.SA BKBR3.SA BLAU3.SA BOAS3.SA BRML3.SA BRBI11.SA BRPR3.SA BBDC3.SA BBDC4.SA BRAP3.SA BRAP4.SA BBAS3.SA AGRO3.SA BRKM5.SA BRFS3.SA BPAC11.SA CXSE3.SA CAML3.SA CRFB3.SA CBAV3.SA CCRO3.SA CEAB3.SA CMIG4.SA CESP6.SA CIEL3.SA CLSA3.SA COGN3.SA CSMG3.SA CPLE3.SA CPLE6.SA CPLE11.SA CSAN3.SA CPFE3.SA CMIN3.SA CVCB3.SA CYRE3.SA DXCO3.SA PNVL3.SA DIRR3.SA ECOR3.SA ELET3.SA ELET6.SA EMBR3.SA ENAT3.SA ENBR3.SA ENGI11.SA ENEV3.SA EGIE3.SA ENJU3.SA EQTL3.SA ESPA3.SA ETER3.SA EVEN3.SA EZTC3.SA FHER3.SA FESA4.SA FLRY3.SA GFSA3.SA GGBR4.SA GOAU4.SA GOLL4.SA GGPS3.SA GRND3.SA GMAT3.SA NTCO3.SA SBFG3.SA SOMA3.SA HAPV3.SA HBSA3.SA HYPE3.SA IGTA3.SA PARD3.SA MEAL3.SA ROMI3.SA IFCM3.SA INTB3.SA GNDI3.SA MYPK3.SA RANI3.SA IRBR3.SA ITSA4.SA ITUB3.SA ITUB4.SA JBSS3.SA JHSF3.SA KEPL3.SA KLBN11.SA LIGT3.SA RENT3.SA LCAM3.SA LWSA3.SA LOGG3.SA LOGN3.SA LAME3.SA LAME4.SA AMAR3.SA LREN3.SA MDIA3.SA MGLU3.SA MRFG3.SA CASH3.SA LEVE3.SA BEEF3.SA MOSI3.SA MOVI3.SA MRVE3.SA MLAS3.SA MULT3.SA NEOE3.SA OPCT3.SA ODPV3.SA OIBR3.SA OMGE3.SA ONCO3.SA ORVR3.SA PCAR3.SA PGMN3.SA PETR3.SA PETR4.SA RECV3.SA PRIO3.SA PETZ3.SA PSSA3.SA PTBL3.SA POSI3.SA QUAL3.SA LJQQ3.SA RADL3.SA RAIZ4.SA RAPT4.SA RCSL3.SA RDOR3.SA RAIL3.SA SBSP3.SA SAPR4.SA SAPR11.SA SANB11.SA STBP3.SA SMTO3.SA SEQL3.SA SEER3.SA CSNA3.SA SIMH3.SA SQIA3.SA SLCE3.SA SMFT3.SA SULA11.SA SUZB3.SA TAEE11.SA TASA4.SA TRAD3.SA TECN3.SA VIVT3.SA TEND3.SA TIMS3.SA TOTS3.SA TRPL4.SA TRIS3.SA TPIS3.SA TUPY3.SA UGPA3.SA UNIP6.SA USIM5.SA VALE3.SA VAMO3.SA VIIA3.SA VBBR3.SA VIVA3.SA VIVR3.SA WEGE3.SA WIZS3.SA YDUQ3.SA
## 
## Successfully downloaded symbol: RRRP3.SA
## Successfully downloaded symbol: ABCB4.SA
## Successfully downloaded symbol: AERI3.SA
## Successfully downloaded symbol: AESB3.SA
## Successfully downloaded symbol: ALSO3.SA
## Successfully downloaded symbol: AALR3.SA
## Successfully downloaded symbol: ALLD3.SA
## Successfully downloaded symbol: ALPA4.SA
## Successfully downloaded symbol: ALUP11.SA
## Successfully downloaded symbol: ABEV3.SA
## Successfully downloaded symbol: AMBP3.SA
## Successfully downloaded symbol: AMER3.SA
## Successfully downloaded symbol: ANIM3.SA
## Successfully downloaded symbol: ARZZ3.SA
## Successfully downloaded symbol: ARML3.SA
## Successfully downloaded symbol: ASAI3.SA
## Successfully downloaded symbol: AZUL4.SA
## Successfully downloaded symbol: B3SA3.SA
## Successfully downloaded symbol: BIDI3.SA
## Successfully downloaded symbol: BIDI4.SA
## Successfully downloaded symbol: BIDI11.SA
## Successfully downloaded symbol: BPAN4.SA
## Successfully downloaded symbol: BRSR6.SA
## Successfully downloaded symbol: BBSE3.SA
## Successfully downloaded symbol: BMOB3.SA
## Successfully downloaded symbol: BKBR3.SA
## Successfully downloaded symbol: BLAU3.SA
## Successfully downloaded symbol: BOAS3.SA
## Successfully downloaded symbol: BRML3.SA
## Successfully downloaded symbol: BRBI11.SA
## Successfully downloaded symbol: BRPR3.SA
## Successfully downloaded symbol: BBDC3.SA
## Successfully downloaded symbol: BBDC4.SA
## Successfully downloaded symbol: BRAP3.SA
## Successfully downloaded symbol: BRAP4.SA
## Successfully downloaded symbol: BBAS3.SA
## Successfully downloaded symbol: AGRO3.SA
## Successfully downloaded symbol: BRKM5.SA
## Successfully downloaded symbol: BRFS3.SA
## Successfully downloaded symbol: BPAC11.SA
## Successfully downloaded symbol: CXSE3.SA
## Successfully downloaded symbol: CAML3.SA
## Successfully downloaded symbol: CRFB3.SA
## Successfully downloaded symbol: CBAV3.SA
## Successfully downloaded symbol: CCRO3.SA
## Successfully downloaded symbol: CEAB3.SA
## Successfully downloaded symbol: CMIG4.SA
## Successfully downloaded symbol: CESP6.SA
## Successfully downloaded symbol: CIEL3.SA
## Successfully downloaded symbol: CLSA3.SA
## Successfully downloaded symbol: COGN3.SA
## Successfully downloaded symbol: CSMG3.SA
## Successfully downloaded symbol: CPLE3.SA
## Successfully downloaded symbol: CPLE6.SA
## Successfully downloaded symbol: CPLE11.SA
## Successfully downloaded symbol: CSAN3.SA
## Successfully downloaded symbol: CPFE3.SA
## Successfully downloaded symbol: CMIN3.SA
## Successfully downloaded symbol: CVCB3.SA
## Successfully downloaded symbol: CYRE3.SA
## Successfully downloaded symbol: DXCO3.SA
## Successfully downloaded symbol: PNVL3.SA
## Successfully downloaded symbol: DIRR3.SA
## Successfully downloaded symbol: ECOR3.SA
## Successfully downloaded symbol: ELET3.SA
## Successfully downloaded symbol: ELET6.SA
## Successfully downloaded symbol: EMBR3.SA
## Successfully downloaded symbol: ENAT3.SA
## Successfully downloaded symbol: ENBR3.SA
## Successfully downloaded symbol: ENGI11.SA
## Successfully downloaded symbol: ENEV3.SA
## Successfully downloaded symbol: EGIE3.SA
## Successfully downloaded symbol: ENJU3.SA
## Successfully downloaded symbol: EQTL3.SA
## Successfully downloaded symbol: ESPA3.SA
## Successfully downloaded symbol: ETER3.SA
## Successfully downloaded symbol: EVEN3.SA
## Successfully downloaded symbol: EZTC3.SA
## Successfully downloaded symbol: FHER3.SA
## Successfully downloaded symbol: FESA4.SA
## Successfully downloaded symbol: FLRY3.SA
## Successfully downloaded symbol: GFSA3.SA
## Successfully downloaded symbol: GGBR4.SA
## Successfully downloaded symbol: GOAU4.SA
## Successfully downloaded symbol: GOLL4.SA
## Successfully downloaded symbol: GGPS3.SA
## Successfully downloaded symbol: GRND3.SA
## Successfully downloaded symbol: GMAT3.SA
## Successfully downloaded symbol: NTCO3.SA
## Successfully downloaded symbol: SBFG3.SA
## Successfully downloaded symbol: SOMA3.SA
## Successfully downloaded symbol: HAPV3.SA
## Successfully downloaded symbol: HBSA3.SA
## Successfully downloaded symbol: HYPE3.SA
##   >>>> ERROR DOWNLOADING SYMBOL::IGTA3.SA:: <<<<
## Successfully downloaded symbol: PARD3.SA
## Successfully downloaded symbol: MEAL3.SA
## Successfully downloaded symbol: ROMI3.SA
## Successfully downloaded symbol: IFCM3.SA
## Successfully downloaded symbol: INTB3.SA
## Successfully downloaded symbol: GNDI3.SA
## Successfully downloaded symbol: MYPK3.SA
## Successfully downloaded symbol: RANI3.SA
## Successfully downloaded symbol: IRBR3.SA
## Successfully downloaded symbol: ITSA4.SA
## Successfully downloaded symbol: ITUB3.SA
## Successfully downloaded symbol: ITUB4.SA
## Successfully downloaded symbol: JBSS3.SA
## Successfully downloaded symbol: JHSF3.SA
## Successfully downloaded symbol: KEPL3.SA
## Successfully downloaded symbol: KLBN11.SA
## Successfully downloaded symbol: LIGT3.SA
## Successfully downloaded symbol: RENT3.SA
## Successfully downloaded symbol: LCAM3.SA
## Successfully downloaded symbol: LWSA3.SA
## Successfully downloaded symbol: LOGG3.SA
## Successfully downloaded symbol: LOGN3.SA
## Successfully downloaded symbol: LAME3.SA
## Successfully downloaded symbol: LAME4.SA
## Successfully downloaded symbol: AMAR3.SA
## Successfully downloaded symbol: LREN3.SA
## Successfully downloaded symbol: MDIA3.SA
## Successfully downloaded symbol: MGLU3.SA
## Successfully downloaded symbol: MRFG3.SA
## Successfully downloaded symbol: CASH3.SA
## Successfully downloaded symbol: LEVE3.SA
## Successfully downloaded symbol: BEEF3.SA
## Successfully downloaded symbol: MOSI3.SA
## Successfully downloaded symbol: MOVI3.SA
## Successfully downloaded symbol: MRVE3.SA
## Successfully downloaded symbol: MLAS3.SA
## Successfully downloaded symbol: MULT3.SA
## Successfully downloaded symbol: NEOE3.SA
## Successfully downloaded symbol: OPCT3.SA
## Successfully downloaded symbol: ODPV3.SA
## Successfully downloaded symbol: OIBR3.SA
## Successfully downloaded symbol: OMGE3.SA
## Successfully downloaded symbol: ONCO3.SA
## Successfully downloaded symbol: ORVR3.SA
## Successfully downloaded symbol: PCAR3.SA
## Successfully downloaded symbol: PGMN3.SA
## Successfully downloaded symbol: PETR3.SA
## Successfully downloaded symbol: PETR4.SA
## Successfully downloaded symbol: RECV3.SA
## Successfully downloaded symbol: PRIO3.SA
## Successfully downloaded symbol: PETZ3.SA
## Successfully downloaded symbol: PSSA3.SA
## Successfully downloaded symbol: PTBL3.SA
## Successfully downloaded symbol: POSI3.SA
## Successfully downloaded symbol: QUAL3.SA
## Successfully downloaded symbol: LJQQ3.SA
## Successfully downloaded symbol: RADL3.SA
## Successfully downloaded symbol: RAIZ4.SA
## Successfully downloaded symbol: RAPT4.SA
## Successfully downloaded symbol: RCSL3.SA
## Successfully downloaded symbol: RDOR3.SA
## Successfully downloaded symbol: RAIL3.SA
## Successfully downloaded symbol: SBSP3.SA
## Successfully downloaded symbol: SAPR4.SA
## Successfully downloaded symbol: SAPR11.SA
## Successfully downloaded symbol: SANB11.SA
## Successfully downloaded symbol: STBP3.SA
## Successfully downloaded symbol: SMTO3.SA
## Successfully downloaded symbol: SEQL3.SA
## Successfully downloaded symbol: SEER3.SA
## Successfully downloaded symbol: CSNA3.SA
## Successfully downloaded symbol: SIMH3.SA
## Successfully downloaded symbol: SQIA3.SA
## Successfully downloaded symbol: SLCE3.SA
## Successfully downloaded symbol: SMFT3.SA
## Successfully downloaded symbol: SULA11.SA
## Successfully downloaded symbol: SUZB3.SA
## Successfully downloaded symbol: TAEE11.SA
## Successfully downloaded symbol: TASA4.SA
## Successfully downloaded symbol: TRAD3.SA
## Successfully downloaded symbol: TECN3.SA
## Successfully downloaded symbol: VIVT3.SA
## Successfully downloaded symbol: TEND3.SA
## Successfully downloaded symbol: TIMS3.SA
## Successfully downloaded symbol: TOTS3.SA
## Successfully downloaded symbol: TRPL4.SA
## Successfully downloaded symbol: TRIS3.SA
## Successfully downloaded symbol: TPIS3.SA
## Successfully downloaded symbol: TUPY3.SA
## Successfully downloaded symbol: UGPA3.SA
## Successfully downloaded symbol: UNIP6.SA
## Successfully downloaded symbol: USIM5.SA
## Successfully downloaded symbol: VALE3.SA
## Successfully downloaded symbol: VAMO3.SA
## Successfully downloaded symbol: VIIA3.SA
## Successfully downloaded symbol: VBBR3.SA
## Successfully downloaded symbol: VIVA3.SA
## Successfully downloaded symbol: VIVR3.SA
## Successfully downloaded symbol: WEGE3.SA
## Successfully downloaded symbol: WIZS3.SA
## Successfully downloaded symbol: YDUQ3.SA
## 
## 
## DOWNLOAD SUMMARY:
## =================
## Symbols successfully downloaded: RRRP3.SA ABCB4.SA AERI3.SA AESB3.SA ALSO3.SA AALR3.SA ALLD3.SA ALPA4.SA ALUP11.SA ABEV3.SA AMBP3.SA AMER3.SA ANIM3.SA ARZZ3.SA ARML3.SA ASAI3.SA AZUL4.SA B3SA3.SA BIDI3.SA BIDI4.SA BIDI11.SA BPAN4.SA BRSR6.SA BBSE3.SA BMOB3.SA BKBR3.SA BLAU3.SA BOAS3.SA BRML3.SA BRBI11.SA BRPR3.SA BBDC3.SA BBDC4.SA BRAP3.SA BRAP4.SA BBAS3.SA AGRO3.SA BRKM5.SA BRFS3.SA BPAC11.SA CXSE3.SA CAML3.SA CRFB3.SA CBAV3.SA CCRO3.SA CEAB3.SA CMIG4.SA CESP6.SA CIEL3.SA CLSA3.SA COGN3.SA CSMG3.SA CPLE3.SA CPLE6.SA CPLE11.SA CSAN3.SA CPFE3.SA CMIN3.SA CVCB3.SA CYRE3.SA DXCO3.SA PNVL3.SA DIRR3.SA ECOR3.SA ELET3.SA ELET6.SA EMBR3.SA ENAT3.SA ENBR3.SA ENGI11.SA ENEV3.SA EGIE3.SA ENJU3.SA EQTL3.SA ESPA3.SA ETER3.SA EVEN3.SA EZTC3.SA FHER3.SA FESA4.SA FLRY3.SA GFSA3.SA GGBR4.SA GOAU4.SA GOLL4.SA GGPS3.SA GRND3.SA GMAT3.SA NTCO3.SA SBFG3.SA SOMA3.SA HAPV3.SA HBSA3.SA HYPE3.SA PARD3.SA MEAL3.SA ROMI3.SA IFCM3.SA INTB3.SA GNDI3.SA MYPK3.SA RANI3.SA IRBR3.SA ITSA4.SA ITUB3.SA ITUB4.SA JBSS3.SA JHSF3.SA KEPL3.SA KLBN11.SA LIGT3.SA RENT3.SA LCAM3.SA LWSA3.SA LOGG3.SA LOGN3.SA LAME3.SA LAME4.SA AMAR3.SA LREN3.SA MDIA3.SA MGLU3.SA MRFG3.SA CASH3.SA LEVE3.SA BEEF3.SA MOSI3.SA MOVI3.SA MRVE3.SA MLAS3.SA MULT3.SA NEOE3.SA OPCT3.SA ODPV3.SA OIBR3.SA OMGE3.SA ONCO3.SA ORVR3.SA PCAR3.SA PGMN3.SA PETR3.SA PETR4.SA RECV3.SA PRIO3.SA PETZ3.SA PSSA3.SA PTBL3.SA POSI3.SA QUAL3.SA LJQQ3.SA RADL3.SA RAIZ4.SA RAPT4.SA RCSL3.SA RDOR3.SA RAIL3.SA SBSP3.SA SAPR4.SA SAPR11.SA SANB11.SA STBP3.SA SMTO3.SA SEQL3.SA SEER3.SA CSNA3.SA SIMH3.SA SQIA3.SA SLCE3.SA SMFT3.SA SULA11.SA SUZB3.SA TAEE11.SA TASA4.SA TRAD3.SA TECN3.SA VIVT3.SA TEND3.SA TIMS3.SA TOTS3.SA TRPL4.SA TRIS3.SA TPIS3.SA TUPY3.SA UGPA3.SA UNIP6.SA USIM5.SA VALE3.SA VAMO3.SA VIIA3.SA VBBR3.SA VIVA3.SA VIVR3.SA WEGE3.SA WIZS3.SA YDUQ3.SA 
## Symbols NOT downloaded: IGTA3.SA
## 
## Applying na.locf to the matrix...
head(Precos_Acoes)
##            RRRP3.SA ABCB4.SA AERI3.SA AESB3.SA ALSO3.SA AALR3.SA ALLD3.SA
## 2020-01-02       NA 18.50136       NA       NA 49.58987 18.84397       NA
## 2020-01-03       NA 18.91149       NA       NA 50.98803 18.84397       NA
## 2020-01-06       NA 18.79301       NA       NA 50.64097 18.83405       NA
## 2020-01-07       NA 18.55605       NA       NA 50.04601 18.78446       NA
## 2020-01-08       NA 18.50136       NA       NA 51.56316 18.81421       NA
## 2020-01-09       NA 18.50136       NA       NA 51.93997 19.73657       NA
##            ALPA4.SA ALUP11.SA ABEV3.SA AMBP3.SA AMER3.SA  ANIM3.SA ARZZ3.SA
## 2020-01-02 32.17125  26.29335 17.91744       NA 63.89658  9.866666 62.62740
## 2020-01-03 32.17125  26.74442 17.66547       NA 63.03391  9.983333 61.73566
## 2020-01-06 31.66764  26.91356 17.74946       NA 62.64178 10.066666 61.35349
## 2020-01-07 32.38849  26.34034 17.78678       NA 62.48494 10.813333 61.63767
## 2020-01-08 32.74397  26.04902 17.70280       NA 63.31820 10.923333 60.75573
## 2020-01-09 33.53393  26.19938 17.56282       NA 66.70026 10.810000 60.06978
##            ARML3.SA ASAI3.SA AZUL4.SA B3SA3.SA BIDI3.SA BIDI4.SA BIDI11.SA
## 2020-01-02       NA       NA    58.80 13.72084 4.893915 5.203439  15.07547
## 2020-01-03       NA       NA    56.76 13.32542 5.119227 5.296239  15.42675
## 2020-01-06       NA       NA    55.00 13.19768 5.434001 5.717154  16.70928
## 2020-01-07       NA       NA    56.82 13.62350 5.927701 5.809955  17.44168
## 2020-01-08       NA       NA    56.92 13.58701 5.665940 5.760241  17.33894
## 2020-01-09       NA       NA    57.28 13.58092 5.467135 5.720469  16.74242
##            BPAN4.SA BRSR6.SA BBSE3.SA BMOB3.SA BKBR3.SA BLAU3.SA BOAS3.SA
## 2020-01-02 10.25531 19.37065 31.21789       NA 17.28928       NA       NA
## 2020-01-03 10.69833 19.24000 30.87375       NA 16.99033       NA       NA
## 2020-01-06 10.94340 19.20516 31.38176       NA 17.16970       NA       NA
## 2020-01-07 10.93398 19.29226 31.39814       NA 17.27932       NA       NA
## 2020-01-08 10.83972 18.87418 31.38176       NA 17.16970       NA       NA
## 2020-01-09 10.83972 18.63031 31.29163       NA 17.08002       NA       NA
##            BRML3.SA BRBI11.SA BRPR3.SA BBDC3.SA BBDC4.SA BRAP3.SA BRAP4.SA
## 2020-01-02    18.22        NA 14.23753 27.13344 31.70484 22.56769 24.00585
## 2020-01-03    18.63        NA 15.10922 26.71003 31.71943 22.65641 24.34672
## 2020-01-06    18.50        NA 14.72180 26.55683 31.15286 22.49798 24.46034
## 2020-01-07    18.80        NA 15.25450 26.10490 30.61166 22.75148 24.56765
## 2020-01-08    18.25        NA 14.81866 25.70659 30.13811 22.56135 24.56134
## 2020-01-09    18.51        NA 14.81866 25.45381 29.65610 22.47263 24.24572
##            BBAS3.SA AGRO3.SA BRKM5.SA BRFS3.SA BPAC11.SA CXSE3.SA CAML3.SA
## 2020-01-02 48.00270 16.94585 26.90310    35.88  18.23577       NA 8.524587
## 2020-01-03 47.92240 17.12191 28.09781    35.94  18.28869       NA 8.570918
## 2020-01-06 47.28891 17.13071 29.55801    35.57  18.26223       NA 8.394866
## 2020-01-07 46.93200 17.07789 28.78808    35.16  18.33509       NA 8.209547
## 2020-01-08 46.50373 16.90183 30.23059    36.51  18.17260       NA 8.265143
## 2020-01-09 45.55796 17.25395 30.84121    36.84  18.29629       NA 8.283675
##            CRFB3.SA CBAV3.SA CCRO3.SA CEAB3.SA CMIG4.SA CESP6.SA CIEL3.SA
## 2020-01-02 22.31084       NA 18.05200    18.10 10.43481 27.47140 7.871391
## 2020-01-03 22.88888       NA 17.85248    18.00 10.32204 27.69090 7.770357
## 2020-01-06 21.89128       NA 17.39642    17.81 10.46488 27.22657 7.577477
## 2020-01-07 21.76076       NA 17.78597    17.18 10.84829 26.59340 7.357041
## 2020-01-08 21.35053       NA 17.66245    16.27 11.14901 25.92645 7.191714
## 2020-01-09 21.81669       NA 17.23491    15.71 11.05879 26.52585 6.750843
##            CLSA3.SA COGN3.SA CSMG3.SA CPLE3.SA CPLE6.SA CPLE11.SA CSAN3.SA
## 2020-01-02       NA    12.20 17.54729 5.821024 5.439591        NA 17.05802
## 2020-01-03       NA    12.15 17.69477 6.149872 5.435631        NA 17.38866
## 2020-01-06       NA    11.99 17.32219 6.086665 5.444343        NA 17.28638
## 2020-01-07       NA    12.08 17.06087 6.124248 5.472064        NA 17.59800
## 2020-01-08       NA    12.00 17.06863 6.060185 5.386526        NA 17.30779
## 2020-01-09       NA    12.10 17.16436 6.273723 5.545722        NA 17.12225
##            CPFE3.SA CMIN3.SA CVCB3.SA CYRE3.SA DXCO3.SA PNVL3.SA DIRR3.SA
## 2020-01-02 27.37388       NA    44.71 27.52719 14.11444 19.26121 13.94850
## 2020-01-03 27.20284       NA    44.20 29.02256 14.32607 19.26121 13.93950
## 2020-01-06 27.45939       NA    43.82 28.75711 14.22840 19.26121 13.75964
## 2020-01-07 27.14064       NA    44.40 28.84559 14.28538 19.58822 13.84058
## 2020-01-08 26.96960       NA    44.34 27.95191 14.22840 19.58822 13.92152
## 2020-01-09 27.45939       NA    43.39 27.81034 14.12258 19.58822 13.93950
##            ECOR3.SA ELET3.SA ELET6.SA EMBR3.SA ENAT3.SA ENBR3.SA ENGI11.SA
## 2020-01-02    16.30 31.79857 32.06020    20.20 14.92841 19.77205  48.10260
## 2020-01-03    16.17 31.31551 31.92768    20.41 15.22540 19.89839  48.93304
## 2020-01-06    16.08 31.46542 31.97738    20.38 15.02449 19.74498  49.08649
## 2020-01-07    16.44 32.27329 32.46603    20.50 15.09438 19.62766  48.50879
## 2020-01-08    16.48 32.73970 32.73933    20.33 14.91967 19.60059  48.47269
## 2020-01-09    16.43 32.77301 33.21970    20.19 14.23833 20.08790  49.39340
##            ENEV3.SA EGIE3.SA ENJU3.SA EQTL3.SA ESPA3.SA ETER3.SA EVEN3.SA
## 2020-01-02  10.9650 45.38751       NA 22.28335       NA 3.968861 14.61614
## 2020-01-03  11.0675 45.64936       NA 22.01842       NA 3.793084 14.53519
## 2020-01-06  11.3500 45.26531       NA 22.04680       NA 4.135387 14.58916
## 2020-01-07  11.2650 45.36133       NA 21.93325       NA 4.477689 15.06587
## 2020-01-08  11.2125 44.90745       NA 22.08465       NA 4.533197 14.87698
## 2020-01-09  11.3750 45.03838       NA 22.34959       NA 5.060529 14.84101
##            EZTC3.SA FHER3.SA FESA4.SA FLRY3.SA GFSA3.SA GGBR4.SA GOAU4.SA
## 2020-01-02 51.02477     3.70 18.49758 28.96132     9.71 18.21001 7.771845
## 2020-01-03 51.43041     3.88 18.60975 28.21401    10.15 17.91178 7.787902
## 2020-01-06 51.67187     3.77 18.33868 29.04797     9.90 17.86792 7.747758
## 2020-01-07 52.38658     3.90 18.40411 29.34782     9.55 18.14861 7.803960
## 2020-01-08 52.92745     3.78 18.59105 29.16979     9.09 18.21879 7.755787
## 2020-01-09 53.70011     3.90 18.23587 29.71326     9.07 18.12230 7.763816
##            GOLL4.SA GGPS3.SA GRND3.SA GMAT3.SA NTCO3.SA SBFG3.SA SOMA3.SA
## 2020-01-02    37.17       NA 10.69575       NA    38.33    37.43       NA
## 2020-01-03    35.90       NA 10.80880       NA    41.00    37.97       NA
## 2020-01-06    34.25       NA 10.67836       NA    40.13    38.00       NA
## 2020-01-07    34.45       NA 11.22619       NA    40.78    38.65       NA
## 2020-01-08    35.81       NA 11.19141       NA    40.99    38.65       NA
## 2020-01-09    36.65       NA 10.80880       NA    42.00    38.90       NA
##            HAPV3.SA HBSA3.SA HYPE3.SA PARD3.SA MEAL3.SA ROMI3.SA IFCM3.SA
## 2020-01-02 13.16234       NA 33.41003 26.78342     8.87 11.50667       NA
## 2020-01-03 13.71903       NA 34.09679 27.44545     8.60 11.35984       NA
## 2020-01-06 13.29602       NA 33.31722 27.54281     8.70 11.28642       NA
## 2020-01-07 12.93065       NA 32.99240 27.68885     8.75 11.13958       NA
## 2020-01-08 12.66457       NA 32.56550 28.42878     8.70 11.54672       NA
## 2020-01-09 12.58912       NA 33.26155 28.41904     8.49 11.52002       NA
##            INTB3.SA GNDI3.SA MYPK3.SA RANI3.SA IRBR3.SA ITSA4.SA ITUB3.SA
## 2020-01-02       NA 65.53124 21.24656 3.869094 35.23128 12.43174 30.46062
## 2020-01-03       NA 66.75664 21.34522 3.841058 34.95748 12.24981 30.20834
## 2020-01-06       NA 64.00407 21.24656 3.710219 34.63070 12.22382 29.90000
## 2020-01-07       NA 61.81847 21.31831 3.841058 34.86034 12.05922 29.32068
## 2020-01-08       NA 62.18426 21.14791 4.018625 34.99282 11.87729 29.02168
## 2020-01-09       NA 62.52262 21.37212 3.943859 36.34413 11.64339 28.75071
##            ITUB4.SA JBSS3.SA JHSF3.SA KEPL3.SA KLBN11.SA LIGT3.SA RENT3.SA
## 2020-01-02 35.75817 23.61733 6.803896 23.27067  18.17568 23.31595 48.87367
## 2020-01-03 35.38206 23.85176 7.269425 23.46299  18.61803 23.16962 48.16450
## 2020-01-06 34.85551 23.28738 7.305235 23.42452  18.78514 23.21839 47.60307
## 2020-01-07 34.04689 23.70415 7.367901 23.27067  18.84413 23.14035 48.06600
## 2020-01-08 33.49213 24.28590 7.251521 23.07835  19.05055 22.89646 47.82961
## 2020-01-09 32.82454 24.58112 7.278378 23.07835  18.93259 22.81841 48.08569
##            LCAM3.SA LWSA3.SA LOGG3.SA LOGN3.SA LAME3.SA LAME4.SA AMAR3.SA
## 2020-01-02 22.12042       NA 31.72876    22.53 20.25499 26.55891    13.46
## 2020-01-03 22.92168       NA 30.68912    25.05 20.46258 26.60846    13.95
## 2020-01-06 22.41612       NA 31.57183    24.60 20.22533 26.30125    13.63
## 2020-01-07 22.88352       NA 31.87588    23.91 19.82992 25.77602    13.67
## 2020-01-08 22.97891       NA 31.39529    23.44 19.87935 25.58773    13.87
## 2020-01-09 23.32230       NA 31.31682    23.63 20.00785 26.28143    14.31
##            LREN3.SA MDIA3.SA MGLU3.SA MRFG3.SA CASH3.SA LEVE3.SA BEEF3.SA
## 2020-01-02 55.21742 35.75137 12.02974 8.377728       NA 28.09133 10.81319
## 2020-01-03 54.83330 36.16074 11.92244 8.560938       NA 27.36718 11.09707
## 2020-01-06 54.78529 36.59739 11.93219 8.402710       NA 27.85807 11.08847
## 2020-01-07 54.75647 35.34199 11.89805 8.660871       NA 27.65983 11.59601
## 2020-01-08 55.11179 35.65129 12.26384 8.877393       NA 27.69759 11.72505
## 2020-01-09 54.68925 35.93330 12.70036 9.085587       NA 27.31054 11.54440
##            MOSI3.SA MOVI3.SA MRVE3.SA MLAS3.SA MULT3.SA NEOE3.SA OPCT3.SA
## 2020-01-02       NA 18.90520 20.93606       NA 32.06448 22.97123       NA
## 2020-01-03       NA 19.40728 20.93606       NA 32.06448 22.64441       NA
## 2020-01-06       NA 19.47894 20.65314       NA 32.00714 22.78448       NA
## 2020-01-07       NA 19.44006 20.74745       NA 32.09315 22.85912       NA
## 2020-01-08       NA 19.53726 20.38909       NA 32.21740 23.18837       NA
## 2020-01-09       NA 19.80942 20.23819       NA 32.01670 23.13193       NA
##            ODPV3.SA OIBR3.SA OMGE3.SA ONCO3.SA ORVR3.SA PCAR3.SA PGMN3.SA
## 2020-01-02 15.51501     0.86    39.30       NA       NA 83.26806       NA
## 2020-01-03 15.70655     0.86    40.57       NA       NA 83.26806       NA
## 2020-01-06 15.58797     0.92    39.68       NA       NA 83.26806       NA
## 2020-01-07 15.18664     0.94    38.57       NA       NA 83.26806       NA
## 2020-01-08 15.23225     0.92    39.60       NA       NA 81.50677       NA
## 2020-01-09 15.41467     0.90    39.79       NA       NA 85.40411       NA
##            PETR3.SA PETR4.SA RECV3.SA PRIO3.SA PETZ3.SA PSSA3.SA PTBL3.SA
## 2020-01-02 25.77569 24.95087       NA    7.156       NA 28.83138 4.775723
## 2020-01-03 25.13915 24.74768       NA    7.570       NA 28.09419 4.731666
## 2020-01-06 25.95643 25.04026       NA    7.718       NA 28.64259 4.731666
## 2020-01-07 25.60280 24.94274       NA    7.728       NA 28.62461 4.802156
## 2020-01-08 25.18631 24.78832       NA    7.570       NA 28.31895 5.066495
## 2020-01-09 25.32776 24.70705       NA    7.280       NA 28.41334 5.075307
##            POSI3.SA QUAL3.SA LJQQ3.SA RADL3.SA RAIZ4.SA RAPT4.SA RCSL3.SA
## 2020-01-02 9.779281 36.02271       NA 22.62177       NA 13.37098     1.75
## 2020-01-03 9.567525 36.76706       NA 22.11745       NA 13.45514     1.79
## 2020-01-06 9.172888 38.27413       NA 21.68207       NA 13.30553     1.91
## 2020-01-07 9.509774 37.30004       NA 21.38460       NA 13.45514     2.15
## 2020-01-08 9.278768 36.58327       NA 21.67025       NA 13.34293     2.23
## 2020-01-09 9.355769 36.84976       NA 22.32233       NA 13.73565     2.18
##            RDOR3.SA RAIL3.SA SBSP3.SA SAPR4.SA SAPR11.SA SANB11.SA STBP3.SA
## 2020-01-02       NA    26.40 58.02500 6.214901  30.62932  43.10525 7.907043
## 2020-01-03       NA    25.63 57.02837 5.774236  30.16658  43.07991 8.069581
## 2020-01-06       NA    24.90 56.15631 5.783353  29.65513  41.83958 7.964409
## 2020-01-07       NA    25.27 55.19802 5.920111  29.89563  41.75152 7.964409
## 2020-01-08       NA    25.60 54.38347 5.917073  30.01437  41.42567 7.830553
## 2020-01-09       NA    25.93 55.21718 5.901877  29.90781  40.64190 7.840115
##            SMTO3.SA SEQL3.SA SEER3.SA CSNA3.SA SIMH3.SA SQIA3.SA SLCE3.SA
## 2020-01-02 22.13625       NA 26.77324 13.96082       NA 24.57847 21.44816
## 2020-01-03 22.44984       NA 26.39615 13.85628       NA 24.45857 21.35738
## 2020-01-06 23.15083       NA 26.03792 13.51415       NA 24.97811 21.16758
## 2020-01-07 22.96636       NA 25.92480 13.51415       NA 25.07803 20.63117
## 2020-01-08 22.78189       NA 25.82109 13.49515       NA 25.06803 20.75495
## 2020-01-09 22.86490       NA 26.68840 13.35259       NA 24.21878 20.63117
##            SMFT3.SA SULA11.SA SUZB3.SA TAEE11.SA TASA4.SA TRAD3.SA TECN3.SA
## 2020-01-02       NA  52.69147 40.30898  24.73483     6.24       NA 3.320646
## 2020-01-03       NA  54.64647 40.17063  24.70275     6.34       NA 3.099269
## 2020-01-06       NA  54.86844 40.10146  24.85513     6.55       NA 3.012644
## 2020-01-07       NA  53.79276 40.61532  24.55838     6.33       NA 3.128145
## 2020-01-08       NA  53.40859 42.64115  24.30975     6.12       NA 3.060769
## 2020-01-09       NA  55.20993 42.53244  24.00498     6.29       NA 3.436146
##            VIVT3.SA TEND3.SA TIMS3.SA TOTS3.SA TRPL4.SA TRIS3.SA TPIS3.SA
## 2020-01-02 40.80102 30.76918 14.65090 22.63134 18.27799 14.86634 1.653212
## 2020-01-03 41.27467 30.80827 14.67877 23.11244 17.82045 15.48379 1.653212
## 2020-01-06 40.87714 30.84737 14.66019 22.58552 17.92480 15.41729 1.623514
## 2020-01-07 41.78214 31.81502 14.81812 22.68043 17.79636 15.74027 1.633413
## 2020-01-08 42.20505 31.57066 14.97606 22.37606 17.65990 15.67377 1.623514
## 2020-01-09 41.49458 31.71728 14.77167 22.74589 17.59568 15.58828 1.603715
##            TUPY3.SA UGPA3.SA UNIP6.SA USIM5.SA VALE3.SA VAMO3.SA VIIA3.SA
## 2020-01-02 24.82716 24.85781 28.64082 8.874281 44.33061       NA    11.73
## 2020-01-03 25.84571 24.56651 27.42675 8.782794 44.00422       NA    11.48
## 2020-01-06 24.79778 24.03245 28.11119 8.618116 43.74312       NA    11.48
## 2020-01-07 25.12097 24.56651 27.91563 8.700455 44.06134       NA    11.65
## 2020-01-08 25.12097 24.37231 28.92601 8.590671 44.06950       NA    11.60
## 2020-01-09 25.51272 24.42086 28.67342 8.535777 43.49017       NA    11.66
##            VBBR3.SA VIVA3.SA VIVR3.SA WEGE3.SA WIZS3.SA YDUQ3.SA
## 2020-01-02 26.86739 28.68686 2.342545 17.28075 12.20178 45.16428
## 2020-01-03 26.71007 28.50990 2.468941 17.06008 12.20178 44.21752
## 2020-01-06 25.37282 28.00851 2.805998 17.10422 12.86501 45.06767
## 2020-01-07 25.33786 27.42849 2.949247 17.23662 13.57926 44.22718
## 2020-01-08 25.21550 27.47764 2.848130 16.58442 12.66944 43.96634
## 2020-01-09 24.55124 27.92004 2.679601 16.33923 12.54190 44.35277

Next, having obtained the stock prices in a single organized dataframe, We will now treat the data to separate the momentum effect. For this, we will use the dplyr, tidyquant and tidyr packages to organize dataframes. We will change from wide format (stock prices arranged along columns) to long format (data stacked with the stock code label). We did this via the gather() function. We label the months (actually, the information is in daily frequency) to be able to define a moving window of 12 months excluding the current month.

We calculate the daily return discretely, as follows: $r_t=P_t/P_{t-1} $. The monthly return is the cumulative daily return for the data grouped into months. Remembering that the accumulated return is \(r_{m}=\prod_{t=1}^{t-d}\), where \(d\) is the number of working days in the month. For this we control using the group_by and ungroup() functions.

We grouped all 12-month moving window returns by date (and no longer by asset). That way we can take the whole sample and divide it into deciles, using the ntile(.,n=10) function. We filter only the top decile (greater than or equal to 10).

DF_Final <- as.data.table(Precos_Acoes) %>%
            gather("Codigo", "Preco", -index) %>%
            rename(Datas = index) %>%
            mutate(Mes_Ano = as.yearmon(Datas)) %>%
            ungroup() %>%
            group_by(Codigo) %>%
            mutate(Retorno_Diario =  (Preco - lag(Preco,1))/lag(Preco,1)) %>%
            group_by(Codigo,Mes_Ano) %>%
            mutate(Retorno_Mensal = Return.cumulative(Retorno_Diario)) %>%
            ungroup() %>%
            filter(Mes_Ano >= (Mes_Ano[length(Mes_Ano)] - 13/12)) %>%
            filter(Mes_Ano <= (Mes_Ano[length(Mes_Ano)] - 1/12)) %>%
            group_by(Codigo) %>%
          
    mutate(Retorno_Diario = replace_na(Retorno_Diario, replace = 0)) %>%
            mutate(Ret_Acumulado_Diario =cumprod(1+Retorno_Diario) -1) %>%          
            mutate(Retorno_12 = Return.cumulative(Retorno_Diario, geometric = TRUE)) %>%
            ungroup() %>%
            group_by(Datas) %>%
            mutate(Decil_Retorno=ntile(x = Retorno_12, n = 10)) %>%
            filter(Decil_Retorno >= 10)

head(DF_Final, 20)
## # A tibble: 20 x 9
## # Groups:   Datas [20]
##    Datas      Codigo   Preco Mes_Ano   Retorno_Diario Retorno_Mensal
##    <date>     <chr>    <dbl> <yearmon>          <dbl>          <dbl>
##  1 2021-01-04 AMBP3.SA  26.8 jan 2021         0.0163         -0.0454
##  2 2021-01-05 AMBP3.SA  26.5 jan 2021        -0.00968        -0.0454
##  3 2021-01-06 AMBP3.SA  26.7 jan 2021         0.00790        -0.0454
##  4 2021-01-07 AMBP3.SA  26.2 jan 2021        -0.0194         -0.0454
##  5 2021-01-08 AMBP3.SA  26.0 jan 2021        -0.00761        -0.0454
##  6 2021-01-11 AMBP3.SA  26.6 jan 2021         0.0238         -0.0454
##  7 2021-01-12 AMBP3.SA  26.9 jan 2021         0.00899        -0.0454
##  8 2021-01-13 AMBP3.SA  26.5 jan 2021        -0.0148         -0.0454
##  9 2021-01-14 AMBP3.SA  26.2 jan 2021        -0.00904        -0.0454
## 10 2021-01-15 AMBP3.SA  26.3 jan 2021         0.00342        -0.0454
## 11 2021-01-18 AMBP3.SA  26.1 jan 2021        -0.00644        -0.0454
## 12 2021-01-19 AMBP3.SA  26.9 jan 2021         0.0278         -0.0454
## 13 2021-01-20 AMBP3.SA  26.3 jan 2021        -0.0204         -0.0454
## 14 2021-01-21 AMBP3.SA  26.3 jan 2021         0              -0.0454
## 15 2021-01-22 AMBP3.SA  26.1 jan 2021        -0.00758        -0.0454
## 16 2021-01-26 AMBP3.SA  25.9 jan 2021        -0.00954        -0.0454
## 17 2021-01-27 AMBP3.SA  25.9 jan 2021         0.00193        -0.0454
## 18 2021-01-28 AMBP3.SA  25.7 jan 2021        -0.00769        -0.0454
## 19 2021-01-29 AMBP3.SA  25.1 jan 2021        -0.0225         -0.0454
## 20 2021-02-01 AMBP3.SA  24.9 fev 2021        -0.00833        -0.0845
## # ... with 3 more variables: Ret_Acumulado_Diario <dbl>, Retorno_12 <dbl>,
## #   Decil_Retorno <int>
tail(DF_Final, 20)
## # A tibble: 20 x 9
## # Groups:   Datas [20]
##    Datas      Codigo   Preco Mes_Ano   Retorno_Diario Retorno_Mensal
##    <date>     <chr>    <dbl> <yearmon>          <dbl>          <dbl>
##  1 2022-01-04 VAMO3.SA 10.8  jan 2022        -0.0441          0.0250
##  2 2022-01-05 VAMO3.SA 10    jan 2022        -0.0766          0.0250
##  3 2022-01-06 VAMO3.SA 10.5  jan 2022         0.049           0.0250
##  4 2022-01-07 VAMO3.SA 10.2  jan 2022        -0.0276          0.0250
##  5 2022-01-10 VAMO3.SA 10.1  jan 2022        -0.0137          0.0250
##  6 2022-01-11 VAMO3.SA 10.2  jan 2022         0.0159          0.0250
##  7 2022-01-12 VAMO3.SA 10.8  jan 2022         0.0577          0.0250
##  8 2022-01-13 VAMO3.SA 10.2  jan 2022        -0.0537          0.0250
##  9 2022-01-14 VAMO3.SA 10.2  jan 2022         0.00196         0.0250
## 10 2022-01-17 VAMO3.SA 10.2  jan 2022        -0.00488         0.0250
## 11 2022-01-18 VAMO3.SA  9.74 jan 2022        -0.0451          0.0250
## 12 2022-01-19 VAMO3.SA  9.99 jan 2022         0.0257          0.0250
## 13 2022-01-20 VAMO3.SA 11    jan 2022         0.101           0.0250
## 14 2022-01-21 VAMO3.SA 11.2  jan 2022         0.0164          0.0250
## 15 2022-01-24 VAMO3.SA 11.1  jan 2022        -0.00805         0.0250
## 16 2022-01-25 VAMO3.SA 11.5  jan 2022         0.0406          0.0250
## 17 2022-01-26 VAMO3.SA 12.0  jan 2022         0.0373          0.0250
## 18 2022-01-27 VAMO3.SA 11.9  jan 2022        -0.00501         0.0250
## 19 2022-01-28 VAMO3.SA 12.0  jan 2022         0.00588         0.0250
## 20 2022-01-31 VAMO3.SA 12.3  jan 2022         0.0250          0.0250
## # ... with 3 more variables: Ret_Acumulado_Diario <dbl>, Retorno_12 <dbl>,
## #   Decil_Retorno <int>

Now, let’s plot using the ggplot2 library.

### Plot
theme_set(theme_bw())
p <- ggplot(data = DF_Final, aes(y = Ret_Acumulado_Diario, x = Datas)) + 
     geom_line(aes(color = Codigo), size = 1.5) + 
     xlab('Time (days)') + 
     ylab('Daily Cumulative Return') + 
     ggtitle('Momentum Screen - B3') + 
     facet_wrap( ~ Codigo, scales = "free") +
     scale_x_date(date_breaks = "month", date_labels = "%b") +
     scale_y_continuous(labels = scales::percent) 


p + theme(legend.position = "none", 
          axis.text.x = element_text(angle = 90, 
                                     vjust = 0.5, 
                                     hjust=1))  

Júlio Fernando Costa Santos
Júlio Fernando Costa Santos
Professor of Economics

My research interests include Finance, Macroeconomics and Econometric techniques.

Related