0
votes

Is it possible to retrieve the last granularity interval entry using a Coinbase Pro API web socket? For example on an hour graph (3600) to retrieve the hourly update via a web socket. I can do this using a normal REST call and I can get the ticker via a web socket but it's not exactly what I am looking for.

This is a working web socket example for a BTC-GBP and BTC-USD ticker.

from websocket import create_connection
import json, time

URL = "wss://ws-feed.pro.coinbase.com"

ws = create_connection(URL)

params = {"type": "subscribe", "product_ids": ["BTC-USD"],
"channels": ["heartbeat", {"name": "ticker", "product_ids": ["BTC-USD", "BTC-GBP"]}]}

while True:
    ws.send(json.dumps(params))
    result = ws.recv()
    print(result)
    time.sleep(1)
    converted = json.loads(result)

To explain it another way, a web socket version of this:

https://api.pro.coinbase.com/products/BTC-GBP/candles?granularity=3600

Can it be done and if so how?

1

1 Answers

0
votes

For starters, please do not send a subscribe message to their websocket server once for each message you expect to receive. Subscribing once is sufficient. If you are trying to allow for updating your subscription, then consider adding a conditional clause that detects when there is a change to your subscription dictionary prior to deciding whether to send it or not.

ws.send(json.dumps(params))
while True:
    result = ws.recv()
    print(result)
    time.sleep(1)
    converted = json.loads(result)

To answer your original question; however, you can subscribe to the 'matches' channel, and create a candle on your own from the data. Create four variables (Open, High, Low, Close), and store the first price that occurs in a slice of time (in your case, an hour), then store the max and min prices seen during the slice of time, and finally the last price seen. You can also keep a running summation of the volume during the time window.