# B3 Indexes

`rb3` comes with a diverse range of functions to explore the index delivered by the B3 Exchange. These functions will be presented here.

    library(rb3)
    library(ggplot2)
    library(dplyr)
    library(stringr)

## B3 Indexes

The function `rb3::indexes_get` list the names of available indexes.

    indexes_get()
    #>  [1] "AGFS" "BDRX" "GPTW" "IBOV" "IBRA" "IBXL" "IBXX" "ICO2" "ICON" "IDIV"
    #> [11] "IEEX" "IFIL" "IFIX" "IFNC" "IGCT" "IGCX" "IGNM" "IMAT" "IMOB" "INDX"
    #> [21] "ISEE" "ITAG" "IVBX" "MLCX" "SMLL" "UTIL"

## Indexes Composition and Weights

The composition of B3 indexes are available through the function `rb3::index_weights_get`. This function returns a data.frame with the current compostion of the requested index, all symbols that compound the index, their weights and theoretical position. Here the IBOVESPA (`IBOV`) Index has its composition listed.

    index_weights_get("IBOV")
    #> # A tibble: 89 × 3
    #>    symbol  weight   position
    #>    <chr>    <dbl>      <dbl>
    #>  1 ABEV3  0.0328  4386652506
    #>  2 ALPA4  0.00157  202765994
    #>  3 AMER3  0.00303  611169249
    #>  4 ARZZ3  0.00244   60758326
    #>  5 ASAI3  0.00937  935793199
    #>  6 AZUL4  0.00185  327646296
    #>  7 B3SA3  0.0399  5901731302
    #>  8 BBAS3  0.0254  1420731069
    #>  9 BBDC3  0.0104  1508883586
    #> 10 BBDC4  0.0402  5156077326
    #> # ℹ 79 more rows

The IBr100 Index (`IBXX`)

    index_weights_get("IBXX")
    #> # A tibble: 100 × 3
    #>    symbol  weight   position
    #>    <chr>    <dbl>      <dbl>
    #>  1 ABEV3  0.0301  4386652506
    #>  2 ALPA4  0.00145  202765994
    #>  3 AMER3  0.00279  611169249
    #>  4 ARZZ3  0.00225   60758326
    #>  5 ASAI3  0.00862  935793199
    #>  6 AURE3  0.0021   301966318
    #>  7 AZUL4  0.00171  327646296
    #>  8 B3SA3  0.0367  5901731302
    #>  9 BBAS3  0.0234  1420731069
    #> 10 BBDC3  0.00962 1508883586
    #> # ℹ 90 more rows

The Small Caps Index (`SMLL`)

    index_weights_get("SMLL")
    #> # A tibble: 129 × 3
    #>    symbol  weight  position
    #>    <chr>    <dbl>     <dbl>
    #>  1 AALR3  0.00326  42015455
    #>  2 ABCB4  0.00469  67562128
    #>  3 AERI3  0.00092 227070796
    #>  4 AESB3  0.0109  315209392
    #>  5 AGRO3  0.00637  59212546
    #>  6 ALPA4  0.0110  202765994
    #>  7 ALSO3  0.00748 122470927
    #>  8 ALUP11 0.0142  140050337
    #>  9 AMAR3  0.0007  156188662
    #> 10 AMBP3  0.00329  43853759
    #> # ℹ 119 more rows

### Index Composition

`rb3::index_comp_get` returns a vector with symbols that compound the given index.

    index_comp_get("SMLL")
    #>   [1] "AALR3"  "ABCB4"  "AERI3"  "AESB3"  "AGRO3"  "ALPA4"  "ALSO3"  "ALUP11"
    #>   [9] "AMAR3"  "AMBP3"  "AMER3"  "ANIM3"  "ARML3"  "ARZZ3"  "AZUL4"  "BEEF3"
    #>  [17] "BLAU3"  "BMOB3"  "BOAS3"  "BPAN4"  "BRAP4"  "BRFS3"  "BRML3"  "BRPR3"
    #>  [25] "BRSR6"  "CAML3"  "CASH3"  "CBAV3"  "CEAB3"  "CLSA3"  "COGN3"  "CSMG3"
    #>  [33] "CURY3"  "CVCB3"  "CYRE3"  "DASA3"  "DIRR3"  "DMMO3"  "DXCO3"  "ECOR3"
    #>  [41] "EMBR3"  "ENAT3"  "ENBR3"  "ENJU3"  "ESPA3"  "EVEN3"  "EZTC3"  "FESA4"
    #>  [49] "FHER3"  "FLRY3"  "GFSA3"  "GGPS3"  "GMAT3"  "GOAU4"  "GOLL4"  "GRND3"
    #>  [57] "GUAR3"  "HBSA3"  "IFCM3"  "IGTI11" "INTB3"  "JALL3"  "JHSF3"  "KEPL3"
    #>  [65] "LAVV3"  "LEVE3"  "LIGT3"  "LJQQ3"  "LOGG3"  "LOGN3"  "LWSA3"  "MATD3"
    #>  [73] "MBLY3"  "MDIA3"  "MEAL3"  "MEGA3"  "MILS3"  "MLAS3"  "MODL3"  "MOVI3"
    #>  [81] "MRFG3"  "MRVE3"  "MYPK3"  "ODPV3"  "ONCO3"  "ORVR3"  "PARD3"  "PCAR3"
    #>  [89] "PETZ3"  "PGMN3"  "PNVL3"  "POMO4"  "POSI3"  "PTBL3"  "QUAL3"  "RANI3"
    #>  [97] "RAPT4"  "RCSL3"  "RECV3"  "ROMI3"  "RRRP3"  "SAPR11" "SBFG3"  "SEER3"
    #> [105] "SEQL3"  "SIMH3"  "SLCE3"  "SMFT3"  "SMTO3"  "SOMA3"  "SQIA3"  "STBP3"
    #> [113] "SYNE3"  "TASA4"  "TEND3"  "TRAD3"  "TRIS3"  "TTEN3"  "TUPY3"  "UNIP6"
    #> [121] "USIM3"  "USIM5"  "VIIA3"  "VIVA3"  "VLID3"  "VULC3"  "WIZS3"  "YDUQ3"
    #> [129] "ZAMP3"

