knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(magrittr) library(lubridate) library(DT) library(Rblpapi) blpConnect() getData <- function(ticker){ Rblpapi::bdh(ticker, c("PX_LAST"), start.date = Sys.Date()-365) } getBarData <- function(ticker){ #Get 3 days worth of data, starting at 9am my_data <- getBars(ticker, startTime = ymd(Sys.Date() - 3) + hm("8, 0"), endTime = ymd(Sys.Date()) + hm("8, 0")) my_data %<>% select(date = times, PX_LAST = close) %>% filter(PX_LAST != 0) %>% as_tibble() my_data } chart_subtitle <- function(data){ paste("Last done: ", format(data$PX_LAST[length(data$PX_LAST)], big.mark = ","), " Change: ", format(round(data$PX_LAST[length(data$PX_LAST)] - data$PX_LAST[length(data$PX_LAST) - 1], 3), big.mark = ","), " Low: ", format(min(data$PX_LAST), big.mark = ","), " High: ", format(max(data$PX_LAST), big.mark = ",") ) } ggNTS <- function(ticker, title = ticker){ #Get normal (non-intraday) time series data <- getData(ticker = ticker) ggplot2::ggplot(data, ggplot2::aes(x = date, y = PX_LAST)) + ggplot2::geom_line(colour = "navyblue") + ggplot2::labs(title = title, subtitle = chart_subtitle(data = data), y = "Price", x = "Date") } ggITS <- function(ticker, title = ticker){ #Get intraday time series data <- getBarData(ticker = ticker) ggplot2::ggplot(data, ggplot2::aes(x = date, y = PX_LAST)) + ggplot2::geom_line(colour = "navyblue") + ggplot2::labs(title = title, subtitle = chart_subtitle(data = data), y = "Price", x = "Date") } ggTS <- function(ticker, title = ticker){ #normal_chart <- ggNTS(ticker, title = title) ggNTS(ticker, title = title) #intraday_chart <- ggITS(ticker, title = title) #gridExtra::grid.arrange(normal_chart, intraday_chart, # layout_matrix = matrix(c(1, 1, 2), ncol = 3)) } get_country_rates <- function(country_code, country_name){ tenors <- c("2Y", "5Y", "10Y", "30Y") bbg_ticker_list <- paste0("CT", country_code, tenors, " Corp") yields <- bdp(bbg_ticker_list, fields = "YLD_YTM_MID") rownames(yields) <- tenors colnames(yields) <- country_name yields }
eur_yields <- cbind(get_country_rates("DEM", "Germany"), get_country_rates("GBP", "United Kingdom"), get_country_rates("FRF", "France"), get_country_rates("ESP", "Spain"), get_country_rates("ITL", "Italy"), get_country_rates("PTE", "Portugal") ) %>% format(digits = 3, big.mark = ",") key_tenors <- c("2Y", "5Y", "10Y", "30Y") #Change factor order to change order in chart country_factors <- rev(c("Germany", "France", "United Kingdom", "Spain", "Italy", "Portugal")) eur_yields %>% rownames_to_column("Tenor") %>% gather("Country", "Yield", -Tenor) %>% select(Country, Tenor, Yield) %>% mutate(Country = factor(Country, levels = country_factors), Tenor = factor(Tenor, levels = key_tenors), Yield = as.numeric(Yield)) %>% ggplot(aes(x = Tenor, y = Country, fill = Yield)) + geom_tile() + geom_text(aes(label = Yield)) + scale_fill_gradient2(low = "red", mid = "white", high = "green")
ggTS("GTDEM10Y Govt", title = "German Govt 10Y") ggTS("DEYC2Y10 Index", title = "German Sell 2Y Buy 10Y") ggTS("DEYC1030 Index", title = "German Sell 10Y Buy 30Y") ggTS("GTGBP10Y Govt", title = "UK Govt 10Y")
ggTS("EURUSD Curncy") ggTS("GBPUSD Curncy") ggTS("GBPEUR Curncy") ggTS("DXY Index", title = "US Dollar Index") ggTS("USDJPY Curncy")
All charts are price return (instead of total returns).
ggTS("SPX Index") ggTS("INDU Index", title = "Dow Jones Industrial Average") ggTS("CCMP Index", title = "NASDAQ Composite Index") ggTS("SX5E Index", title = "EuroStoxx 50") ggTS("E300 Index", title = "EuroFirst 300") ggTS("UKX Index", title = "FTSE 100") ggTS("ASX Index", title = "FTSE All-Share") ggTS("NKY Index", title = "Nikkei 225") ggTS("HSI Index")
ggTS("XAU Curncy", title = "Spot Gold") ggTS("CO1 Comdty", title = "Brent generic 1st contract") ggTS("HG1 Comdty", title = "Copper generic 1st contract") ggTS("LN1 Comdty", title = "Nickel generic 1st contract") ggTS("XBTUSD COIN Curncy", title = "BitCoin")
ggTS("VIX Index")
#TODO Add team positions
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.