#' @import R6
#' @import readxl
#' @import dplyr
#' @export
TickStore <- R6::R6Class(
classname = 'TickStore',
public = list(
data_lib = 'DATACENTRAL', # data library, DataCentral is default
meta = NULL, # data.frame containing metadata, default is to read DataCentral xlsx
data_store_path = NULL, # path to read data time-series, default assumes txt files in Data Dump Files
initialize = function(
data_lib = 'DATACENTRAL',
meta = NULL,
data_store_path = NULL) {
if (data_lib == 'DATACENTRAL') {
if (is.null(meta)) {
sht_vec <- readxl::excel_sheets(
path = 'Q:/TEST/Data Source Excel File/Forefront Data Central.xlsx'
)
meta <- lapply(
X = sht_vec,
FUN = readxl::read_excel,
path = 'Q:/TEST/Data Source Excel File/Forefront Data Central.xlsx'
) %>%
do.call(what = 'bind_rows') %>%
rename(
'name' = 'Name',
'forefront_id' = 'ForefrontID',
'provider_id' = 'ProviderID',
'data_provider' = 'DataProvider',
'asset_type' = 'AssetType',
'asset_class' = 'AssetClass',
'country' = 'Country',
'currency' = 'Currency',
'maturity' = 'Maturity',
'duration' = 'Duration',
'benchmark' = 'Benchmark',
'cme_asset' = 'CMEAsset',
'mult' = 'Multiplier',
'rf_net' = 'RiskFreeNet',
'auto_correl' = 'Smooth',
'data_type' = 'DataType',
'freq' = 'Frequency',
'constr_fnc' = 'ConstructFunction',
'ext_meth' = 'ExtendMethod',
'state' = 'State',
'mgr_name' = 'ManagerName',
'notice_period' = 'NoticePeriod',
'liq' = 'Liquidity',
'lock_up' = 'LockUp',
'mgmt_fee' = 'ManagementFee',
'perf_fee' = 'PerformanceFee',
'main_strat' = 'MainStrategy',
'sub_strat' = 'SubStrategy',
'off_bench' = 'OfficialBenchmark',
'aum' = 'AUM',
'aum_date' = 'AUMDate',
'contact_name' = 'ContactName',
'contact_num' = 'ContactNumber',
'contact_email' = 'ContactEmail',
'mgr_address' = 'ManagerAddress',
'auditor' = 'Auditor',
'admin' = 'Administrator',
'custodian' = 'Custodian',
'pb' = 'PrimeBroker',
'legal' = 'LegalConsultant'
)
}
if (is.null(data_store_path)) {
date_vec <- list.files('Q:/TEST/Data Dump Files') %>%
sub(pattern = 'Data Files_', replacement = '') %>%
as.Date(format = '%Y%m%d') %>%
sort(decreasing = TRUE)
data_store_path <- paste0(
'Q:/TEST/Data Dump Files/Data Files_',
format(date_vec[1], '%Y%m%d'),
'/'
)
}
}
# next data library type goes here
self$meta <- meta
self$data_store_path <- data_store_path
},
getItem = function(key_id) {
if (is.null(self$data_lib)) {
stop('must set data_lib')
}
if (is.null(self$meta)) {
stop('must set meta')
}
if (is.null(self$data_store_path)) {
stop('must set data_store_path')
}
if (toupper(self$data_lib) == 'DATACENTRAL') {
meta_datum <- self$meta %>%
filter(toupper(provider_id) == toupper(key_id)) %>%
filter(!is.na(provider_id))
if (nrow(meta_datum) == 0) {
stop(paste0(key_id, ' not found.'))
}
if (nrow(meta_datum) > 1) {
warning('Multiple instances of ', key_id, ' found. Attempting to
retrieve data for first instance.')
}
file_name <- paste0(
self$data_store_path,
meta_datum$forefront_id[1],
'.txt'
)
time_series <- read.table(file_name)
colnames(time_series) <- c('date', 'value')
time_series$date <- as.Date(time_series$date, origin = '0000-01-01') - 1
}
# next library type goes here
item <- list()
item$meta <- meta_datum
item$time_series <- time_series
return(item)
},
genAsset = function(key_id, check_meta = TRUE, check_ts = TRUE) {
item <- self$getItem(key_id)
if (check_ts) {
item <- self$checkTimeSeriesGap(item)
}
asset <- Asset$new(
meta = item$meta,
time_series = item$time_series,
check_meta = check_meta
)
return(asset)
},
checkTimeSeriesGap = function(item) {
gap <- switch(toupper(item$meta$freq),
D = 10,
W = 15,
M = 36,
Q = 108)
date_delta <- item$time_series$date - lag(item$time_series$date, 1)
gap_exists <- date_delta > gap
gap_exists[1] <- FALSE
if (sum(gap_exists) > 0) {
first_gap <- max(gap_exists * 1:length(date_delta))
item$time_series <- item$time_series[first_gap:nrow(item$time_series), ]
}
return(item)
}
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.