You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过RStudio调用Binance API创建新订单?

Fixing Binance API Order Creation in RStudio

Let's break down where you went wrong and walk through a working solution to create orders via the Binance API in R:

Key Issues in Your Current Script

  • Wrong API Endpoint: You're using /api/v3/account (for fetching account details) instead of the correct endpoint for creating orders: POST /api/v3/order
  • Incorrect Request Method: Binance requires a POST request for order creation, not GET
  • Stale Timestamp: The timestamp must be a current Unix timestamp in milliseconds, not a hardcoded old value
  • Missing Signature Generation: You didn't implement the HMAC SHA256 signing logic required for authenticated endpoints

Working R Script for Binance Order Creation

First, make sure you have the required packages installed:

install.packages(c("httr", "digest"))

Then use this script as a template:

library(httr)
library(digest)

# Replace these with your actual Binance API credentials
api_key <- "YOUR_API_KEY_HERE"
api_secret <- "YOUR_API_SECRET_HERE"

# API endpoint for creating orders
url <- "https://api.binance.com/api/v3/order"

# Generate current Unix timestamp in milliseconds
timestamp <- as.integer(Sys.time() * 1000)

# Define order parameters (adjust these as needed)
order_params <- list(
  symbol = "ETHBTC",
  side = "BUY",
  type = "MARKET",
  quantity = 0.001, # Note: Use a valid minimum quantity for the pair
  recvWindow = 5000,
  timestamp = timestamp
)

# Generate the signature: Convert params to URL query string, then HMAC SHA256 with secret key
query_string <- paste(names(order_params), order_params, sep = "=", collapse = "&")
signature <- hmac(api_secret, query_string, algo = "sha256")

# Add the signature to your params
order_params$signature <- signature

# Send the POST request
response <- POST(
  url = url,
  add_headers("X-MBX-APIKEY" = api_key),
  body = order_params,
  encode = "form"
)

# Check the response
content(response)

Important Notes

  • Test First: Use Binance's Testnet (https://testnet.binance.vision/api/v3/order) before using the mainnet to avoid accidental trades
  • Quantity Validation: Ensure your quantity matches the minimum tradable size for the symbol (check Binance's trading rules for each pair)
  • Signature Logic: The signature is generated from the URL-encoded query string of all parameters except the API key. The HMAC SHA256 hash must be in lowercase hex format (the digest::hmac function handles this by default)
  • Timestamp Sync: If you get "timestamp expired" errors, make sure your system clock is synchronized (Binance allows a small window via recvWindow, but it's best to have accurate time)

内容的提问来源于stack exchange,提问作者Bart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:45:06