使用 python coinbase API——函数——get_buy_price
、get_sell_price
、get_spot_price
、get_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。但现在您可以使用我的代码片段。
关于python - 以太坊历史价格 - Coinbase API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037896/