INTRODUÇÃO

Este relatório tem por objetivo organizar o estudo de otimização de portfólio:

  • Etapa 1: Selecionar Ativos;
  • Etapa 2:Otimizar o Portfólio;
  • Etapa 3:Realizar Análise de Desempenho e Risco.

ETAPA 1 - SELEÇÃO DE ATIVOS

Inicialmente, serão considerados alguns ativos, sem maior aprofundamento. São eles:

  • Ibovespa (^BVSP);
  • Minerva (BEEF3.SA);
  • Itaú (ITSA3.SA);
  • Prio (PRIO3.SA);
  • Weg (WEGE3.SA);
  • CSN (CMIN3.SA);
  • Renner (LREN3.SA);
  • Transmissora Aliança de Energia Elétrica (TAEE11.SA);
  • Banco do Brasil (BBAS3.SA);
  • Cemig (CMIG3.SA).

Para obtenção dos dados será utilizada a função getSymbols do pacote quantmod.

# PACOTES UTILIZADOS:
library(quantmod)

prazo <- 180

# IMPORTAÇÃO DOS DADOS:
ibovespa <- getSymbols(Symbols = '^BVSP',
                       from = Sys.Date()-prazo,
                       to = Sys.Date(),
                       auto.assign = FALSE)

minerva <- getSymbols(Symbols = 'BEEF3.SA',
                       from = Sys.Date()-prazo,
                       to = Sys.Date(),
                       auto.assign = FALSE)

itau <- getSymbols(Symbols = 'ITSA3.SA',
                   from = Sys.Date()-prazo,
                   to = Sys.Date(),
                   auto.assign = FALSE)

prio <- getSymbols(Symbols = 'PRIO3.SA',
                        from = Sys.Date()-prazo,
                        to = Sys.Date(),
                        auto.assign = FALSE)

weg <- getSymbols(Symbols = 'WEGE3.SA',
                     from = Sys.Date()-prazo,
                     to = Sys.Date(),
                     auto.assign = FALSE)

csn <- getSymbols(Symbols = 'CMIN3.SA',
                     from = Sys.Date()-prazo,
                     to = Sys.Date(),
                     auto.assign = FALSE)

renner <- getSymbols(Symbols = 'LREN3.SA',
                     from = Sys.Date()-prazo,
                     to = Sys.Date(),
                     auto.assign = FALSE)

taesa <- getSymbols(Symbols = 'TAEE11.SA',
                     from = Sys.Date()-prazo,
                     to = Sys.Date(),
                     auto.assign = FALSE)

brasil <- getSymbols(Symbols = 'BBAS3.SA',
                     from = Sys.Date()-prazo,
                     to = Sys.Date(),
                     auto.assign = FALSE)

cemig <- getSymbols(Symbols = 'CMIG3.SA',
                    from = Sys.Date()-prazo,
                    to = Sys.Date(),
                    auto.assign = FALSE)

O código acima “cria” objetos com os preços de abertura, fechamento, mais alto, mais baixo, ajustado e volume de operações para cada ação. O objeto é do tipo xts (série temporal). O próximo passo consiste em “juntar” o preço ajustado de todas os ativos em uma tabela única contendo uma coluna com as datas.

# PACOTES NECESSÁRIOS:
library(magrittr)
library(tibble)
library(janitor)
library(dplyr)

# ORGANIZANDO AS TABELAS:
tabela_ibovespa <- ibovespa %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, bvsp_adjusted) %>% 
  rename("data" = rowname)

tabela_minerva <- minerva %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, beef3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_itau <- itau %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, itsa3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_prio <- prio %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, prio3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_weg <- weg %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, wege3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_csn <- csn %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, cmin3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_renner <- renner %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, lren3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_taesa <- taesa %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, taee11_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_brasil <- brasil %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, bbas3_sa_adjusted) %>% 
  rename("data" = rowname)

tabela_cemig <- cemig %>% 
  as.data.frame() %>% # transforma em tabela 
  rownames_to_column() %>% # transforma a data da linha em coluna 
  clean_names() %>%  # altera o nome das colunas
  select(rowname, cmig3_sa_adjusted) %>% 
  rename("data" = rowname)

