---
title: "Data Visualization: Stock valuation with {highchart} and {quantmod}"
author: "Rubén F. Bustillo"
output:
flexdashboard::flex_dashboard:
orientation: columns
source_code: embed
vertical_layout: fill
theme: cerulean
---
```{r setup, include=FALSE}
# PACKAGES / LIBRARIES:
library(flexdashboard)
library(tidyverse)
library(highcharter)
library(quantmod)
library(lubridate)
```
Sheet A. Overview
==================================================================================
Column {data-width=150}
-----------------------------------------------------------------------
### ibex{.value-box}
```{r}
ibex <- getSymbols("^IBEX", auto.assign = FALSE)
ibex_last <- ibex %>%
tail(n=1)
valueBox(value = format(ibex_last$IBEX.Close, big.mark= ","),
caption = "Ibex",
color = "salmon")
```
### eu50{.value-box}
```{r}
eu50 <- getSymbols("^STOXX50E", auto.assign = FALSE)
eu50_last <- eu50 %>%
tail(n=1)
valueBox(value = format(eu50_last$STOXX50E.Close, big.mark= ","),
caption = "Euro Stoxx 50",
color = "lightskyblue")
```
### eudol{.value-box}
```{r}
eudol <- getSymbols("EURUSD=X", auto.assign = FALSE)
eudol_last <- eudol %>%
tail(n=1)
valueBox(value = format(eudol_last$'EURUSD=X.Close', big.mark= ","),
caption = "EUR/USD",
color = "limegreen")
```
### brent{.value-box}
```{r}
brent <- getSymbols("BZ=F", auto.assign = FALSE)
brent_last <- brent %>%
tail(n=1)
valueBox(value = format(brent_last$'BZ=F.Close', big.mark= ","),
caption = "Brent Crude Oil",
color = "orange")
```
### Gold{.value-box}
```{r}
gold <- getSymbols("GC=F", auto.assign = FALSE)
gold_last <- gold %>%
tail(n=1)
valueBox(value = format(gold_last$'GC=F.Close', big.mark= ","),
caption = "Gold Jun 20",
color = "plum")
```
### Bitcoin{.value-box}
```{r}
bitcoin <- getSymbols("BTC-EUR", auto.assign = FALSE)
bitcoin_last <- bitcoin %>%
tail(n=1)
valueBox(value = format(bitcoin_last$'BTC-EUR.Close', big.mark= ","),
caption = "Bitcoin EUR",
color = "pink")
```
### FTSE100{.value-box}
```{r}
ftse100 <- getSymbols("^FTSE", auto.assign = FALSE)
ftse100_last <- ftse100 %>%
tail(n=1)
valueBox(value = format(ftse100_last$'FTSE.Close', big.mark= ","),
caption = "FTSE100",
color = "lightblue")
```
### S&P500{.value-box}
```{r}
syp500 <- getSymbols("^GSPC", auto.assign = FALSE)
syp500_last <- syp500 %>%
tail(n=1)
valueBox(value = format(syp500_last$'GSPC.Close', big.mark= ","),
caption = "S&P 500",
color = "cyan")
```
column
-----------------------------------------------------------------------
```{r}
hchart(ibex, color = "red") %>%
hc_title(text = "IBEX 35") %>%
hc_yAxis_multiples(
create_yaxis(1, turnopposite = T)
) %>%
hc_add_theme(hc_theme_economist()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
```{r}
#Euro Stoxx 50
hchart(eu50, color = "blue")%>%
hc_title(text = "Euro Stoxx 50") %>%
hc_yAxis_multiples(
create_yaxis(1, turnopposite = T)
) %>%
hc_add_theme(hc_theme_538()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
column
-----------------------------------------------------------------------
```{r}
eudol <- getSymbols("EURUSD=X", auto.assign = FALSE)
hchart(eudol, color = "green") %>%
hc_title(text = "EURO / USD") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
```{r}
hchart(brent, color = "orange") %>%
hc_title(text = "Brent Crude Oil") %>%
hc_add_theme(hc_theme_darkunica()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
Sheet B. Comparison | Time Series
======================================================================
Column
---------------------------------------------------------------
### Spanish Banks
```{r}
santander <- getSymbols("SAN.MC", auto.assign = FALSE)
sabadell <- getSymbols("SAB.MC", auto.assign = FALSE)
bbva <- getSymbols("BBVA.MC", auto.assign = FALSE)
caixa <- getSymbols("CABK.MC", auto.assign = FALSE)
bankia <- getSymbols("BKIA.MC", auto.assign = FALSE)
bankinter <- getSymbols("BKT.MC", auto.assign = FALSE)
highchart(type = "stock") %>%
hc_title (text = "Stock Valuation of some of the most important Spanish Banks") %>%
hc_subtitle (text = "Banks: Banco de Santander (SAN), Banco de Sabadell (SAB), Banco Bilbao Vizcaya Argentaria (BBVA), Caixa Bank (CABK), Bankinter (BKT)") %>%
hc_add_series(santander, color = "red") %>%
hc_add_series(sabadell, color = "blue") %>%
hc_add_series(bbva, color = "darkblue") %>%
hc_add_series(caixa, color = "grey" ) %>%
#hc_add_series(bankia, type = "ohlc") %>%
hc_add_series(bankinter, color = "orange") %>%
hc_add_theme(hc_theme_ffx()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
Column
----------------------------------------------------------------
### Time Series
```{r}
eurusd <- getSymbols("EUR/USD", src = "oanda", auto.assign = FALSE)
eurgbp <- getSymbols("EUR/GBP", src = "oanda", auto.assign = FALSE)
highchart(type = "stock") %>%
hc_title(text = "Exchange Rate EUR/USD & EUR/GBP") %>%
hc_subtitle(text = "Data from Oanda Corporation") %>%
hc_add_series(eurusd, id = "eurusd",
color = "orange",
name = "EUR/USD") %>%
hc_add_series(eurgbp, id = "eurgbp",
color = "grey",
name = "EUR/GBP") %>%
hc_yAxis(title = list(text = "")) %>%
hc_add_theme(hc_theme_elementary()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```
Sheet C. Bankia
========================================================================
```{r}
data_flags <- tibble(
date = ymd(c("2012-05-07",
"2012-05-09",
"2012-05-25",
"2012-05-28",
"2012-06-09",
"2012-06-09",
"2012-06-27",
"2013-01-02",
"2013-12-23",
"2015-07-07")),
title = sprintf("Note #%s", seq_along(date)),
text = c("Rodrigo Rato stepped down as chairman of Bankia SA. José Ignacio Goirigolzarri became the new president.",
"Banco Financiero y de Ahorros (BFA) was taken over by the Bank of Spain. De facto nationalization of Bankia",
"Trading in shares in Bankia suspended by the CVMV",
"Spanish risk premium hit a euro-area record (514 basic points).
At night the company reported the biggest loss in Spain´s banking history",
"Eurozone finance ministers agreed that Spanish banks would be provided with up to 100 billion euros of rescue loans.",
"Minister of economy announced that Spain had asked and obtained a rescue loan that would be used to rehabilitate the financial system",
"The value of the group estimated at -13.63bill. Bankia was worth less than nothing",
"Bankia removed from Spain´s Ibex stock index",
"Bankia returned to Ibex 35",
"Bankia paid its first dividend (1,75 cents per share"))
bankia.SMA.5 <- SMA(Cl(bankia), n = 5)
bankia.SMA.10 <- SMA(Cl(bankia), n = 10)
bankia.SMA.100 <- SMA(Cl(bankia), n = 100)
bankia.RSI.14 <- RSI(Cl(bankia))
bankia.RSI.SellLevel <- xts(rep(70, NROW(bankia)), index(bankia))
bankia.RSI.BuyLevel <- xts(rep(30, NROW(bankia)), index(bankia))
highchart(type = "stock") %>%
hc_yAxis_multiples(
create_yaxis(3, height = c(2, 1, 1))
) %>%
hc_add_series(bankia.SMA.5, yAxis = 0, name = "Moving Av.: 5 periods", color = "grey") %>%
hc_add_series(bankia.SMA.100, yAxis = 0, name = "Moving Av.: 100 periods" , color = "lightskyblue") %>%
hc_add_series(bankia$BKIA.MC.Volume, color = "lightgrey", yAxis = 1, name = "Volume", type = "column") %>%
hc_add_series(bankia.RSI.14, yAxis = 2, name = "Osciallator", color = "blue") %>%
hc_title (text = "Bankia. Stock Valuation") %>%
hc_add_series(bankia,
id = "bankia",
color = "red",
name = "Bankia") %>%
hc_add_series(data_flags, hcaes(x = date),
type = "flags", onSeries = "bankia")%>%
hc_add_series(bankia.RSI.SellLevel, color = "limegreen",
yAxis = 2, name = "Sell") %>%
hc_add_series(bankia.RSI.BuyLevel, color = "limegreen",
yAxis = 2, name = "Buy") %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_credits(enabled = TRUE, text = "quantmod.com",
href = "https://www.quantmod.com/")
```