R/cancel_order.R

Defines functions cancel_order

Documented in cancel_order

#' Cancel Pending Orders
#'
#' @name cancel_order
#'
#' @description This is an auth based function. User must have valid api keys generated by GDAX which must be passed as mandatory arguments. The users can cancel all pending orders except stop orders (GDAX does not treat STOP order as an open order unless the stop price is kicked in). User can now pass an optional order id to cancel a specific order including stop orders. Open orders can now be determined with \code{\link{open_orders}}. This function is a common call to delete one individual order or deleting all open orders.
#'
#'@param order_id Optional character value. This is the order id as generated by GDAX. Default value is "all" which will cancel all open orders.
#' @param api.key Mandatory character value. This is the API key as generated by GDAX. Typically a 32 character value.
#' @param secret Mandatory character value. This is the API secret as generated by GDAX. Typically a 88 character value.
#' @param passphrase Mandatory character value. This is the passphrase as generated by GDAX. Typically a 11 character value.
#'
#' @return  A Dataframe of order-ids of all orders that were cancelled.
#' @examples
#' \dontrun{
#' cancel_order(api.key = your_key,
#'              secret = your_api_secret,
#'              passphrase = your_api_pass)
#'
#' cancel_order(order_id = "a0a00000-0000-000a-a000-a0a0aa00000a",
#'              api.key = your_key,
#'              secret = your_api_secret,
#'              passphrase = your_api_pass)
#' }
#' @export

cancel_order <- function(order_id = "all",
                         api.key,
                         secret,
                         passphrase) {

  #get url extension----
  if (order_id == "all"){
    req.url <- "/orders/"
  } else {
    req.url <- paste0("/orders/",order_id)
  }


  #define method----
  method = "DELETE"

  #fetch response----
  response <-
    auth(
      method = method,
      req.url = req.url,
      api.key = api.key,
      secret = secret,
      passphrase = passphrase
    )

  #transform----
  response <- as.data.frame(response)
  if(!("message" %in% colnames(response))){
    print(paste0("Order id: ",response, " was successfully deleted"))
  } else {
    print(response$message)
  }

  #return----
  return(response)
}

Try the rgdax package in your browser

Any scripts or data that you put into this service are public.

rgdax documentation built on Aug. 3, 2021, 9:06 a.m.