consolidado <- tabela_ibovespa %>% 
  bind_cols(tabela_minerva[2],
            tabela_itau[2],
            tabela_prio[2],
            tabela_weg[2],
            tabela_csn[2],
            tabela_renner[2],
            tabela_taesa[2],
            tabela_brasil[2],
            tabela_cemig[2])

consolidado <- tabela_ibovespa %>% 
  full_join(tabela_minerva, by = c('data' = 'data')) %>% 
  full_join(tabela_itau, by = c('data' = 'data')) %>% 
  full_join(tabela_prio, by = c('data' = 'data')) %>% 
  full_join(tabela_weg, by = c('data' = 'data')) %>% 
  full_join(tabela_csn, by = c('data' = 'data')) %>%
  full_join(tabela_renner, by = c('data' = 'data')) %>% 
  full_join(tabela_taesa, by = c('data' = 'data')) %>% 
  full_join(tabela_brasil, by = c('data' = 'data')) %>% 
  full_join(tabela_cemig, by = c('data' = 'data')) 


consolidado
##           data bvsp_adjusted beef3_sa_adjusted itsa3_sa_adjusted
## 1   2023-05-15        109029          9.490742          8.702969
## 2   2023-05-16        108194          9.196302          8.635727
## 3   2023-05-17        109460          8.931309          8.770209
## 4   2023-05-18        110108          9.549629          8.741394
## 5   2023-05-19        110745         10.010917          8.712574
## 6   2023-05-22        110213         10.099248          8.693364
## 7   2023-05-23        109929          9.971658          8.702969
## 8   2023-05-24        108800          9.549629          8.616516
## 9   2023-05-25        110054          9.618332          8.750998
## 10  2023-05-26        110906          9.745922          8.702969
## 11  2023-05-29        110333          9.539815          8.693364
## 12  2023-05-30        108967          9.657590          8.683758
## 13  2023-05-31        108335         10.030546          8.558878
## 14  2023-06-01        110565          9.952029          8.639331
## 15  2023-06-02        112558         10.167951          8.860851
## 16  2023-06-05        112696         10.226838          8.880115
## 17  2023-06-06        114610         10.501648          8.880115
## 18  2023-06-07        115488         10.462389          8.899377
## 19  2023-06-09        117019         10.805902          9.043848
## 20  2023-06-12        117336         10.747013          8.995690
## 21  2023-06-13        116743         10.913862          9.092006
## 22  2023-06-14        119069         10.943306          9.217211
## 23  2023-06-15        119221         11.031637          9.390577
## 24  2023-06-16        118758         10.953120          9.294262
## 25  2023-06-19        119858         11.178857          9.525416
## 26  2023-06-20        119622         10.992379          9.583203
## 27  2023-06-21        120420         10.972750          9.631361
## 28  2023-06-22        118934         10.462389          9.486891
## 29  2023-06-23        118977         10.001101          9.520412
## 30  2023-06-26        118243          9.873512          9.491178
## 31  2023-06-27        117523         10.266097          9.432711
## 32  2023-06-28        116681          9.932400          9.335265
## 33  2023-06-29        118383         10.187580          9.520412
## 34  2023-06-30        118087         10.432945          9.491178
## 35  2023-07-03        119673         10.226838          9.637346
## 36  2023-07-04        119076         10.059990          9.549644
## 37  2023-07-05        119549         10.305355          9.656835
## 38  2023-07-06        117426         10.246468          9.559389
## 39  2023-07-07        118898         10.324986          9.617856
## 40  2023-07-10        117942          9.991288          9.695813
## 41  2023-07-11        117220          9.598702          9.452199
## 42  2023-07-12        117666          9.569259          9.422966
## 43  2023-07-13        119264          9.814624          9.598367
## 44  2023-07-14        117711          9.696849          9.500922
## 45  2023-07-17        118219          9.687034          9.598367
## 46  2023-07-18        117841          9.480927          9.627601
## 47  2023-07-19        117552          9.539815          9.569134
## 48  2023-07-20        118083          9.657590          9.637346
## 49  2023-07-21        120217          9.853883          9.725046
## 50  2023-07-24        121342          9.422040          9.666580
## 51  2023-07-25        122008          9.166859          9.841982
## 52  2023-07-26        122560          9.323893          9.794260
## 53  2023-07-27        119990          9.372966          9.676727
## 54  2023-07-28        120187          9.598702          9.764877
## 55  2023-07-31        121943          9.902956          9.706111
## 56  2023-08-01        121248         10.030546          9.706111
## 57  2023-08-02        120859         10.050175          9.784465
## 58  2023-08-03        120586         10.344614          9.725700
## 59  2023-08-04        119508         10.501648          9.637550
## 60  2023-08-07        119380         10.423131          9.696317
## 61  2023-08-08        119090         10.413316          9.578785
## 62  2023-08-09        118409         10.550721          9.449340
## 63  2023-08-10        118350         10.854975          9.381079
## 64  2023-08-11        118065         10.609610          9.429837
## 65  2023-08-14        116810         10.285726          9.429837
## 66  2023-08-15        116171         10.240000          9.390830
## 67  2023-08-16        115592         10.440000          9.400582
## 68  2023-08-17        114982         10.600000          9.283562
## 69  2023-08-18        115409         10.660000          9.249152
## 70  2023-08-21        114429         10.570000          9.190240
## 71  2023-08-22        116156         11.200000          9.278608
## 72  2023-08-23        118135         10.960000          9.425888
## 73  2023-08-24        117026         10.720000          9.366977
## 74  2023-08-25        115837         10.460000          9.288427
## 75  2023-08-28        117121         10.900000          9.416070
## 76  2023-08-29        118404          8.910000          9.445525
## 77  2023-08-30        117535          8.600000          9.308064
## 78  2023-08-31        115742          8.400000          9.209878
## 79  2023-09-01        117893          8.560000          9.308064
## 80  2023-09-04        117777          8.950000          9.219697
## 81  2023-09-05        117331          8.990000          9.121511
## 82  2023-09-06        115985          8.730000          8.974231
## 83  2023-09-08        115313          8.700000          8.934958
## 84  2023-09-11        116883          8.560000          9.121511
## 85  2023-09-12        117968          8.600000          9.190240
## 86  2023-09-13        118176          8.710000          9.249152
## 87  2023-09-14        119392          8.670000          9.258971
## 88  2023-09-15        118758          8.580000          9.249152
## 89  2023-09-18        118288          8.650000          9.258971
## 90  2023-09-19        117846          8.620000          9.239333
## 91  2023-09-20        118695          8.580000          9.298246
## 92  2023-09-21        116145          8.270000          9.170604
## 93  2023-09-22        116009          8.240000          9.047812
## 94  2023-09-25        115925          8.160000          9.117411
## 95  2023-09-26        114193          8.140000          8.978213
## 96  2023-09-27        114327          8.150000          8.988155
## 97  2023-09-28        115731          8.150000          9.017983
## 98  2023-09-29        116565          8.090000          9.097525
## 99  2023-10-02        115057          8.230000          8.978213
## 100 2023-10-03        113419          8.130000          8.908614
## 101 2023-10-04        113607          8.150000          8.968271
## 102 2023-10-05        113284          8.070000          9.027926
## 103 2023-10-06        114170          8.130000          9.067697
## 104 2023-10-09        115156          8.100000          8.948385
## 105 2023-10-10        116737          8.160000          9.087583
## 106 2023-10-11        117051          8.130000          9.167124
## 107 2023-10-13        115754          7.930000          9.067697
## 108 2023-10-16        116534          7.910000          9.057754
## 109 2023-10-17        115908          7.800000          8.858901
## 110 2023-10-18        114060          7.710000          8.998098
## 111 2023-10-19        114004          7.460000          8.928499
## 112 2023-10-20        113155          7.430000          8.820000
## 113 2023-10-23        112785          7.380000          8.880000
## 114 2023-10-24        113762          7.510000          8.910000
## 115 2023-10-25        112830          7.370000          8.870000
## 116 2023-10-26        114777          7.600000          9.020000
## 117 2023-10-27        113301          7.610000          8.840000
## 118 2023-10-30        112532          7.550000          8.750000
## 119 2023-10-31        113144          7.570000          8.780000
## 120 2023-11-01        115053          7.600000          8.880000
## 121 2023-11-03        118160          7.790000          9.130000
## 122 2023-11-06        118487          8.050000          9.190000
## 123 2023-11-07        119268          8.050000          9.350000
## 124 2023-11-08        119099          7.880000          9.410000
## 125 2023-11-09        119012          6.820000          9.430000
## 126 2023-11-10        120636          6.840000          9.500000
##     prio3_sa_adjusted wege3_sa_adjusted cmin3_sa_adjusted lren3_sa_adjusted
## 1               34.47          39.36090              4.47          16.95712
## 2               34.50          39.42049              4.45          16.60507
## 3               36.50          39.18217              4.69          18.20885
## 4               36.35          39.02330              4.73          18.43378
## 5               36.97          39.84746              4.90          18.98141
## 6               36.19          40.07584              4.78          18.51201
## 7               36.16          39.93682              4.67          18.20885
## 8               35.95          39.53964              4.52          17.98393
## 9               35.63          38.76513              4.53          19.06942
## 10              35.35          39.23182              4.58          19.59750
## 11              35.12          38.60626              4.57          19.25523
## 12              34.13          38.07006              4.44          18.89340
## 13              34.19          37.34520              4.40          19.02053
## 14              34.17          36.92815              4.47          20.10602
## 15              35.32          37.40477              4.67          20.01800
## 16              35.35          37.36506              4.73          20.06690
## 17              34.28          37.93105              4.77          20.65365
## 18              34.79          37.88140              4.61          21.17195
## 19              34.48          37.53386              4.56          21.34797
## 20              33.14          37.48421              4.48          22.31611
## 21              34.34          37.08703              4.41          21.02526
## 22              35.40          37.60337              4.49          21.01548
## 23              35.60          37.57358              4.49          20.60476
## 24              36.27          37.58350              4.47          19.88110
## 25              36.06          37.40477              4.53          20.32116
## 26              36.16          36.68985              4.45          21.10349
## 27              38.67          36.19337              4.38          21.27952
## 28              38.30          36.14372              4.32          20.92747
## 29              38.21          36.34231              4.35          21.81738
## 30              37.04          36.20138              4.37          21.16217
## 31              35.80          36.30083              4.35          20.62432
## 32              36.14          36.38039              4.25          20.64422
## 33              36.29          36.63898              4.26          21.09793
## 34              37.08          37.54401              4.18          19.72692
## 35              37.42          37.48434              4.23          20.01296
## 36              38.04          36.98706              4.21          20.00309
## 37              38.62          37.29538              4.19          20.17077
## 38              38.15          36.68870              4.12          19.52965
## 39              39.35          36.79810              4.21          20.14118
## 40              39.49          36.09198              4.12          18.83920
## 41              40.70          35.75383              4.12          18.60248
## 42              41.55          35.86323              4.12          18.31644
## 43              42.89          35.86323              4.21          18.19808
## 44              41.52          35.80356              4.21          17.58654
## 45              41.99          35.10738              4.23          17.71477
## 46              43.18          35.74389              4.15          17.80354
## 47              44.14          37.70314              4.12          17.53723
## 48              44.20          37.95177              4.08          17.63586
## 49              45.17          38.67779              4.15          18.05013
## 50              46.17          41.46816              4.32          18.60248
## 51              44.96          40.42995              4.42          18.38548
## 52              45.59          40.40999              4.46          18.74057
## 53              45.09          40.28021              4.34          18.37562
## 54              45.16          40.25027              4.29          18.38548
## 55              45.62          39.85096              4.45          18.49398
## 56              45.80          39.79106              4.40          18.39535
## 57              45.25          39.96077              4.32          18.74057
## 58              47.16          40.36008              4.29          18.29671
## 59              47.57          40.77935              4.21          19.35210
## 60              47.81          40.85921              4.27          18.74057
## 61              47.86          40.58968              4.20          18.88852
## 62              48.12          40.70947              4.14          18.05013
## 63              47.38          40.17041              4.13          18.46439
## 64              48.36          39.73117              4.14          18.25726
## 65              48.59          39.24202              4.12          17.33010
## 66              46.82          39.28194              4.08          17.20187
## 67              45.98          37.54496              4.05          17.25119
## 68              45.38          36.84616              4.05          16.87638
## 69              45.17          36.19729              4.06          17.12296
## 70              44.75          35.97767              4.06          16.87638
## 71              45.10          36.40693              4.11          16.94542
## 72              45.85          36.38696              4.19          17.17228
## 73              46.06          35.91777              4.10          16.65938
## 74              46.39          36.29712              4.09          16.38320
## 75              46.69          36.55666              4.14          16.47198
## 76              45.85          36.42689              4.23          16.47198
## 77              45.81          36.23722              4.35          16.21552
## 78              46.42          35.81795              4.28          15.81112
## 79              46.95          35.88783              4.36          16.54102
## 80              46.61          36.06752              4.41          16.21552
## 81              47.49          35.84789              4.33          15.85058
## 82              46.79          35.48852              4.29          15.35740
## 83              47.49          35.33878              4.21          15.30809
## 84              47.71          35.21898              4.38          15.53495
## 85              48.11          35.58834              4.30          15.79140
## 86              48.20          36.86613              4.29          16.02812
## 87              49.25          36.01760              4.43          15.84071
## 88              49.40          36.03756              4.49          15.36727
## 89              48.16          35.72810              4.47          15.21932
## 90              48.71          35.91777              4.46          14.40065
## 91              48.71          35.72810              4.55          14.45983
## 92              46.44          34.91950              4.62          13.75952
## 93              46.76          34.53018              4.68          13.70034
## 94              46.98          36.13000              4.62          13.61157
## 95              46.93          36.08000              4.56          12.98031
## 96              48.30          36.02000              4.60          12.90000
## 97              47.24          36.39000              4.68          13.42000
## 98              47.04          36.27000              4.85          13.40000
## 99              45.40          36.15000              4.79          12.85000
## 100             45.17          35.17000              4.74          12.53000
## 101             43.90          35.16000              4.66          12.93000
## 102             43.71          34.98000              4.67          12.73000
## 103             44.54          34.80000              4.66          12.59000
## 104             48.45          34.85000              4.66          12.68000
## 105             48.25          35.30000              4.73          13.00000
## 106             47.60          35.17000              4.82          13.10000
## 107             50.00          34.54000              4.83          12.69000
## 108             49.80          34.65000              4.92          12.96000
## 109             50.20          34.28000              4.93          12.81000
## 110             50.20          34.29000              4.77          12.24000
## 111             50.03          33.94000              4.78          12.34000
## 112             49.70          33.81000              4.70          12.22000
## 113             48.84          33.90000              4.80          12.40000
## 114             48.85          35.01000              4.93          12.38000
## 115             48.36          31.47000              4.84          12.35000
## 116             48.00          32.66000              4.93          12.68000
## 117             47.77          31.63000              4.95          12.21000
## 118             46.36          32.30000              5.07          11.94000
## 119             47.70          33.01000              5.18          12.26000
## 120             48.07          33.08000              5.33          12.27000
## 121             48.20          33.53000              5.63          13.24000
## 122             48.01          33.21000              5.69          12.61000
## 123             46.82          32.78000              5.77          13.18000
## 124             45.48          32.45000              5.90          13.45000
## 125             44.73          32.41000              5.89          13.55000
## 126             46.47          33.17000              6.03          12.96000
##     taee11_sa_adjusted bbas3_sa_adjusted cmig3_sa_adjusted
## 1             36.23963          42.57689          19.34996
## 2             36.09339          41.35056          18.84023
## 3             36.16164          41.41762          18.50695
## 4             36.22988          41.38888          18.52655
## 5             36.15189          42.19366          18.67359
## 6             36.15189          41.63798          18.14426
## 7             36.31763          42.61522          17.83058
## 8             36.39563          42.73977          17.58552
## 9             36.67837          42.78767          17.62473
## 10            36.73687          42.53857          17.35027
## 11            36.91236          42.46192          17.28164
## 12            36.64912          42.82599          17.01698
## 13            35.99589          42.74935          16.79153
## 14            36.15189          42.86432          16.75232
## 15            36.00564          43.50061          17.07580
## 16            35.75214          43.77285          16.96797
## 17            36.42488          44.27844          17.27184
## 18            36.52237          44.25900          17.38947
## 19            37.05861          46.41750          18.14426
## 20            37.20485          48.09958          19.07549
## 21            36.70762          47.59894          18.42853
## 22            37.15611          47.83391          18.47754
## 23            37.05861          48.81290          18.18347
## 24            36.45412          48.45068          17.47769
## 25            36.86361          49.35135          17.77177
## 26            36.88311          49.34156          17.93841
## 27            36.90261          50.89817          18.36971
## 28            36.63937          49.92896          17.76197
## 29            37.09761          49.94854          18.52655
## 30            36.86361          50.06602          18.33234
## 31            36.52237          49.14577          18.58985
## 32            36.34688          48.54858          18.64927
## 33            36.23963          48.99891          19.04543
## 34            36.69787          48.36256          19.13457
## 35            36.57112          49.33177          18.73841
## 36            36.46387          49.01849          18.31253
## 37            36.19088          48.69542          18.02532
## 38            36.10313          47.98075          17.77772
## 39            36.23963          48.91080          17.79752
## 40            35.83014          48.01012          17.78762
## 41            35.56690          47.32483          17.90647
## 42            35.31341          46.56121          17.72820
## 43            35.31341          47.00175          18.09465
## 44            35.26466          46.68848          17.83714
## 45            35.49865          47.09966          17.99561
## 46            35.44991          47.25629          17.91637
## 47            35.28416          46.43394          17.39146
## 48            35.46941          46.94302          17.33204
## 49            35.69365          47.50105          17.34194
## 50            35.83014          46.57100          18.02532
## 51            35.33291          46.32624          18.13426
## 52            35.41091          46.71785          18.66908
## 53            35.19641          45.75843          18.18378
## 54            35.28416          46.28708          18.22340
## 55            35.09892          47.17797          18.26301
## 56            35.22567          46.36541          18.31253
## 57            35.29391          46.57100          18.72850
## 58            35.50840          46.64931          18.71860
## 59            35.58640          46.40457          18.77802
## 60            35.44016          46.32624          18.49081
## 61            35.31000          46.00318          18.76812
## 62            34.69000          45.74864          19.26332
## 63            34.88000          45.98360          19.53073
## 64            34.83000          46.48288          19.62977
## 65            34.82000          46.02275          19.70900
## 66            34.50000          45.77800          19.57034
## 67            34.45000          46.16961          19.70900
## 68            34.50000          46.60036          19.65948
## 69            34.72000          46.78637          19.08505
## 70            34.45000          46.61994          19.33265
## 71            34.50000          47.63164          19.70900
## 72            34.57000          47.84016          19.53073
## 73            34.90000          47.21460          18.84735
## 74            34.77000          46.59898          19.08505
## 75            34.73000          47.93945          19.11476
## 76            34.94000          48.20755          19.21380
## 77            34.81000          47.51249          19.66939
## 78            34.20000          46.76778          19.50102
## 79            34.91000          47.08552          19.57034
## 80            34.86000          46.93658          19.44159
## 81            34.30000          46.76778          19.17418
## 82            34.00000          46.45004          18.81764
## 83            34.25000          46.31102          18.85726
## 84            34.70000          47.04580          19.14447
## 85            34.78000          47.30000          19.37226
## 86            34.78000          47.26000          19.63967
## 87            34.92000          47.39000          19.80804
## 88            35.31000          47.55000          19.80804
## 89            35.15000          47.35000          19.80804
## 90            35.01000          47.28000          19.80804
## 91            35.09000          47.70000          19.71890
## 92            34.98000          46.61000          19.80804
## 93            34.93000          46.69000          19.50102
## 94            34.73000          46.42000          19.60006
## 95            34.47000          45.76000          19.68000
## 96            34.39000          46.16000          19.51000
## 97            34.55000          47.64000          19.50000
## 98            34.56000          47.18000          19.10000
## 99            34.50000          47.05000          18.89000
## 100           33.83000          46.45000          18.70000
## 101           34.25000          46.80000          19.31000
## 102           34.24000          46.94000          19.05000
## 103           34.14000          48.85000          18.80000
## 104           33.97000          48.59000          18.89000
## 105           34.16000          48.51000          18.63000
## 106           34.02000          48.97000          18.61000
## 107           33.50000          48.82000          17.98000
## 108           33.78000          49.81000          18.02000
## 109           33.46000          49.36000          17.16000
## 110           33.60000          49.12000          16.30000
## 111           33.77000          49.50000          16.36000
## 112           33.76000          49.34000          16.00000
## 113           33.89000          49.27000          15.92000
## 114           33.89000          49.04000          15.52000
## 115           33.81000          49.05000          15.28000
## 116           34.31000          50.07000          15.31000
## 117           33.96000          48.80000          14.94000
## 118           33.75000          48.46000          15.17000
## 119           33.79000          48.35000          15.40000
## 120           33.99000          48.40000          16.48000
## 121           34.73000          50.10000          16.83000
## 122           35.19000          50.45000          17.00000
## 123           35.80000          51.02000          17.01000
## 124           35.71000          51.67000          16.99000
## 125           35.65000          49.51000          17.45000
## 126           36.03000          50.24000          17.44000

