Skip to content

Instantly share code, notes, and snippets.

@icook
Last active October 21, 2018 09:12
Show Gist options
  • Save icook/d3069a2dfa12421bf56a2e2169df206f to your computer and use it in GitHub Desktop.
Save icook/d3069a2dfa12421bf56a2e2169df206f to your computer and use it in GitHub Desktop.
Makes repeated requests to Coinbase's historical candle data. Adjust granularity if desired.
import requests
import pprint
import time
import csv
from datetime import datetime
from dateutil.relativedelta import relativedelta
entries = []
granularity = 900
chunk_size = relativedelta(seconds=granularity * 300)
grab_time = datetime.utcnow() - relativedelta(years=3)
while 1:
# Our loop exit condition
if grab_time > datetime.utcnow():
break
# Get the coinbase entries
start = grab_time.isoformat()
end = (grab_time + chunk_size).isoformat()
req = "https://api.pro.coinbase.com/products/BTC-USD/candles?granularity={}&start={}&end={}".format(
granularity, start, end)
print(req)
res = requests.get(req)
# Convert the output to our universal format
processed = [{"ts": r[0], "open": r[1], "high": r[2], "low": r[3], "close": r[4], "volume": r[5], "exchange": "coinbase"} for r in res.json()]
print("Got {} entries from {} to {}".format(len(processed), start, end))
entries.extend(processed)
grab_time = grab_time + chunk_size
time.sleep(0.35) # They allow 3 req/s. Sloppy
with open('coinbase-3year.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['open', 'high', 'low', 'close', 'volume', 'datetime', 'timestamp'])
for e in entries:
writer.writerow([e['open'], e['high'], e['low'], e['close'], e['volume'], datetime.utcfromtimestamp(e['ts']), e['ts']])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment