BitMEX / api-connectors

Libraries for connecting to the BitMEX API.
https://www.bitmex.com/app/restAPI
910 stars 795 forks source link

ws.market_depth(): data is not updating properly #252

Open stabilus opened 6 years ago

stabilus commented 6 years ago

using python 3.7

from bitmex_websocket import BitMEXWebsocket
ws = BitMEXWebsocket(endpoint="https://www.bitmex.com/api/v1", symbol="XBTUSD", api_key='xxx', api_secret='yyy'
instrument = ws.get_instrument()

I am running ws.market_depth() in my loop and get the following problem:

after a while when the price point of the instrument shifts, the data for the price ticks between price right at beginning of connection and current price is not updated.

Example:

price of XBTUSD at connecting: 6500. All data is updated correctly. When the price shifts to - say - 6550, data between 6500 and 6550 is missing, i.e. market depth data does not show any orders in that range.

I have been observing this for a while but assigned it first to bitmex. But the behavior is consistent, as soon as the price moves, the order data is incomplete. This is particularly grave when trying to rely on order volume right around the actual price.

shuckc commented 6 years ago

I have tried to reproduce your report using the following code to repeatedly print the best bid/ask, and I find tight spreads printed at every interval.

from bitmex_websocket import BitMEXWebsocket
import logging
from time import sleep

def run():
    ws = BitMEXWebsocket(endpoint="https://www.bitmex.com/api/v1", symbol="XBTUSD",
                         api_key=None, api_secret=None)
    logger = logging.getLogger()

    # Run forever
    while(ws.ws.sock.connected):
        md = ws.market_depth()
        sell = None
        buy = None
        if md:
            for o in md:
                if o['side'] == 'Sell':
                    if sell == None or o['price'] < sell['price']:
                        sell = o
                if o['side'] == 'Buy':
                    if buy == None or o['price'] > buy['price']:
                        buy = o
        if buy and sell:
            logger.info("Market Depth: {0:14,} @ {1:10.1f} | {2:14,} @ {3:10.1f}".format(sell['size'], sell['price'], buy['size'], buy['price']))
        sleep(0.5)

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    run()