ETAPA 2 - OTIMIZAÇÃO DE PORTFÓLIO

Nesta seção, o objeto “consolidado” será utilizado para otimização de portfólio. Em primeiro lugar, vamos transformar esta tabela em um objeto de série temporal (para tanto, será utilizada a função timeSeries do pacote com o mesmo nome). Em seguida, será utilizada a função returns do pacote timeSeries para cálculo dos retornos diários de cada uma das ações.

# PACOTES NECESSÁRIOS:
library(timeSeries)
## Carregando pacotes exigidos: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:zoo':
## 
##     time<-
library(fPortfolio)
## Carregando pacotes exigidos: fBasics
## 
## Attaching package: 'fBasics'
## The following object is masked from 'package:TTR':
## 
##     volatility
## Carregando pacotes exigidos: fAssets
consolidado_ts <- timeSeries(consolidado[,-1], consolidado[,1]) # Série temporal de preços ajustados
retornos_ts <- returns(consolidado_ts) # 

# CÁLCULO DO RISCO E RETORNO INDIVIDUAL (CADA AÇÃO)
retornos_acao <- basicStats(retornos_ts) %>% 
  as.data.frame() %>% 
  rownames_to_column() %>% 
  rename("estatistica" = rowname) %>% 
  dplyr::filter(estatistica == 'Mean' | estatistica == 'Stdev')

