ccxt icon indicating copy to clipboard operation
ccxt copied to clipboard

Binance futures `watch_liquidations_for_symbols` symbols should be lowercase

Open timborden opened this issue 1 year ago • 5 comments

Operating System

OSX

Programming Languages

Python

CCXT Version

4.3.82

Description

~~There are two URL paths for Binance WebSockets....one for a single stream and one for multiple streams - https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Connect~~

~~The watch_liquidations_for_symbols method accepts multiple symbols, so the URL for multiple streams (wss://fstream.binance.com/stream?streams=<streamName1>/<streamName2>/<streamName3>) should be used....but the URL for a single stream (wss://fstream.binance.com/ws/<streamName>) is used instead.~~

~~There also appears to be~~ Is there an error in the stream method that's used to compose the URL? - https://github.com/ccxt/ccxt/blob/a34de623eb7b70f23dd78d254cf345664d7bbd82/python/ccxt/pro/binance.py#L173

~~...when used in the watch_liquidations_for_symbols, the stream method returns 0 resulting in a wss://fstream.binance.com/ws/0 URL.~~

~~Here's a dump of values passed from watch_liquidations_for_symbols to watch_multiple:~~

url=wss://fstream.binance.com/ws/0
message_hashes=['liquidations::ZRO/USDT:USDT', 'liquidations::ZK/USDT:USDT', 'liquidations::NTRN/USDT:USDT', 'liquidations::SEI/USDT:USDT', 'liquidations::ZETA/USDT:USDT', 'liquidations::ONDO/USDT:USDT', 'liquidations::MAVIA/USDT:USDT', 'liquidations::ENA/USDT:USDT']
message={'method': 'SUBSCRIBE', 'params': ['ZROUSDT@forceOrder', 'ZKUSDT@forceOrder', 'NTRNUSDT@forceOrder', 'SEIUSDT@forceOrder', 'ZETAUSDT@forceOrder', 'ONDOUSDT@forceOrder', 'MAVIAUSDT@forceOrder', 'ENAUSDT@forceOrder'], 'id': 1}
subscribe_hashes=['ZROUSDT@forceOrder', 'ZKUSDT@forceOrder', 'NTRNUSDT@forceOrder', 'SEIUSDT@forceOrder', 'ZETAUSDT@forceOrder', 'ONDOUSDT@forceOrder', 'MAVIAUSDT@forceOrder', 'ENAUSDT@forceOrder']
subscription={'id': 1}

(edit: Binance also accepts connections that send a subscription message, instead of via the URL - https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Live-Subscribing-Unsubscribing-to-streams)

timborden avatar Aug 14 '24 15:08 timborden

@timborden It seems that both URLs work. I can subscribed streams.

BTW, I didn't get liquidations with the new url neither. Not sure what happend.

(connecting to wss://fstream.binance.com/stream?streams=ETHUSDT@forceOrder/DOGEUSDT@forceOrder
onUpgrade
onOpen
sending {
  method: 'SUBSCRIBE',
  params: [ 'ETHUSDT@forceOrder', 'DOGEUSDT@forceOrder' ],
  id: 1
}
onMessage { result: null, id: 1 }

sc0Vu avatar Aug 15 '24 00:08 sc0Vu

Thanks for your help debugging @sc0Vu

I just ran things on my end, with verbose enabled and waited for Binance to send Liquidation messages. There were a couple of messages from Binance:

  • 2024-08-15 12:06:00.318, ENAUSDT, buy, quantity=1904.0, price=0.3242443
  • 2024-08-15 12:10:43.668, ZETAUSDT', sell, quantity=13.0, price=0.597001

....but they weren't received:

2024-08-15T11:59:15.236Z connecting to wss://fstream.binance.com/ws/0 with timeout 10000 ms
2024-08-15T11:59:16.324Z connected
2024-08-15T11:59:16.325Z ping loop
2024-08-15T11:59:16.326Z sending {'method': 'SUBSCRIBE', 'params': ['ZETAUSDT@forceOrder', 'SEIUSDT@forceOrder', 'ENAUSDT@forceOrder', 'ZKUSDT@forceOrder', 'NTRNUSDT@forceOrder', 'MAVIAUSDT@forceOrder', 'ONDOUSDT@forceOrder', 'ZROUSDT@forceOrder'], 'id': 1}
2024-08-15T11:59:16.665Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-15T11:59:16.666Z message {"result":null,"id":1}
2024-08-15T12:00:16.955Z ping WSMessage(type=<WSMsgType.PING: 9>, data=bytearray(b'1723723216846'), extra='')
2024-08-15T12:02:16.620Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-15T12:03:17.011Z ping WSMessage(type=<WSMsgType.PING: 9>, data=bytearray(b'1723723396900'), extra='')
2024-08-15T12:05:16.774Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-15T12:06:17.084Z ping WSMessage(type=<WSMsgType.PING: 9>, data=bytearray(b'1723723576955'), extra='')
2024-08-15T12:08:16.660Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-15T12:09:17.142Z ping WSMessage(type=<WSMsgType.PING: 9>, data=bytearray(b'1723723757012'), extra='')
2024-08-15T12:11:16.693Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-15T12:12:17.175Z ping WSMessage(type=<WSMsgType.PING: 9>, data=bytearray(b'1723723937062'), extra='')

timborden avatar Aug 15 '24 12:08 timborden

@sc0Vu - I figured out the issue....I spotted All symbols for streams are lowercase in the Binance docs - https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Connect

....and was able to resolve the issue with the following:

import asyncio
import ccxt.pro as ccxt  # noqa: E402

class custom(ccxt.binanceusdm):
   async def watch_multiple(self, url, message_hashes, message=None, subscribe_hashes=None, subscription=None):
        message["params"] = [f"{x[0].lower()}@aggTrade" for x in (p.split("@") for p in message["params"])]
        await super().watch_multiple(url, message_hashes, message, subscribe_hashes, subscription)
  
async def example():
   binance = custom({})
   await binance.load_markets()
   binance.verbose = True
   symbols = ['ZETA/USDT:USDT', 'SEI/USDT:USDT', 'ENA/USDT:USDT', 'ZK/USDT:USDT', 'NTRN/USDT:USDT', 'MAVIA/USDT:USDT', 'ONDO/USDT:USDT', 'ZRO/USDT:USDT']

   while True:
       message = await binance.watch_liquidations_for_symbols(symbols)
       print(message)

   await binance.close()

asyncio.run(example())

....resulting in:

2024-08-16T09:09:44.168Z connecting to wss://fstream.binance.com/ws/0 with timeout 10000 ms
2024-08-16T09:09:45.198Z connected
2024-08-16T09:09:45.198Z ping loop
2024-08-16T09:09:45.204Z sending {'method': 'SUBSCRIBE', 'params': ['zetausdt@aggTrade', 'seiusdt@aggTrade', 'enausdt@aggTrade', 'zkusdt@aggTrade', 'ntrnusdt@aggTrade', 'maviausdt@aggTrade', 'ondousdt@aggTrade', 'zrousdt@aggTrade'], 'id': 1}
2024-08-16T09:09:45.539Z pong WSMessage(type=<WSMsgType.PONG: 10>, data=bytearray(b''), extra='')
2024-08-16T09:09:45.540Z message {"result":null,"id":1}
2024-08-16T09:09:46.726Z message {"e":"aggTrade","E":1723799386735,"a":36729489,"s":"ZETAUSDT","p":"0.558100","q":"21","f":77353900,"l":77353900,"T":1723799386580,"m":true}
2024-08-16T09:09:47.283Z message {"e":"aggTrade","E":1723799387293,"a":36729490,"s":"ZETAUSDT","p":"0.558200","q":"13","f":77353901,"l":77353901,"T":1723799387139,"m":false}

timborden avatar Aug 16 '24 09:08 timborden

@timborden Thanks, we'll fix this asap!

sc0Vu avatar Aug 16 '24 15:08 sc0Vu

Can confirm the issue has been fixed..... thanks @sc0Vu!

timborden avatar Aug 21 '24 09:08 timborden