Skip to content

Instantly share code, notes, and snippets.

@ChadThackray
Last active October 22, 2024 07:39
Show Gist options
  • Save ChadThackray/4e04a1162ad2bd85649c5283bf8f276a to your computer and use it in GitHub Desktop.
Save ChadThackray/4e04a1162ad2bd85649c5283bf8f276a to your computer and use it in GitHub Desktop.
Get OHLC/Candlestick data from Coinbase pro historical API in Python
# Licensed under the MIT License. See comment below for full licence information.
import requests
import pandas as pd
import time
from datetime import datetime,timedelta
apiUrl = "https://api.pro.coinbase.com"
sym = "ETH-USD"
barSize = "60"
timeEnd = datetime.now()
delta = timedelta(seconds = int(barSize))
timeStart = timeEnd - (300*delta)
timeStart = timeStart.isoformat()
timeEnd = timeEnd.isoformat()
parameters = {
"start":timeStart,
"end":timeEnd,
"granularity":barSize,
}
data = requests.get(f"{apiUrl}/products/{sym}/candles",
params = parameters,
headers = {"content-type":"application/json"})
df = pd.DataFrame(data.json(),
columns = ["time","low","high","open","close","volume"])
df["date"] = pd.to_datetime(df["time"], unit='s')
df = df[["date","open","high","low","close"]]
df.set_index("date", inplace = True)
df = df.resample("3 min").agg({
"open":"first",
"high":"max",
"low":"min",
"close":"last"})
df.reset_index(inplace = True)
df = df.dropna()
print(df)
@petejfjohnson
Copy link

Thanks for this! Does this work with the current coinbase advanced api now that pro is deprecated? I have been looking everywhere but cant find how to fetch historic candle data. Thanks in advance

@ChadThackray
Copy link
Author

Probably not if this API has been depreciated. Although it seems to be still a similar process: https://docs.cdp.coinbase.com/exchange/reference/exchangerestapi_getproductcandles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment