Created
March 8, 2020 00:31
-
-
Save brianddk/866ee3296666acda358e05172682b6d8 to your computer and use it in GitHub Desktop.
Historic Daily Market Data GBP
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
# [rights] Copyright 2020 brianddk at github https://github.com/brianddk | |
# [license] Apache 2.0 License https://www.apache.org/licenses/LICENSE-2.0 | |
# [repo] https://gist.github.com/brianddk/866ee3296666acda358e05172682b6d8 | |
# [btc] BTC-b32: bc1qwc2203uym96u0nmq04pcgqfs9ldqz9l3mz8fpj | |
# [tipjar] https://gist.github.com/brianddk/3ec16fbf1d008ea290b0 | |
# [ref] https://docs.pro.coinbase.com/#get-historic-rates | |
# [post] https://redd.it/fexxlo/ | |
from json import dumps | |
from datetime import datetime as dt | |
from requests import get | |
from requests.auth import AuthBase | |
from time import sleep | |
class CoinbaseExchangePublic(AuthBase): | |
def __call__(self, request): | |
request.headers.update({ | |
'Content-Type': 'application/json' | |
}) | |
sleep(1.1/3) | |
return request | |
url = 'https://api.pro.coinbase.com/' | |
auth = CoinbaseExchangePublic() | |
candles = [] | |
day=86400 | |
params = dict(granularity = day) | |
while True: | |
r = get(url + 'products/BTC-GBP/candles', auth=auth, params=params) | |
days = len(r.json()) | |
if days: | |
candles += r.json() | |
else: | |
break | |
start = candles[-1][0] | |
params['end'] = dt.utcfromtimestamp(start - day).isoformat() | |
params['start'] = dt.utcfromtimestamp(start - day * days).isoformat() | |
# print(len(candles)) | |
print(dumps(candles, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment