Coinbase Historic Spot Price at Date-Time

8.7k views Asked by At

The coinbase API allows you to specify a date when making a request to https://api.coinbase.com/v2/prices/BTC-USD/spot. The API states the date should be formatted as YYYY-MM-DD. Is it possible to receive the historic price up to a specific hour (Ex: price from 1 hour ago)? I see this done on their ios app but it appears they may not be providing this data via the API.

Thanks for all the help!

3

There are 3 answers

0
Gavin Stevens On BEST ANSWER

Doubtful, Spot price normally means the price "right now". It does say you can use YYYY-MM-DD (UTC), but doesn't specify a time.

Check out this similar thread: Get bitcoin historical data

0
John T On
1
fireitup On

I did some research on this recently, and I think @gavinstevens answer is correct and the experiments described below confirm that.

Built up some dates to test with:

import dt as dt
import requests
today=dt.date.today()
today_dt=dt.datetime.now()
earlier_dt=today_dt-dt.timedelta(hours=6)

print(today)
'2017-03-31'
print(today_dt.strftime("%Y-%m-%d %H:%M:%S"))
'2017-03-31 11:57:58'
print(earlier_dt.strftime("%Y-%m-%d %H:%M:%S"))
'2017-03-31 05:57:58'

#build dicts to pass in the api requests
dict_today={'date':today.strftime("%Y-%m-%d")}
dict_dt_now={'date':today_dt.strftime("%Y-%m-%d %H:%M:%S")}
dict_dt_earler={'date': earlier_dt.strftime("%Y-%m-%d %H:%M:%S")}

Now, for the Spot Price case:

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_today)
print(r.text)

{"data":{"amount":"1082.42","currency":"USD"},...}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_dt_now)
print(r.text)

{"data":{"amount":"1078.45","currency":"USD"},...}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_dt_earler)    
print(r.text)

{"data":{"amount":"1078.03","currency":"USD"},...}

as you can see from the code snips above the api returned a different price for each datetime passed. Next to go back many days just to make sure that this model holds up for all historical data...

way_earlier_dt=today_dt-dt.timedelta(days=365)
dict_way_earlier={'date': way_earlier_dt.strftime("%Y-%m-%d %H:%M:%S")}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_way_earlier)
print(r.text)

{"data":{"amount":"415.23","currency":"USD"},...}    

Indeed this returns a reasonable price for that datetime. Subtracting six hours as a final check...

way_earlier_dt=way_earlier_dt-dt.timedelta(hours=6)
dict_way_earlier={'date': way_earlier_dt.strftime("%Y-%m-%d %H:%M:%S")}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_way_earlier)
print(r.text)

{"data":{"amount":"415.23","currency":"USD"},...}

Uh-oh! Price is identical as the previous request. Looks like we may have a breakdown.

Let's do a "walk across midnight" test to see if the datetime history request is only valid for the current day. We can start back with our "today_dt" value and subtract 12, then 16 hours...

yesterday_dt_12=today_dt-dt.timedelta(hours=12)
yesterday_dt_16=today_dt-dt.timedelta(hours=16)

print(yesterday_dt_12.strftime("%Y-%m-%d %H:%M:%S"))
'2017-03-30 23:57:58'
print(yesterday_dt_16.strftime("%Y-%m-%d %H:%M:%S"))
'2017-03-30 19:57:58'

dict_yest_12={'date':yesterday_dt_12.strftime("%Y-%m-%d %H:%M:%S")}
dict_yest_16={'date':yesterday_dt_16.strftime("%Y-%m-%d %H:%M:%S")}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_yest_12)
print(r.text)

{"data":{"amount":"1039.37","currency":"USD"},..}

r=requests.get('https://api.coinbase.com/v2/prices/BTC-USD/spot/',params=dict_yest_16)
print(r.text)

{"data":{"amount":"1039.37","currency":"USD"},...}

Alas, we have our answer. We cannot get historical BTC Spot prices using the coinbase api spot request. I did some more fiddling, and confirmed the price differences in the initial experiment are fluctuations in the current BTC Spot price today that the api is returning.

Sad!