Created
September 3, 2018 18:04
-
-
Save rhammell/ae10894a5de74bb12cc30a58c58c053e to your computer and use it in GitHub Desktop.
Get hourly candle data using GDAX API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' Get historic crypto price data using the GDAX API ''' | |
import time | |
import datetime | |
import json | |
import gdax | |
# Create gdax client | |
client = gdax.PublicClient() | |
# Set complete date range | |
end = datetime.datetime.now() | |
start = end - datetime.timedelta(days=30) | |
# Chunk date range into 100-hour ranges to comply with API limits | |
date_ranges = [] | |
span = datetime.timedelta(hours=100) | |
stop = end - span | |
while start < stop: | |
current = start + span | |
date_ranges.append((start, current)) | |
start = current | |
date_ranges.append((start, end)) | |
# Loop through date ranges | |
for dates in date_ranges: | |
print(dates[0], dates[1]) | |
# Make API request | |
candles = client.get_product_historic_rates( | |
'ETH-USD', | |
start=dates[0].isoformat(), | |
end=dates[1].isoformat(), | |
granularity=3600) | |
# Loop through returned candle data | |
candles.reverse() | |
for candle in candles: | |
# Format candle date | |
candle[0] = datetime.datetime.fromtimestamp(candle[0]).isoformat() | |
# Print candle data - [time, low, high, open, close, volume | |
print(candle) | |
# Pause between requests to comply with API limits | |
time.sleep(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment