En este ejercicio se analizarán algunas acciones del mercado bursatil chileno. El siguiente código en lenguaje R permitirá obtener los precios de los instrumentos en sus diferentes etapas de transacción (“Open”, “Hight”, “Low”, “Close”), para posteriormente visualizar dichos resultados en un periodo de tiempo definido, utilizando principalmente library(dygraphs)
disponible en su web dygraphs.
library(quantmod)
library(dygraphs)
library(dplyr)
library(data.table)
library(xts)
library(htmlwidgets)
start <- as.Date("2014-01-01")
end <- as.Date("2020-05-05")
# banca
getSymbols("BCH", src = "yahoo", from = start, to = end)
getSymbols("BSANTANDER.SN", src = "yahoo", from = start, to = end)
getSymbols("ITCB", src = "yahoo", from = start, to = end)
# retail
getSymbols("CENCOSUD.SN", src = "yahoo", from = start, to = end)
getSymbols("FALABELLA.SN", src = "yahoo", from = start, to = end)
getSymbols("FORUS.SN", src = "yahoo", from = start, to = end)
getSymbols("SMU.SN", src = "yahoo", from = start, to = end)
# extraemos los precios de cierre
BCH.C <- BCH$BCH.Close
BSANTANDER <- BSANTANDER.SN$BSANTANDER.SN.Close
ITCB <- ITCB$ITCB.Close
CENCOSUD <- CENCOSUD.SN$CENCOSUD.SN.Close
FALABELLA <- FALABELLA.SN$FALABELLA.SN.Close
FORUS <- FORUS.SN$FORUS.SN.Close
SMU <- SMU.SN$SMU.SN.Close
retail <- cbind(CENCOSUD, FALABELLA, FORUS, SMU)
banca <- cbind(BCH.C, BSANTANDER, ITCB)
Luego ploteamos los datos en dos graficos diferentes, uno por cada grupo. Se indica un periodo de tiempo acotado en la escala de rangos, para un análisis en detalle.
dyg_banca <-
dygraph(banca,
main = "Acciones de la banca") %>%
dySeries("BSANTANDER.SN.Close", label = "Santander") %>%
dySeries("BCH.Close", label = "Banco.de.Chile") %>%
dySeries("ITCB.Close", label = "Itaú") %>%
dyOptions(gridLineColor = "white") %>%
dyRoller(rollPeriod = 7) %>%
dyRangeSelector(height = 20) %>%
dyRangeSelector(dateWindow = c("2018-01-01", "2020-05-05")) %>%
dyLegend(show = "follow")
dyg_banca
dyg_retail <-
dygraph(retail,
main = "Acciones del retail") %>%
dySeries("FALABELLA.SN.Close", label = "Falabella") %>%
dySeries("FORUS.SN.Close", label = "Forus") %>%
dySeries("CENCOSUD.SN.Close", label = "Cencosud") %>%
dySeries("SMU.SN.Close", label = "SMU") %>%
dyOptions(gridLineColor = "white") %>%
dyRoller(rollPeriod = 7) %>%
dyRangeSelector(height = 20) %>%
dyRangeSelector(dateWindow = c("2018-01-01", "2020-05-05")) %>%
dyLegend(show = "follow")
dyg_retail
Opcionalmente se puede analizar de forma individual cada acción en un grafico Candlestick, para esto tomé como ejemplo Forus y Banco de Chile.
FORUS_CDLSTK <- FORUS.SN[,c(1,2,3,4)]
names(FORUS_CDLSTK) <- c("Open", "Hight", "Low", "Close")
cdlstk_Forus <-
dygraph(FORUS_CDLSTK,
main = "Forus") %>%
dyOptions(gridLineColor = "white") %>%
dyRoller(rollPeriod = 7) %>%
dyRangeSelector(height = 20) %>%
dyRangeSelector(dateWindow = c("2020-03-05", "2020-05-05")) %>%
dyCandlestick()
cdlstk_Forus
BCH_CDLSTK <- BCH[,c(1,2,3,4)]
names(BCH_CDLSTK) <- c("Open", "Hight", "Low", "Close")
cdlstk_bch <-
dygraph(BCH_CDLSTK,
main = "Banco de Chile") %>%
dyOptions(gridLineColor = "white") %>%
dyRoller(rollPeriod = 7) %>%
dyRangeSelector(height = 20) %>%
dyRangeSelector(dateWindow = c("2020-03-05", "2020-05-05")) %>%
dyCandlestick()
cdlstk_bch
Por último guardamos todos los gráficos en formato html.
# save as html
saveWidget(dyg_banca,
"./html/stock_banca.html",
selfcontained = TRUE)
saveWidget(dyg_retail,
"./html/stock_retail.html",
selfcontained = TRUE)
saveWidget(cdlstk_Forus,
"./html/stock_forus.html",
selfcontained = TRUE)
saveWidget(cdlstk_bch,
"./html/stock_bch.html",
selfcontained = TRUE)
Versión de Rstudio y Packages:
## [1] "R version 3.6.3 (2020-02-29)"