2017-03-27 66 views
9

使用python coinbase API--函數get_buy_price,get_sell_price,get_spot_price,get_historical_data,等等......都似乎只返回比特幣價格。有沒有一種查詢以太坊價格的方法?歷史etherium價格 - Coinbase API

看起來currency_pair = 'BTC-USD'可以改成類似於currency_pair = 'ETH-USD'的東西,雖然這沒有效果。

我會想到的是,API根本不支持這一點,除了官方文件明確規定:

獲取的總價格購買一個比特幣或醚

我可以工作通過在買入/賣出請求中使用quote='true'標誌,可以有所迴應。然而,這隻能發揮作用,我想要歷史數據。

回答

13

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他們的方式。但現在你可以使用我的代碼片段。


PR submitted

+0

@sam,順便說一下,除了歷史數據,所有的方法應與貨幣對工作了。如果他們似乎沒有工作,也許你需要在coinbase網站更新您的個人資料中的API版本andalso確保你是不是想向您的代碼 –

+0

舊的API版本我不知道蟒蛇,我是iOS開發者,基本上我使用的網址來獲取數據和分析數據。可以請你給我的網址此歷史數據,最後你的程序生成的URL來獲取數據。 THX ..我 –

4

我嘗試這樣做,有這樣的使用「currency_pair」參數與「歷史」的參數,就會產生一個歷史與僅在過去數天1秒的粒度問題。

安裝GDAX Python客戶端:

我與GDAX Python client解決了這個利用GDAX客戶端API,而不是一起

pip install gdax 

然後你就可以使用公共API的一部分,即使沒有一個GDAX帳戶:

import gdax 

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

,獲得可用的產品(cryptocurrency/FIAT貨幣對)的列表,使用

client.get_products() 

並掃描id條目。

0

有些東西對我來說也有類似的問題,叫做匯率。 嘗試更改參數

coinbase \ wallet \ client。PY

響應= self._get( 'V2', '價格', '發現',數據=參數)

響應= self._get( 'V2', '價格', '發現',則params =參數)