#' @title Asset
#' @import R6
#' @export
Asset <- R6Class(
classname = 'Asset',
public = list(
key_id = NULL,
name = NULL,
ticker = NULL,
data_source = NULL,
asset_class = NULL,
strategy = NULL,
benchmark = NULL,
vehicle = NULL,
geography = NULL,
country = NULL,
backfill = NULL,
freq = NULL,
is_return = NULL,
net_rf = NULL,
time_series = NULL,
initialize = function(key_id, dm) {
item <- dm$arctic_lib$read(key_id)
if (class(item$data)[1] == 'character') {
stop('data was not loaded')
}
if (is.null(item$metadata$is_return)) {
stop('is_price needs to be specified as TRUE or FALSE')
}
if (item$metadata$is_return) {
if (toupper(item$metadata$data_source) == 'TIINGO') {
time_series <- data.frame(item$data) %>%
mutate(date = as.Date(date, origin = '1970-01-01')) %>%
mutate(value = adjClose / lag(adjClose) - 1) %>%
select(date, value)
} else {
time_series <- NA
}
} else {
time_series <- NA
}
self$key_id <- item$metadata$key_id
self$name <- item$metadata$name
self$ticker <- item$metadata$ticker
self$data_source <- item$metadata$data_source
self$asset_class <- item$metadata$asset_class
self$strategy <- item$metadata$strategy
self$benchmark <- item$metadata$benchmark
self$vehicle <- item$metadata$vehicle
self$geography <- item$metadata$geography
self$country <- item$metadata$country
self$backfill <- item$metadata$backfill
self$freq <- item$metadata$freq
self$is_return <- item$metadata$is_return
self$net_rf <- item$metadata$net_rf
self$time_series <- time_series
},
changeFreq = function(freq = c('w', 'm', 'q', 'a'), is_month_end = TRUE) {
freq <- freq[1]
freq <- toupper(freq)
if (!freq %in% c('W', 'M', 'Q', 'A')) {
stop('freq must be \'W\', \'M\', \'Q\', \'A\'')
}
d <- self$time_series
if (self$is_return == TRUE) {
d <- d %>%
replace_na(list(value = 0)) %>%
mutate(value = cumprod(1 + value))
}
if (freq == 'W') {
d <- d %>%
mutate(xdate = paste0(lubridate::isoweek(date), '-',
lubridate::year(date)))
}
if (freq == 'M') {
d <- d %>%
mutate(xdate = paste0(lubridate::month(date), '-',
lubridate::year(date)))
}
if (freq == 'Q') {
d <- d %>%
mutate(xdate = paste0(lubridate::quarter(date), '-',
lubridate::year(date)))
}
if (freq == 'A') {
d <- d %>%
mutate(xdate = lubridate::year(date))
}
d <- d %>%
group_by(xdate) %>%
filter(date == max(date)) %>%
ungroup() %>%
select(-xdate)
if (self$is_return == TRUE) {
d <- d %>%
mutate(value = value / lag(value) - 1)
}
if (freq %in% c('M', 'Q', 'A') & is_month_end) {
d <- d %>%
mutate(date = lubridate::ceiling_date(date, unit = 'months') - 1)
}
self$time_series <- d
self$freq <- freq
}
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.