R/AcctStore.R

#' @import R6
#' @import dplyr
#' @import readxl
#' @export
AcctStore <- R6::R6Class(
  classname = 'AcctSore',
  public = list(
    balance_sheet = NULL,     # via advent xlsx dump
    asset_class = NULL,       # asset classification of the CME Indexes
    cme = NULL,               # list of CMEs: mean vec, covar mat, and rf
    color_pal = NULL,         # brand color palettes
    ticker_dict = NULL,       # dictionary of tickers to cme indexes
    cme_dict = NULL,          # dictionary of cme indexes to asset classes
    port_code_dict = NULL,    # dictionary of port_code to mgp
    mgp_dict = NULL,          # dictionary of mgp to frontier file

    initialize = function(
      balance_sheet = NULL,
      asset_class = NULL,
      cme = NULL,
      color_pal = NULL,
      ticker_dict = NULL,
      port_code_dict = NULL,
      mgp_dict = NULL) {
      # create new instance of AcctStore
      if (is.null(balance_sheet)) {
        advent_file_name <- self$latestAdventfile()
        balance_sheet <- readxl::read_excel(advent_file_name)
      }
      if (is.null(asset_class)) {
        asset_class <- readxl::read_excel(
          path = 'X:/Client-Security-Data/Dictionaries/CME.xlsx'
        ) %>%
          mutate(`CME ID` = standardCmeId(`Provider ID`),
                 `Meta Asset Class` = factor(`Meta Asset Class`),
                 `Asset Class` = factor(`Asset Class`, levels = unique(`Asset Class`)),
                 `Sub-Asset Class` = factor(`Sub-Asset Class`, levels = unique(`Sub-Asset Class`))) %>%
          select(-`Provider ID`)
      }
      if (is.null(cme)) {
        cme <- lapply(
          X  = 1:3,
          FUN = readxl::read_excel,
          path = 'X:/Client-Security-Data/Dictionaries/CME-Num.xlsx'
        )
        cme[[1]] <- cme[[1]] %>%
          mutate(`Provider ID` = standardCmeId(`CME ID`))
      }
      if (is.null(color_pal)) {
        color_pal <- readxl::read_excel(
          path = 'X:/Client-Security-Data/Dictionaries/Colors.xlsx',
          sheet = 'Sheet2'
        )
      }
      if (is.null(ticker_dict)) {
        ticker_dict <- readxl::read_excel(
          path = 'X:/Client-Security-Data/Dictionaries/Ticker.xlsx'
        ) %>%
          mutate(`CME ID` = standardCmeId(`CME ID`))
      }
      if (is.null(port_code_dict)) {
        port_code_dict <- readxl::read_excel(
          path = 'X:/Client-Security-Data/Dictionaries/Port-Code.xlsx'
        )
      }
      if (is.null(mgp_dict)) {
        mgp_dict <- readxl::read_excel(
          path = 'X:/Client-Security-Data/Dictionaries/MGP.xlsx'
        )
      }
      self$asset_class <- asset_class
      self$balance_sheet <- balance_sheet
      self$cme <- cme
      self$color_pal <- color_pal
      self$ticker_dict <- ticker_dict
      self$port_code_dict <- port_code_dict
      self$mgp_dict <- mgp_dict
    },

    latestAdventfile = function(
      fpath = 'X:/Client-Security-Data/Advent-Data-Files/',
      ext = '.xlsx') {
      # get most recent advent xlsx dump based on file name
      # fpath = file path containing advent xlsx dumps
      # ext = file extension of advent dumps (.xlsx by default)
      all_files <- list.files(fpath, pattern = paste0('*', ext)) %>%
        gsub(pattern = ext, replacement = '') %>%
        gsub(pattern = '~$', replacement = '')
      date_vec <- as.Date(all_files) %>%
        sort(decreasing = TRUE)
      return(paste0(fpath, date_vec[1], ext))
    }
  )
)
alejandro-sotolongo/InvTools documentation built on Nov. 1, 2019, 9:08 p.m.