# CÁLCULO DO RISCO E RETORNO DO PORTFÓLIO
fronteira_eficiente <- portfolioFrontier(retornos_ts)
frontierPlot(fronteira_eficiente)
points(x = 0.015,
       y = - 0.00051,
       col = "Red",
       pch = 19,
       cex = 1.5)

points(x = 0.015,
       y = 0.0009,
       col = "Green",
       pch = 19,
       cex = 1.5)

frontierPlot(fronteira_eficiente,
             frontier = "upper")

VISUALIZAÇÃO INTERATIVA

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:timeSeries':
## 
##     filter
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(magrittr)

tabela_retorno <- retornos_ts %>% 
  as.data.frame() %>% 
  rownames_to_column() %>% 
  rename("data" = rowname) %>% 
  mutate(data = as.Date(data))

grafico <- tabela_retorno %>%
  ggplot() + 
  geom_line(mapping = aes(x = data, y = bvsp_adjusted)) + 
  geom_point(mapping = aes(x = data, y = bvsp_adjusted))
  
ggplotly(grafico)

ANÁLISE DA CARTEIRA DE MENOR VARIÂNCIA (MENOR RISCO)

Para identificar os principais “atributos” da carteira de menor variância (menor risco), será utilizada a função “minvariancePortfolio()” do pacote “fPortfolio”. Conforme exemplo apresentado no código abaixo:

