I have been searching and have not found a good solution. I’m trying to get coinbase’s btc-usd price at midnight. The closest I have gotten successfully is calling by date but I cant get any closer than the day this way https://api.coinbase.com/v2/prices/BTC-USD/spot?date=2024-05-07
I have attached my best attempt but that gives me a different number every time it is called.
import requests
from datetime import datetime
CRYPTO_PAIR = "BTC-USD"
now = datetime.now()
midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
formatted_date = midnight.strftime("%Y-%m-%d")
url = f"https://api.coinbase.com/v2/prices/{CRYPTO_PAIR}/spot"
response = requests.get(url, params={"date": formatted_date})
data = response.json()["data"]
price = data["amount"]
print(price)
I have also tried in a different way but this gives me the value 44,000 and something when it should be at a little above 63,000.
midnight_timestamp = int(time.mktime(time.strptime('00:00:00:2024', '%H:%M:%S:%Y')))
ohlcv = exchange.fetch_ohlcv(symbol, '1d', since=midnight_timestamp * 1000)
midnight_price = ohlcv[0][4] # Closing price at midnight
return midnight_price
kobis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.