以太坊历史价格-Coinbase API

2024-05-14 10:44:02 发布

您现在位置:Python中文网/ 问答频道 /正文

使用python coinbase API——函数--get_buy_priceget_sell_priceget_spot_priceget_historical_data,等等。。。所有这些似乎都只返回比特币价格。有没有查询以太坊价格的方法?

似乎currency_pair = 'BTC-USD'可以改为类似于currency_pair = 'ETH-USD'的内容,尽管这没有效果。

我希望API根本不支持这一点,除了官方文档明确声明:

Get the total price to buy one bitcoin or ether

我可以在buy/sell请求中使用quote='true'标志来解决这个问题。不过,这只是向前推进的工作,我想历史数据。


Tags: 方法函数apidataget价格buyprice
3条回答

source code永远是你的朋友。

def get_spot_price(self, **params):
    """https://developers.coinbase.com/api/v2#get-spot-price"""
    if 'currency_pair' in params:
        currency_pair = params['currency_pair']
    else:
        currency_pair = 'BTC-USD'
    response = self._get('v2', 'prices', currency_pair, 'spot', data=params)
    return self._make_api_object(response, APIObject)

def get_historic_prices(self, **params):
    """https://developers.coinbase.com/api/v2#get-historic-prices"""
    response = self._get('v2', 'prices', 'historic', data=params)
    return self._make_api_object(response, APIObject)

我们可以看到两个函数都调用同一个api端点。我们看到get_spot_price支持currency_pair参数,并将其作为api调用的一部分传递。另一方面,get_historic_prices没有。

我想知道如果那样会发生什么。我们试试看:

from coinbase.wallet.client import Client
from coinbase.wallet.model import APIObject

client = Client(api_key, api_secret)
client._make_api_object(client._get('v2', 'prices', 'ETH-USD', 'historic'), APIObject)


<APIObject @ 0x10dd04938> {
    "currency": "USD",
    "prices": [
        {
          "price": "52.60",
          "time": "2017-03-30T17:03:48Z"
        },
        {
          "price": "52.60",
          "time": "2017-03-30T17:03:38Z"
        },
        {
          "price": "52.54",
          "time": "2017-03-30T17:03:28Z"
        },
        {
          "price": "52.54",
          "time": "2017-03-30T17:03:18Z"
        },
        {
          "price": "52.54",
          "time": "2017-03-30T17:03:08Z"
        },
        {
          "price": "52.53",
          "time": "2017-03-30T17:02:58Z"
        },
        {
          "price": "52.53",
          "time": "2017-03-30T17:02:48Z"
        },
        {
          "price": "52.53",
          "time": "2017-03-30T17:02:38Z"
        },
        {
          "price": "52.53",
          "time": "2017-03-30T17:02:28Z"
        },
        .....

成功!

我会按他们的方式发送公关。但现在你可以用我的代码片段。


PR submitted

对我来说,汇率也有类似的问题。 尝试在中更改参数

coinbase\wallet\client.py

response = self._get('v2', 'prices', 'spot', data=params)

response = self._get('v2', 'prices', 'spot', params=params)

我尝试了这个方法,但遇到了一个问题:将“currency_pair”参数与“historic”参数一起使用,只会在过去几天内生成一个1秒粒度的历史记录。

我通过使用GDAX客户机API和GDAX Python client解决了这个问题:

安装GDAX Python客户端:

pip install gdax

然后,即使没有GDAX帐户,也可以使用公共API部分:

import gdax

client = gdax.PublicClient()
client.get_product_historic_rates('ETH-USD', granularity=60*60*24)

要获取可用产品(加密货币/法定货币对)的列表,请使用

client.get_products()

并扫描id条目。

相关问题 更多 >

    热门问题