# PACOTES UTILIZADOS:
library(fPortfolio)

carteira_min_risco <- minvariancePortfolio(retornos_ts)

frontierPlot(fronteira_eficiente,
             frontier = "upper")

minvariancePoints(fronteira_eficiente,
                  col = "green",
                  cex = 1.5,
                  pch = 19)

carteira_min_risco
## 
## Title:
##  MV Minimum Variance Portfolio 
##  Estimator:         covEstimator 
##  Solver:            solveRquadprog 
##  Optimize:          minRisk 
##  Constraints:       LongOnly 
## 
## Portfolio Weights:
##      bvsp_adjusted  beef3_sa_adjusted  itsa3_sa_adjusted  prio3_sa_adjusted 
##             0.0000             0.0168             0.1511             0.1093 
##  wege3_sa_adjusted  cmin3_sa_adjusted  lren3_sa_adjusted taee11_sa_adjusted 
##             0.0463             0.0057             0.0000             0.6537 
##  bbas3_sa_adjusted  cmig3_sa_adjusted 
##             0.0000             0.0172 
## 
## Covariance Risk Budgets:
##      bvsp_adjusted  beef3_sa_adjusted  itsa3_sa_adjusted  prio3_sa_adjusted 
##             0.0000             0.0168             0.1511             0.1093 
##  wege3_sa_adjusted  cmin3_sa_adjusted  lren3_sa_adjusted taee11_sa_adjusted 
##             0.0463             0.0057             0.0000             0.6537 
##  bbas3_sa_adjusted  cmig3_sa_adjusted 
##             0.0000             0.0172 
## 
## Target Returns and Risks:
##   mean    Cov   CVaR    VaR 
## 0.0002 0.0071 0.0131 0.0111 
## 
## Description:
##  Sat Nov 11 16:56:33 2023 by user: Pessoal
weightsPie(carteira_min_risco)

