You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多
文档控制台
免费开始使用

关于Gdax API及gdax-python库:能否以USD指定买卖金额?

Can I specify a buy amount in USD instead of cryptocurrency quantity using the GDAX API or gdax-python?

I'm working with the GDAX API and the gdax-python library, and I've found that the JSON request format for placing buy/sell orders looks like this:

# Place an order
order = {
 'size': 1.0,
 'price': 1.0,
 'side': 'buy',
 'product_id': 'BTC-USD',
}
r = requests.post(api_url + 'orders', json=order, auth=auth)

The size parameter specifies the quantity of cryptocurrency. I'm wondering if it's possible to specify the buy amount in USD instead of the coin quantity using this API or another method?


Short Answer

Absolutely, you can do this—though the GDAX API doesn't have a direct parameter for fiat (USD) amounts, you just need to run a quick calculation first to get the required crypto size.

Step-by-Step Implementation

  1. Fetch the current market price for your target product (like BTC-USD) using the API's ticker endpoint. With gdax-python, that's straightforward:
    import gdax
    # Initialize your authenticated client with your API credentials
    client = gdax.AuthenticatedClient(your_api_key, your_api_secret, your_passphrase)
    # Get the latest ticker data for the product
    ticker_data = client.get_product_ticker(product_id='BTC-USD')
    current_price = float(ticker_data['price'])
    
  2. Calculate the required crypto size from your desired USD amount. For example, if you want to spend $750 USD:
    desired_usd_spend = 750.0
    # Divide your target USD amount by the current price to get the crypto quantity
    calculated_size = desired_usd_spend / current_price
    # Round to the allowed decimal places (BTC supports 8 decimals, ETH uses 6)
    calculated_size = round(calculated_size, 8)
    
  3. Place your order with the computed size:
    order_details = {
        'size': calculated_size,
        'price': current_price,  # Include this for a limit order; omit for market orders
        'side': 'buy',
        'product_id': 'BTC-USD',
        'type': 'limit'  # Swap to 'market' if you want to fill at the best available price
    }
    order_response = client.place_order(**order_details)
    

Key Things to Keep in Mind

  • Price volatility: The market price can shift between when you fetch it and when your order processes. If you want to avoid missing out on the order, consider using a market order instead of a limit order.
  • Decimal precision: Every crypto product has specific decimal limits (check GDAX's product documentation). Rounding to the correct number of decimals will prevent your order from being rejected.
  • Fees: If you need your total spend (including fees) to match your desired USD amount, adjust your calculation to account for fees. For example, with a 0.1% fee rate:
    fee_rate = 0.001
    adjusted_usd_amount = desired_usd_spend / (1 + fee_rate)
    calculated_size = adjusted_usd_amount / current_price
    

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

火山引擎 最新活动