R/traverse_market_hierarchy.R

Defines functions slow_exec request_market_navigation traverse_market_hierarchy

Documented in request_market_navigation traverse_market_hierarchy

#' Traverse IG markets hierarchy
#'
#' @param node_id ID of the node from which to begin the traversal, see \href{https://labs.ig.com/rest-trading-api-reference/service-detail?id=544}{API documentation}
#' @param pause Due to api limits it may be necessary when traversing lots of nodes to introduce a pause to avoid exceeding said limits
#'
#' @return A dataframe containing markets details
#'
#' @examples
traverse_market_hierarchy <- function(node_id, pause = 0) {
  slow_mkt_nav <- slow_exec(pause, request_market_navigation)


  results <- slow_mkt_nav(node_id)

  ids <- results$nodes %>%
    purrr::map("id")

  if (!is.null(results$markets)) {
    return(dplyr::bind_rows(results$markets))
  } else {
    all_mkts <- purrr::map_df(ids, traverse_market_hierarchy, pause = pause)


    return(all_mkts)
  }
}

#' Request market node data
#'
#' @param node_id Numeric id for a particular node in the market hierarchy
#'
#' @return Node data, containing either IDs of children nodes or market information if its a leaf
#'
request_market_navigation <- function(node_id) {
  path <- glue::glue("marketnavigation/{node_id}")

  make_ig_request(path = path, api_version = 1) %>%
    httr::content()
}


slow_exec <- function(pause, f, ...) {
  function(...) {
    Sys.sleep(pause)

    f(...)
  }
}
zumthor86/OptionsAnalytics documentation built on Oct. 20, 2020, 1:15 p.m.