CARTEIRA ÓTIMA E ÍNDICE DE SHARPE

ÍNDICE SHARPE = RC - RL RC = RETORNO DA CARTEIRA RL = RETORNO LIVRE DE RISCO (RENDA FIXA)

SIGMA = RISCO DA CARTEIRA

frontierPlot(fronteira_eficiente)
sharpeRatioLines(fronteira_eficiente,
                 col = "blue",
                 cex = 1.5,
                 pch = 19)

carteira_otima <- tangencyPortfolio(retornos_ts)
weightsPie(carteira_otima)

ALTERANDO CONFIGURAÇÕES DE OTIMIZAÇÃO

configuracao_01 <- portfolioSpec(
  model = list(
        type = "MV",
        optimize = "minRisk",    
        estimator = "covEstimator",     
        tailRisk = list(),               
        params = list(alpha = 0.05, a = 2)),
    portfolio = list(
        weights = NULL, 
        targetReturn = NULL, 
        targetRisk = NULL,
        riskFreeRate = 0, 
        nFrontierPoints = 50,
        status = NA),
    optim = list(
        solver = "solveRshortExact",    
        objective = NULL,  
        parames = list(),
        control = list(meq = 2),   
        trace = FALSE)
  )

carteira_config_01 <- efficientPortfolio(retornos_ts,
                                         spec = configuracao_01)
