Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get real time bid / ask / price from GDAX websocket feed

Tags:

The API doc discourages polling on /ticker endpoint, and recommend to use the websocket stream to listen for match message

But the match response only provide a price and a side (sell / buy)

How can I recreate the ticker data (price, ask and bid) from the websocket feed ?

{
  “price”: “333.99”,
  “size”: “0.193”,
  “bid”: “333.98”,
  “ask”: “333.99”,
  “volume”: “5957.11914015”,
  “time”: “2015-11-14T20:46:03.511254Z”
}

The ticker endpoint and the websocket feed both return a 'price', but I guess that it's not the same. Is the price from the ticker endpoint some sort of average over time ?

How can I compute Bid value, Ask value ?

like image 402
vdaubry Avatar asked Aug 07 '17 09:08

vdaubry


1 Answers

If I use these parameters in the subscribe message:

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

Each time a new trade is executed (and visible on http://www.gdax.com), I get this kind of message from the web socket:

{
 u'best_ask': u'3040.01',
 u'best_bid': u'3040',
 u'last_size': u'0.10000000',
 u'price': u'3040.00000000',
 u'product_id': u'BTC-EUR',
 u'sequence': 2520531767,
 u'side': u'sell',
 u'time': u'2017-09-16T16:16:30.089000Z',
 u'trade_id': 4138962,
 u'type': u'ticker'
}

Just after this particular message, I did a get on https://api.gdax.com/products/BTC-EUR/ticker, and I got this:

{
  "trade_id": 4138962,
  "price": "3040.00000000",
  "size": "0.10000000",
  "bid": "3040",
  "ask": "3040.01",
  "volume": "4121.15959844",
  "time": "2017-09-16T16:16:30.089000Z"
}

Present data are the same from web socket compared to the get request.

Please find below a complete test script implementing a web socket with this ticker.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Test for websockets."""

from websocket import WebSocketApp
from json import dumps, loads
from pprint import pprint

URL = "wss://ws-feed.gdax.com"


def on_message(_, message):
    """Callback executed when a message comes.

    Positional argument:
    message -- The message itself (string)
    """
    pprint(loads(message))
    print


def on_open(socket):
    """Callback executed at socket opening.

    Keyword argument:
    socket -- The websocket itself
    """

    params = {
        "type": "subscribe",
        "channels": [{"name": "ticker", "product_ids": ["BTC-EUR"]}]
    }
    socket.send(dumps(params))


def main():
    """Main function."""
    ws = WebSocketApp(URL, on_open=on_open, on_message=on_message)
    ws.run_forever()


if __name__ == '__main__':
    main()
like image 199
Manu NALEPA Avatar answered Sep 30 '22 09:09

Manu NALEPA