Imagine que usted es un trabajador de una empresa minera que extrae litio (ZQL). Aunque a usted le gustaría estar modelando la demanda que tendrá la empresa con un sofísticado modelo de series de tiempo, su jefe insiste en que se necesita un panorama visual del contexto económico de la empresa. Para esto le pide que realice un pequeño reporte que contenga información del mercado en la que la empresa participa. Para esto, le recomienda obtener información del precio de las acciones de las siguientes empresas: Apple, Amazon, Tesla, Microsoft y Google de los ultimos 5 años (2018-2022)
Pregunta 1: Utilizando el paquete BatchGetSymbols, extraiga los datos del mercado accionario de las empresas que requiere y del precio del commodity que su empresa vende.
#Descargar datos de precio de acciones
library(BatchGetSymbols)
## Warning: package 'BatchGetSymbols' was built under R version 4.3.2
library(data.table)
#Filtramos por fecha:
fecha.ini <- as.Date("2018-1-1")
fecha.ter <- as.Date("2022-12-31")
frec <- "daily"
tickers <- c("AAPL","AMZN","TSLA","MSFT","GOOGL")
acciones <- BatchGetSymbols(tickers = tickers,
first.date = fecha.ini,
last.date = fecha.ter,
freq.data = frec,
cache.folder = file.path(tempdir(),
'BGS_Cache'))
## Warning: `BatchGetSymbols()` was deprecated in BatchGetSymbols 2.6.4.
## ℹ Please use `yfR::yf_get()` instead.
## ℹ 2022-05-01: Package BatchGetSymbols will soon be replaced by yfR. More
## details about the change is available at github
## <<www.github.com/msperlin/yfR> You can install yfR by executing:
##
## remotes::install_github('msperlin/yfR')
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
acc<-as.data.table(acciones$df.tickers)
acc<-acc[,.(ref.date,ticker,price.close)]
names(acc)<-c("fecha","ticker","precio")
litio<-fread(input = "preciolitio.csv")
litio[,fecha:=as.Date(fecha)]
litio[,precio:=precio/100]
datos<-rbind(acc,litio)
datos<-datos[fecha<as.Date("2022-12-31")]
Utilizando el paquete highcharter, realice un plot de todos los datos que descargó.
library(highcharter)
## Warning: package 'highcharter' was built under R version 4.3.2
serie_acc1 <-
hchart(datos,type ='line',hcaes(x=fecha,y=precio,group=factor(ticker),color=factor(ticker))) %>%
hc_xAxis(title = "none") %>%
hc_yAxis(title = "none") %>%
hc_legend(enabled = TRUE, verticalAlign = "top", align = "left", x = 40, y = 0) %>%
hc_tooltip(shared = FALSE,
formatter = JS("function () { return '<b>' + this.series.name + '</b><br/>' + Highcharts.numberFormat(Math.abs(this.point.y), 0);}"))
serie_acc1
#Cargamos librería:
library(quantmod)
## Warning: package 'quantmod' was built under R version 4.3.2
## Warning: package 'xts' was built under R version 4.3.2
## Warning: package 'zoo' was built under R version 4.3.2
## Warning: package 'TTR' was built under R version 4.3.2
#Obtenemos los datos:
amazon <-getSymbols(Symbols = "AMZN", auto.assign = FALSE)
#Genereamos gráficos:
stock1<-hchart(amazon)
stock1
# Cargamos libreria
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.2
#Transformamos bse de datos:
datos_w<-dcast(data = datos,formula = fecha~ticker,value.var = "precio")
# Eliminamos NA:
mcor<-cor(na.omit(datos_w[,.(LITIO,AAPL,AMZN,GOOGL,MSFT,TSLA)]))
#Generamos tabla de correlación:
cpplot<-corrplot(mcor,method="number")