## Warning in as.vector(invSigma %*% one)/(one %*% invSigma %*% one): Recycling array of length 1 in vector-array arithmetic is deprecated.
##   Use c() or as.vector() instead.
weightsPie(carteira_config_01)

frontierPlot(fronteira_eficiente)
points(x = frontierPoints(carteira_config_01)[,1],
       y = frontierPoints(carteira_config_01)[,2],
       col = "Orange",
       cex = 1.5,
       pch = 19)

VENDAS A DESCOBERTO

carteira_otima_vendido <- tangencyPortfolio(retornos_ts,
                                            spec = configuracao_01,
                                            constraints = 'short')

tailoredFrontierPlot(portfolioFrontier(retornos_ts,
                                       spec = configuracao_01,
                                       constraints = 'short'))
## Warning in as.vector(invSigma %*% one)/(one %*% invSigma %*% one): Recycling array of length 1 in vector-array arithmetic is deprecated.
##   Use c() or as.vector() instead.

## Warning in as.vector(invSigma %*% one)/(one %*% invSigma %*% one): Recycling array of length 1 in vector-array arithmetic is deprecated.
##   Use c() or as.vector() instead.

## Warning in as.vector(invSigma %*% one)/(one %*% invSigma %*% one): Recycling array of length 1 in vector-array arithmetic is deprecated.
##   Use c() or as.vector() instead.