R/factorEksponeringNOFond.R

# library(tidyverse)
# library(readxl)
# library(xml2)
# library(here)
# 
# fund.benchmarks.and.expense.ratios.url <-
#   "https://www.oslobors.no/ob_eng/obnewsletter/download/bed6b75e013cf16b9921b1078730528f/file/file/omff_xmlbasicsubscription.xml"
# 
# "https://www.oslobors.no/obnewsletter/download/bed6b75e013cf16b9921b1078730528f/file/file/omff_xmlbasicsubscription.xml"
# AQR.BAB.url <- "https://images.aqr.com/-/media/AQR/Documents/Insights/Data-Sets/Betting-Against-Beta-Equity-Factors-Monthly.xlsx"
# AQR.QMJ.url <- "https://images.aqr.com/-/media/AQR/Documents/Insights/Data-Sets/Quality-Minus-Junk-Factors-Monthly.xlsx"
# fund.metadata <- fund.benchmarks.and.expense.ratios.url %>% read_xml
# 
# flatten.fund.data <- function(x)
#   x %>% xml_children %>% xml_text %>%
#   matrix(dimnames=x %>% xml_children %>% xml_name %>% list) %>% t %>%
#   as_tibble
# 
# funds <- fund.metadata %>% xml_find_all('omff') %>% xml_children
# fund.metadata <-
#   lapply(funds, flatten.fund.data) %>%
#   bind_rows
# 
# fund.metadata %>%
#   filter(rootgroupname == "Fond", security_name == "Delphi Norge") %>%
#   t
# 
# #fund.metadata %>%
# #  group_by(rootgroupname, securitytypename, groupname) %>%
# #  tally %>% arrange(n %>% desc) %>% head(20)
# 
# fund.return.url <- "https://www.oslobors.no/obnewsletter/download/3b6220967fd0c586174f9efd505fc780/file/file/monthly-returns_1_4.txt"
# fund.return.data <-  # run once again if SSL_ERROR_SYSCALL
#   fund.return.url %>%
#   read_tsv(locale=locale("nb", encoding="iso-8859-1"))
# 
# colnames(fund.return.data) <-
#   c("id", "ticker", "name", "isin", "nr", "category",
#     colnames(fund.return.data)[7:ncol(fund.return.data)])
# fund.return.data <-
#   fund.return.data %>%
#   left_join(
#     fund.metadata %>%
#       select(
#         ticker=symbol,
#         root_category=rootgroupname,
#         benchmark_ticker=benchmarksymbol,
#         expense.ratio=totcost
#       ),
#     by="ticker"
#   ) %>%
#   gather(date, ret, -id, -ticker, -benchmark_ticker, -name, -isin, -nr, -root_category, -category, -expense.ratio) %>%
#   filter(!is.na(ret)) %>%
#   mutate(
#     date =
#       date %>%
#       paste0("01") %>%
#       as.Date("%Y%m%d") %>%
#       lubridate::ceiling_date("month") - 1
#   )
# 
# 
# fund.return.data.wide <-
#   fund.return.data %>%
#   filter(
#     (
#       grepl("Delphi", name) &
#         root_category == "Fond"
#     ) |
#       ticker %in% c("OSEFX", "UNMSTHEWOI")
#   ) %>%
#   select(name, date, ret) %>%
#   spread(name, ret)
# fund.return.data.long <-
#   fund.return.data.wide %>%
#   gather(fund, ret, -date) %>%
#   filter(!is.na(ret)) %>%
#   group_by(fund) %>%
#   mutate(cum.ret = cumsum(ret)) %>%
#   ungroup
# 
# fund.return.data.long %>%
#   ggplot(aes(date, cum.ret*1e2, colour=fund)) +
#   geom_line() +
#   theme_minimal() +
#   labs(x="", y="%", title="Delphi Funds\nAbsolute cumulative return before fees") +
#   scale_colour_brewer(palette="Set3")
# 
# return.data.wide %>%
#   select(contains("Norge"), contains("Oslo")) %>%
#   cor(use="pairwise") %>%
#   round(2)
# 
# AQR.BAB.url <- "https://images.aqr.com/-/media/AQR/Documents/Insights/Data-Sets/Betting-Against-Beta-Equity-Factors-Monthly.xlsx"
# AQR.QMJ.url <- "https://images.aqr.com/-/media/AQR/Documents/Insights/Data-Sets/Quality-Minus-Junk-Factors-Monthly.xlsx"
# 
# utils::download.file(url=AQR.BAB.url, destfil=here("AQR_BAB.xlsx"), mode = "wb", method="libcurl")
# AQR.BAB.data <- read_excel(here("AQR_BAB.xlsx"))
# 
# utils::download.file(url=AQR.QMJ.url, destfil=here("AQR_QMJ.xlsx"), mode = "wb", method="libcurl")
# AQR.QMJ.data <- read_excel(here("AQR_QMJ.xlsx"))
# 
# MKT <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="MKT", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, MKT.Global=Global, MKT.Norge=NOR)
# SMB <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="SMB", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, SMB.Global=Global, SMB.Norge=NOR)
# HML.FF <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="HML FF", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, HML.FF.Global=Global, HML.FF.Norge=NOR)
# HML.Devil <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="HML Devil", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, HML.Devil.Global=Global, HML.Devil.Norge=NOR)
# UMD <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="UMD", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, UMD.Global=Global, UMD.Norge=NOR)
# RF <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="RF", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, RF=`Risk Free Rate`)
# BAB <- ("AQR_BAB.xlsx" %>% here) %>%
#   read_excel(sheet="BAB Factors", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, BAB.Global=Global, BAB.Norge=NOR)
# QMJ <- ("AQR_QMJ.xlsx" %>% here) %>%
#   read_excel(sheet="QMJ Factors", skip=18) %>%
#   mutate(date=as.Date(DATE, "%m/%d/%Y")) %>%
#   select(date, QMJ.Global=Global, QMJ.Norge=NOR)
# 
# return.data.wide <-
#   Reduce(
#     function(x, y) merge(x, y, by="date"),
#     list(fund.return.data.wide, MKT, SMB, HML.Devil, HML.FF, UMD, BAB, QMJ, RF)
#   )
# 
# return.data.wide %>%
#   select(contains("Norge"), contains("Oslo")) %>%
#   cor(use="pairwise") %>%
#   round(2)
NorquantDev/smartETF documentation built on July 7, 2019, 8:21 p.m.