#' Get price data by share class for a group of companies
#'
#' This function will use the simfin api to grab price data by share class.
#'
#' @param id_companies A vector of ids of companies
#' @param api_key Your api key
#' @param cache_folder The cache folder to save files
#'
#' @return A dataframe with price data by share class
#' @export
#'
#' @examples
#' \dontrun{
#' df <- simfinR_get_price_by_class(59265, 'YOURAPIKEY')
#' }
simfinR_get_price_by_class <- function(id_companies,
api_key,
cache_folder = 'simfin_cache') {
l_args <- list(id_sim = id_companies,
api_key = api_key,
cache_folder = cache_folder)
l_out <- purrr::pmap(.l = l_args, .f = simfinR_get_single_price_by_class)
df <- dplyr::bind_rows(l_out)
return(df)
}
#' Get price data by share class for single company
#'
#' Helper function of \code{\link{simfinR_get_price_by_class}}.
#' Will fetch data for a single company and save results in cache.
#'
#' @param id_sim The simfin id
#' @param api_key Your api key
#' @param cache_folder Cache folder to save files
#'
#' @return A dataframe with price data by share class
#' @export
#'
#' @examples
#' \dontrun{
#' df <- simfinR_get_single_price_by_class(59265, 'YOURAPIKEY')
#' }
simfinR_get_single_price_by_class = function(id_sim, api_key, cache_folder = 'simfin_cache'){
share_classes = simfinR_get_single_share_classes(id_sim, api_key)
# I don't expect this to happen but check to ensure no duplicate ids.
if(any(duplicated(share_classes$share_class_id))) stop(
'share_class_id is duplicated, this will cause duplication in joins.'
)
df = dplyr::bind_rows(lapply(
split(share_classes, 1:nrow(share_classes)),
function(share_class){
response = simfinR_get_single_price_for_class(
id_sim,
share_class$share_class_id,
api_key,
cache_folder
)
idt = NULL
if(length(response) > 0){
idt = response$priceData
idt$currency = response$currency
idt$share_class_id = share_class$share_class_id
}
return(idt)
}))
df$date = as.Date(df$date)
df = dplyr::rename(df, close_adj = closeAdj, split_coef = splitCoef, ref_date = date)
if(nrow(df) == 0) return(list())
df = dplyr::inner_join(
share_classes,
df,
by = 'share_class_id'
)
return(df)
}
#' Get price data by share class for single company and share class
#'
#' Helper function of \code{\link{simfinR_get_single_price_by_class}}.
#' Will fetch data for a single company and save results in cache.
#'
#' @param id_sim The simfin id
#' @param id_share_class The simfin share class id from simfinR_get_share_classes
#' @param api_key Your api key
#' @param cache_folder Cache folder to save files
#'
#' @return A dataframe with price data for the share class
#' @export
#'
#' @examples
#' \dontrun{
#' df <- simfinR_get_single_shares_outstanding_for_class(59265, 'YOURAPIKEY')
#' }
simfinR_get_single_price_for_class = function(id_sim, id_share_class, api_key, cache_folder = 'simfin_cache'){
message(paste0('Fetching prices for sim ', id_sim, ' and class ', id_share_class))
base_url <- sprintf('https://simfin.com/api/v1/companies/id/%s/shares/classes/%s/prices?api-key=%s',
id_sim,
id_share_class,
api_key)
# set memoise cache fct
mem_fromJSON <- simfinR_set_fromJSON(cache_folder = cache_folder)
l_out <- list()
try({
l_out <- mem_fromJSON(base_url)
}, silent = TRUE)
if (length(l_out) == 0) {
message('\t- Cant find data. Returns empty tibble')
return(dplyr::tibble())
}
return(l_out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.