python - 以太坊历史价格 - Coinbase API

标签 python python-3.x ethereum coinbase-api

使用 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

我可以通过在买/卖请求中使用 quote='true' 标志来稍微解决这个问题。然而,这只会向前发展,我想要历史数据。

最佳答案

source code永远是你的 friend 。

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。但现在您可以使用我的代码片段。


PR submitted

关于python - 以太坊历史价格 - Coinbase API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037896/

相关文章:

python - 理解逻辑语句

javascript - 将 JavaScript 变量传递给 Python

python - 将两个 dfs 与一列分组并连接所有行值

Python pandas 转换对象列中的单个值

python - QCalendar 样式。禁用项目的属性

python - 如何在不引用键的情况下转储类似 json 的字符串?

python-3.x - multiprocessing.Pipe() 与 .Queue()

kubernetes - 在 AWS EKS/kubernetes 上使用公共(public) EFS 存储运行多个 go-ethereum pod

ethereum - 如何检测以太坊地址是否为 ERC20 代币合约?

c# - Poloniex C# API - 获取交易