PythonForForex/Binance-api-step-by-step-guide

Problem with websocket example

vslobody opened this issue · 6 comments

in example websocket_stream.py:

bsm = BinanceSocketManager(client)
conn_key = bsm.start_symbol_ticker_socket('BTCUSDT', btc_trade_history)

the problem is that method start_symbol_ticker_socket is available in ThreadedWebsocketManager class, not BinanceSocketManager.

Any idea how to fix this?

From what I can tell, the BinanceSocketManager class has had a major overhaul and is now using Async methods via aiohttp where the tutorial I wrote was based on v0.7.5 which relied on a threaded method using the twisted library.

If you downgrade to 0.7.5 the example will work as intended. If you want to use the latest version - have you tried swapping out the BinanceSocketManager class for the ThreadedWebsocketManager class in the import and init on line 27?

actually i did, but with little effect. it sits forever in the while not self._bsm loop:

def _start_async_socket(
        self, callback: Callable, socket_name: str, params: Dict[str, Any], path: Optional[str] = None
    ) -> str:
        while not self._bsm:
            time.sleep(0.1)
        socket = getattr(self._bsm, socket_name)(**params)
        path = path or socket._path  # noqa
        self._socket_running[path] = True
        self._loop.call_soon(asyncio.create_task, self.start_listener(socket, socket._path, callback))
        return path

made proposal how to address these issue

It looks like the ThreadedWebSocketManager is also using an async method. I haven't had a chance to play around with the latest version of the library.

There is an example in the Docs on how to use both BinanceSocketManager - https://python-binance.readthedocs.io/en/latest/websockets.html?highlight=websocket#binancesocketmanager-websocket-usage

Alternatively, I found it less complicated to bypass the library and use the standard aiohttp websocket template - https://docs.aiohttp.org/en/stable/client_quickstart.html#websockets

Down the road I will see if i can update the article and code samples to the latest version of python-binance

Igor, one of the authors at AlgoTrading101.com, has posted code that is adapted to work with the latest version of python-binance. It can be found here - https://github.com/IgorWounds/Binance-Course/blob/main/WebSocket/WebSocket%20%2B%20Python-Binance%20Order.py

From what I can see, the only change is that the BinanceSocketManager has been swapped for the ThreadedWebsocketManager class which is what I initially suggested but you said didn't work. Not sure if passing the client through the class had anything to do with it as Igor didn't pass it through.

FYI - The code in this repo has been updated to work with the latest version of python-binance and so the websocket is now coded to use the new ThreadedWebSocketManager class