Last active
October 22, 2024 07:39
-
-
Save ChadThackray/4e04a1162ad2bd85649c5283bf8f276a to your computer and use it in GitHub Desktop.
Get OHLC/Candlestick data from Coinbase pro historical API in Python
This file contains hidden or 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
# 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) |
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
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
Copyright 2021 Chad Thackray
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.