3
votes

I am trying to obtain the ticker channel data via a websocket. I am getting a response with some data, however the data I am getting is not matching the what it is suppose to show.

I have tried doing what the API specifies. The API (https://docs.pro.coinbase.com/#the-ticker-channel) says to send the request as follows:

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

Now this works, and I get a response, however the response I get is:

{
"type":"ticker", "sequence":9568995003, "product_id":"BTC-USD", "price":"7779.00000000", "open_24h":"7895.99000000", "volume_24h":"19546.97986005", "low_24h":"7467.10000000", "high_24h":"7945.50000000", "volume_30d":"569908.80402872", "best_bid":"7775.66", "best_ask":"7778.81" }

when the api says the output should be: { "type": "ticker", "trade_id": 20153558, "sequence": 3262786978, "time": "2017-09-02T17:05:49.250000Z", "product_id": "BTC-USD", "price": "4388.01000000", "side": "buy", // Taker side "last_size": "0.03000000", "best_bid": "4388", "best_ask": "4388.01" }

As you can see, I am missing the last_size, and side. I am unsure as to what I am doing wrong.

from websocket import create_connection
import json

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

ws = create_connection(URL)

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

def single():
    ws.send(json.dumps(params))

    result =  ws.recv()

    print(result)

single()

The expected output should include the last_size, and side tags. Any help is greatly appreciated.

1

1 Answers

1
votes

I have been implementing the same code myself and I also am not getting last_size and side included in the json. My best guess is that the json object being sent is not retrieving this information, which with my understanding means you will have to go without this information. If anyone does know of a way to retrieve this information, feel free to correct me.

EDIT: I think I have discovered the issue for why last_size is not being included in some of the responses (still unsure about side though). When the json is being printed, the price isn't changing, as I realized when I ran in a continuous while loop. Only when a trade has occurred will there be a shift in price, and therefore a 'last_size' category. I will provide the params I am passing as well as the code to illustrate how I get this result.

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

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

You will receive a KeyError if you try to access the 'last_size' if the price hasn't changed. My advice would be to catch this error and ignore that json, as you have all the information you already need from the json returned previously.

Hope this helps in clarifying your issue, my original response is still valid for 'side' as I have not come across anyway to receive that information.