Skip to content

Instantly share code, notes, and snippets.

@gsee
Last active September 18, 2019 20:30
Show Gist options
  • Save gsee/b20b3b9893cd74e462a8 to your computer and use it in GitHub Desktop.
Save gsee/b20b3b9893cd74e462a8 to your computer and use it in GitHub Desktop.
authenticate and place an order on Coinbase Exchange in R
library(RCurl) # for base64Decode(), base64Encode(), getURLContent(), and getCurlHandle()
library(digest) # for hmac()
library(RJSONIO) # for toJSON() and fromJSON()
api.key <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
secret <- "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
passphrase <- "your passphrase"
api.url <- "https://api.gdax.com"
req.url <- "/orders"
method <- "POST"
timestamp <- format(as.numeric(Sys.time()), digits=13)
key <- base64Decode(secret, mode="raw")
order <- list(price="1.0", size="0.01", side="buy", product_id="BTC-USD")
body <- toJSON(order, collapse="")
what <- paste0(timestamp, toupper(method), req.url, body)
sign <- base64Encode(hmac(key, what, algo="sha256", raw=TRUE))
# set headers
httpheader <- list('CB-ACCESS-KEY'=api.key,
'CB-ACCESS-SIGN'=sign,
'CB-ACCESS-TIMESTAMP'=timestamp,
'CB-ACCESS-PASSPHRASE'=passphrase,
'Content-Type'='application/json')
# place an order
(order <- fromJSON(getURLContent(url=paste0(api.url, req.url),
curl=getCurlHandle(useragent="R"),
httpheader=httpheader,
postfields=body)))
@Guceri
Copy link

Guceri commented Aug 24, 2017

Any idea how to cancel orders? I was playing around with the GET and POST successfully, but with cancelling orders it requires a DELETE method and I get a "Bad Request" for whatever reason. I have tried with and without the body as there are no required fields for deleting all orders.

@Bombax1864
Copy link

I copy this code one-to-one and enter my api key, my api secret and my passphrase, but I always get a 400 error. Any ideas?
I have no been trying for the entire weekend to get the GDAX API running in R. But no chance :(

@bryanhalloy
Copy link

Thank you for this.
Would you be able to provide an example of how to pass parameters for a GET? Specifically I am looking to get historical rates (candles) for GDAX using the API as you have done above, but I don't know how to pass in the necessary parameters. I'd appreciate your help! Thanks!
https://docs.gdax.com/?python#get-historic-rates

Param Description
start Start time in ISO 8601
end End time in ISO 8601
granularity Desired timeslice in seconds

@DheerajAgarwal
Copy link

DheerajAgarwal commented Dec 22, 2017

@bryanhalloy May be the below will help. This code is from my rgdax library where I am working on getting all the authenticated functions now. All my public functions are done and few authenticated ones are done too. I am working on remaining functions and ensuring correct and complete documentation. Hopefully I will be able to release it to CRAN by mid to end of Jan 2018.

    req.url <- paste0("/products/", product_id, "/candles")
    content <-
      parse_response(
        path = req.url,
        query = list(
          "start" = start,
          "end" = end,
          "granularity" = granularity
        )
      )

where parse_response is a function defined as:

parse_response <- function(path, query = NULL) {

  api.url <- "https://api.gdax.com"
  url <- modify_url(api.url, path = path, query = query)
  response <- GET(url = url)

  if (response$status_code != 200) {
    content <- fromJSON(content(response,
                                as = "text"))
    message <- content$message
    stop(message)
  } else {
    content <- fromJSON(content(response,
                                as = "text"))
  }
  return(content)
}

Let me know if you have questions or need further support. I will hopefully be able to release the code to github public repo as soon as I enhance to read the api secret and passphrase as arguments rather than in-session variable.

@DheerajAgarwal
Copy link

@Bombax1864 I was just able to place the order successfully using a very similar approach but using jsonlite and not RJSONIO. Let me know if you need any help and I can share my code with you.

@tsulgrove
Copy link

tsulgrove commented Dec 27, 2017

@DheerajAgarwal I am having a 400 error as well, comes up as an invalid signature. Could you post the code that successfully connected? Thanks

@DheerajAgarwal
Copy link

@tsulgrove Here is the link to my repo which is released under MIT. Please use at your own risk. I am currently testing it for CRAN release and hopefully CRAN will accept it without much issues. https://github.com/DheerajAgarwal/rgdax

@DheerajAgarwal
Copy link

rgdax is now available on cran. library(rgdax) should give you all the common functions needed.

@prateekji2016
Copy link

@DheerajAgarwal thanks for making the effort to put together this code. But the bad request error does not seem to go away. Can you please share the exact code you got working, without burying the message in details about rgdax?

@DheerajAgarwal
Copy link

hey @prateekji2016, my complete code with examples is available on CRAN as well as my repo. https://github.com/DheerajAgarwal/rgdax for some time. I believe posting code is only going to make it more confusing as the actual code continues to evolve. Hope you will find what you are looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment