R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# ---- 2) Price behavior over time (2011-02-01 to 2024-02-01) ----
# Required libraries
library(knitr)
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
# Dates
start_date <- as.Date("2011-02-01")
end_date   <- as.Date("2024-02-01")

# ---- Get prices (Close) from Yahoo Finance ----
BIMBO <- get.hist.quote(
  instrument = "BIMBOA.MX", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

CEMEX <- get.hist.quote(
  instrument = "CEMEXCPO.MX", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

MSFT <- get.hist.quote(
  instrument = "MSFT", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

AMZN <- get.hist.quote(
  instrument = "AMZN", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

SHELL <- get.hist.quote(
  instrument = "SHELL.AS", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

ASML <- get.hist.quote(
  instrument = "ASML.AS", start = start_date, end = end_date,
  quote = "Close", provider = "yahoo", quiet = TRUE
)

# ---- Plot each company over time ----
par(cex = 0.7)

plot(BIMBO, xlab = "YEAR", ylab = "Closing", main = "Grupo Bimbo (BIMBOA.MX)")

plot(CEMEX, xlab = "YEAR", ylab = "Closing", main = "CEMEX (CEMEXCPO.MX)")

plot(MSFT,  xlab = "YEAR", ylab = "Closing", main = "Microsoft (MSFT)")

plot(AMZN,  xlab = "YEAR", ylab = "Closing", main = "Amazon (AMZN)")

plot(SHELL, xlab = "YEAR", ylab = "Closing", main = "Shell (SHELL.AS)")

plot(ASML,  xlab = "YEAR", ylab = "Closing", main = "ASML (ASML.AS)")

# ---- Create the portfolio matrix (aligned dates) ----
Portfolio <- merge(BIMBO, CEMEX, MSFT, AMZN, SHELL, ASML, all = FALSE)
names(Portfolio) <- c("BIMBO", "CEMEX", "MSFT", "AMZN", "SHELL", "ASML")

# Quick check (first/last rows)
round(head(Portfolio, n = 3), 2)
##            BIMBO CEMEX  MSFT AMZN SHELL  ASML
## 2011-02-01 26.00  8.75 27.99 8.61 26.49 41.18
## 2011-02-02 25.51  8.70 27.94 8.68 26.59 41.19
## 2011-02-03 25.25  8.82 27.65 8.69 26.00 40.70
round(tail(Portfolio, n = 3), 2)
##            BIMBO CEMEX   MSFT   AMZN SHELL  ASML
## 2024-01-29 79.54 14.32 409.72 161.26 29.10 804.8
## 2024-01-30 77.59 14.35 408.59 159.00 29.25 801.3
## 2024-01-31 78.26 14.27 397.58 155.20 29.07 798.2
# ---- Matrix of graphs for the six companies ----
plot(Portfolio, xlab = "YEAR", main = "My Portfolio Performance (Close Prices)")

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.