Global ETF Map

What and How

Tickers Import

tickers <-  c("ECH", "EDEN", "EFNL", "EIDO", "EIRL", "EIS", "ENOR", "ENZL", 
"EPHE", "EPOL", "EPU", "ERUS", "EWA", "EWC", "EWD", "EWG", "EWH", 
"EWI", "EWJ", "EWK", "EWL", "EWM", "EWN", "EWO", "EWP", "EWQ", 
"EWS", "EWT", "EWU", "EWW", "EWY", "EWZ", "EZA", "FXI", "ICOL", 
"INDA", "KSA", "QAT", "THD", "TUR", "UAE")

name <-   c("Chile", "Denmark", "Finland", "Indonesia", "Ireland", "Israel", 
"Norway", "New Zealand", "Philippines", "Poland", "Peru", "Russia", 
"Australia", "Canada", "Sweden", "Germany", "Hong Kong", "Italy", 
"Japan", "Belgium", "Switzerland", "Malaysia", "Netherlands", 
"Austria", "Spain", "France", "Singapore", "Taiwan", "United Kingdom", 
"Mexico", "Korea", "Brazil", "South Africa", "China", "Colombia", 
"India", "Saudi Arabia", "Qatar", "Thailand", "Turkey", "United Arab Emirates"
)

ytd <-   c(15.82, 17.85, 14.29, 8.8, 13.53, 11.41, 1.51, 7.18, 11.79, 
32.2, 3.45, -5.95, 9.03, -0.13, 16.2, 12.45, 17.26, 9.78, 5.98, 
12.02, 14.13, 13.18, 17.36, 18.18, 21.76, 13.93, 16.61, 15.6, 
7.17, 16.58, 18.3, 12.93, 10.41, 9.18, 5.51, 19.69, -1.29, -2.26, 
7.52, 21.51, 2.85)


etf_ticker_country <- data_frame(tickers, name, ytd)

etf_ticker_country
## # A tibble: 41 × 3
##    tickers        name   ytd
##      <chr>       <chr> <dbl>
## 1      ECH       Chile 15.82
## 2     EDEN     Denmark 17.85
## 3     EFNL     Finland 14.29
## 4     EIDO   Indonesia  8.80
## 5     EIRL     Ireland 13.53
## 6      EIS      Israel 11.41
## 7     ENOR      Norway  1.51
## 8     ENZL New Zealand  7.18
## 9     EPHE Philippines 11.79
## 10    EPOL      Poland 32.20
## # ... with 31 more rows

Test those symbols

# getSymbols is part of the 'quantmod' package.

library(quantmod)
library(purrr)

# Using getSymbols to import the ETF price histories will take a minute or two or 
# five - 42 time series is a lot of data. 

# Let select just the Adjusted prices of the ETFs and merge them into a list.
# We'll use map and the piper operator for that purpose. Again, this is for testing. It's not 
# going into production in our app.

etf_prices <- 
  getSymbols(etf_ticker_country$tickers, auto.assign = TRUE, warnings = FALSE) %>%
  # Let's use the map function to get just the Adjusted prices
  map(~Ad(get(.))) %>% 
  # Now use the reduce() function to combine to one xts object
  reduce(merge) %>% 
  # some name cleanup
  `colnames<-`(etf_ticker_country$name)

# Take a peek at the last 5 rows of each of the time series, 
# just to make sure it looks complete.

tail(etf_prices, n = 5)

Load simple features dataframe

library(rnaturalearth)

world <- ne_countries(type = "countries", returnclass = 'sf')

head(world[c("name", "gdp_md_est", "economy")], n = 6)
## Simple feature collection with 6 features and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -73.41544 ymin: -55.25 xmax: 75.15803 ymax: 42.68825
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##                   name gdp_md_est                   economy
## 0          Afghanistan      22270 7. Least developed region
## 1               Angola     110300 7. Least developed region
## 2              Albania      21810      6. Developing region
## 3 United Arab Emirates     184300      6. Developing region
## 4            Argentina     573900   5. Emerging region: G20
## 5              Armenia      18770      6. Developing region
##                         geometry
## 0 MULTIPOLYGON(((61.210817091...
## 1 MULTIPOLYGON(((16.326528354...
## 2 MULTIPOLYGON(((20.590247430...
## 3 MULTIPOLYGON(((51.579518670...
## 4 MULTIPOLYGON(((-65.5 -55.2,...
## 5 MULTIPOLYGON(((43.582745802...

Leaflet: Palette and Popup

gdpPal <- colorQuantile("Blues", world$gdp_md_est, n = 20)


economyPopup <- paste0("<strong>Country: </strong>", 
                world$name, 
                "<br><strong>Market Stage: </strong>", 
                 world$economy)

leaf_world_economy <- leaflet(world) %>%
  addProviderTiles("CartoDB.Positron") %>% 
  setView(lng =  20, lat =  15, zoom = 2) %>%
      addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = .7, color =
      ~gdpPal(gdp_md_est), layerId = ~name, popup = economyPopup)

leaf_world_economy

Add ETF data to World Map dataframe

library(sf)

world_etf <- merge(world, etf_ticker_country, by = "name")

Map with new data

ytdPal <- colorQuantile("RdYlGn", world_etf$ytd, n = 20)

ytdPopup <- paste0("<strong>Country: </strong>", 
                world_etf$name,
                "<br><strong> Year-to-date: </strong>", 
                world_etf$ytd, "%")

leaf_world_etf <- leaflet(world_etf) %>%
  addProviderTiles("CartoDB.Positron") %>% 
  setView(lng =  20, lat =  15, zoom = 2) %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = .7,
              color =~ytdPal(ytd), layerId = ~tickers, popup = ytdPopup)

leaf_world_etf

Save the leaflet map object

save(leaf_world_etf, file = 'etfData.RDat')

Load the .RDat and build the map

# YAML header
title: "Global ETF Map"
runtime: shiny
output:
 flexdashboard::flex_dashboard:
  source_code: embed

# Load the data from the Notebook

load('etfData.RDat')


output$map1 <- renderLeaflet({
    leaf_world_etf
})

Get the ticker when user clicks

clickedCountry <- eventReactive(input$map1_shape_click, {
     return(input$map1_shape_click$id)
     })

Pass the ticker to quantmod

etf <- getSymbols(as.character(clickedCountry()), auto.assign = FALSE)

dygraph(Ad(etf), main = clickedCountry())