### Index by Segment

`rb3::index_by_segment_get` returns a data.frame with all stocks that are in the index, their economic segment, weights, position and segment weight in the index.

    index_by_segment_get("IBOV")
    #> # A tibble: 88 × 6
    #>    symbol segment                      weight segment_weight position refdate
    #>    <chr>  <chr>                         <dbl>          <dbl>    <dbl> <date>
    #>  1 WEGE3  Bens Indls / Máqs e Equips  0.0278         0.0278    1.32e9 2023-04-14
    #>  2 EMBR3  Bens Indls / Mat Transporte 0.00796        0.00796   7.35e8 2023-04-14
    #>  3 AZUL4  Bens Indls/Transporte       0.0021         0.0236    3.28e8 2023-04-14
    #>  4 CCRO3  Bens Indls/Transporte       0.00722        0.0236    9.98e8 2023-04-14
    #>  5 ECOR3  Bens Indls/Transporte       0.00104        0.0236    3.10e8 2023-04-14
    #>  6 GOLL4  Bens Indls/Transporte       0.00072        0.0236    1.90e8 2023-04-14
    #>  7 RAIL3  Bens Indls/Transporte       0.0125         0.0236    1.22e9 2023-04-14
    #>  8 BRFS3  Cons N  Básico / Alimentos… 0.00362        0.0185    1.08e9 2023-04-14
    #>  9 JBSS3  Cons N  Básico / Alimentos… 0.0103         0.0185    1.13e9 2023-04-14
    #> 10 MRFG3  Cons N  Básico / Alimentos… 0.00107        0.0185    3.17e8 2023-04-14
    #> # ℹ 78 more rows

## Indexes Time Series

`rb3` downloads data from B3 website to build time series for B3 indexes.

The function `rb3::index_get` downloads data from B3 for the given index name and returns data structured in a data.frame. The index names are obtained with `rb3::indexes_get` function.

    index_name <- "IBOV"
    index_data <- index_get(index_name, as.Date("2019-01-01"))
    head(index_data)
    #> # A tibble: 6 × 3
    #>   refdate    index_name   value
    #>   <date>     <chr>        <dbl>
    #> 1 2019-02-01 IBOV        97861.
    #> 2 2019-03-01 IBOV        94604.
    #> 3 2019-04-01 IBOV        96054.
    #> 4 2019-07-01 IBOV       101340.
    #> 5 2019-08-01 IBOV       102126.
    #> 6 2019-10-01 IBOV       104053.

The returned data.frame has three columns: `refdate`, `index_name` and `value`.

    index_data |>
      ggplot(aes(x = refdate, y = value)) +
      geom_line() +
      labs(
        x = NULL, y = "Index",
        title = str_glue("{index_name} Historical Data"),
        caption = str_glue("Data imported using rb3")
      )

The IBOVESPA index starts at 1968 and the series is adjusted for all economic events the that affected the Brazilian currency in the 80-90’s decades.

    index_data <- index_get(index_name, as.Date("1968-01-01"))
    index_data |>
      ggplot(aes(x = refdate, y = value)) +
      geom_line() +
      scale_y_log10() +
      labs(
        x = NULL, y = "Index (log scale)",
        title = str_glue("{index_name} Historical Data - since 1968"),
        caption = str_glue("Data imported using rb3")
      )

The y-axis was transformed to log scale in order to get the visualization improved.

Change `index_name` to get data for other indexes, for example, the Small Caps Index SMLL.

    index_name <- "SMLL"
    index_data <- index_get(index_name, as.Date("2010-01-01"))
    index_data |>
      ggplot(aes(x = refdate, y = value)) +
      geom_line() +
      labs(
        x = NULL, y = "Index",
        title = str_glue("{index_name} Historical Data"),
        caption = str_glue("Data imported using rb3")
      )

## Indexes Last Update

`rb3::indexes_last_update` returns the date where the indexes have been last updated.

    indexes_last_update()
    #> [1] "2023-04-14"