历史以太坊价格 - Coinbase API

sam*_*sam 9 python python-3.x coinbase-api ethereum

使用Python coinbase API--的functions-- get_buy_price,get_sell_price,get_spot_price,get_historical_data,等...一切似乎只返回比特币的价格.有没有办法查询以太坊价格?

似乎currency_pair = 'BTC-USD'可以改为类似于currency_pair = 'ETH-USD'虽然没有效果的东西.

我希望API不支持这个,除了官方文档明确指出:

获得一个比特币或以太币的总价格

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

Gia*_*los 14

源代码永远是你的朋友.

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)
Run Code Online (Sandbox Code Playgroud)

我们可以看到两个函数都调用相同的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"
        },
        .....
Run Code Online (Sandbox Code Playgroud)

成功!

我会按照他们的方式发送PR.但是现在你可以使用我的代码片段了.


公关提交