How to: New order Binance API via RStudio

2019-08-06 19:00发布

问题:

I am trying to create a new order via the Binance API using RStudio.

I found the Binance Official API Docs and figured out that I should use

POST /api/v3/order (HMAC SHA256).

The following script doesn't work out for me:

url='https://api.binance.com/api/v3/account'

GET(url, 
    add_headers("X-MBX-APIKEY"= *[my API key]*),
    query=list("symbol"="ETHBTC", 
               "side"="BUY", 
               "type"="MARKET", 
               "quantity"=1, 
               recvWindow=5000, 
               "timestamp"=1499827319559, 
               "signature"=**???**), 
    verbose())

Does anyone know what I'm doing wrong and how I can create an order via the Binance API using RSTUDIO and how I can create my signature?

回答1:

library(httr)

timestamp <-
  as.character(jsonlite::fromJSON(content(
    GET("https://api.binance.com/api/v1/time"), "text"
  ))$serverTime + 999)

query <-
  list(
    "symbol" = "VENBTC",
    "side" = "BUY",
    "type" = "MARKET",
    "quantity" = 1,
    "recvWindow" = 5000,
    "timestamp" = timestamp
  )

signature <-
  digest::hmac(
    key = "*[my secret key]*",
    object = paste(names(query), query, sep = "=", collapse = "&"),
    algo = "sha256"
  )

POST(
  url,
  add_headers("X-MBX-APIKEY" = "*[my API key]*"),
  query = c(query, signature = signature),
  verbose()
)