Historical ethereum prices - Coinbase API
Asked Answered
S

3

10

Using the python coinbase API-- The functions-- get_buy_price, get_sell_price, get_spot_price, get_historical_data, etc... all seem to return bitcoin prices only. Is there a way of querying Ethereum prices?

It would seem that currency_pair = 'BTC-USD' could be changed to something akin to currency_pair = 'ETH-USD' although this has no effect.

I would expect that the API simply doesn't support this, except that the official documentation explicitly states:

Get the total price to buy one bitcoin or ether

I can work around this somewhat by using the quote='true' flag in the buy/sell request. This however only works moving forward, I would like historical data.

Sladen answered 27/3, 2017 at 4:21 Comment(0)
P
16

source code will always be your 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)

We can see that both functions call the same api endpoint. We see that get_spot_price supports the currency_pair argument and passes it as part of the api call. On the other hand get_historic_prices does not.

I wonder what would happen if it did. Let's try it:

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"
        },
        .....

Success!

I'll sent a PR their way. but for now you can use my code snippet.


PR submitted

Prospector answered 30/3, 2017 at 17:16 Comment(2)
@sam, by the way all the methods except historical data should work with the currency pair already. If they seem not to work, maybe you need to update your API version on your profile at the coinbase site andalso make sure you are not asking for an older API version from your codeProspector
I dont know python, i am iOS developer, basically i use url to fetch data and parse data. can you please give me url for this historical data, finally your program is generating url to fetch data. Thx.. iVelmaveloce
D
4

I tried this and had the problem that using a 'currency_pair' parameter together with the 'historic' parameter will produce a history with 1 second granularity for the past few days only.

I solved this by using the GDAX client API instead, together with the GDAX Python client:

Install GDAX Python client:

pip install gdax

Then you can use the public API part even without having a GDAX account:

import gdax

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

To get a list of available products (cryptocurrency / FIAT currency pairs), use

client.get_products()

and scan for the id entries.

Didynamous answered 30/5, 2017 at 19:51 Comment(0)
D
0

Something worked for me with a similar problem calling exchange rates. Try changing the params in

coinbase\wallet\client.py

from

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

to

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

Divine answered 9/1, 2018 at 1:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.