sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.99k stars 2.2k forks source link

ThreadedDepthCacheManager is broken. Sample code doesn't work. #1121

Open SpaceMonkeyForever opened 2 years ago

SpaceMonkeyForever commented 2 years ago

Simple orderbook ws code:

    def get_orderbook_futures_stream_ws(self):
        dcm = ThreadedDepthCacheManager()
        # start is required to initialise its internal loop
        dcm.start()

        def handle_depth_cache(depth_cache):
            print(f"symbol {depth_cache.symbol}")
            print("top 5 bids")
            print(depth_cache.get_bids()[:5])
            print("top 5 asks")
            print(depth_cache.get_asks()[:5])
            print("last update time {}".format(depth_cache.update_time))

        # multiple depth caches can be started
        dcm_name = dcm.start_futures_depth_socket(handle_depth_cache, symbol='ETHBTC')
        dcm.join()

Result:

  File "D:\Code\Crypto\cryptotrading-python\binance_client.py", line 133, in <module>
    main()
  File "D:\Code\Crypto\cryptotrading-python\binance_client.py", line 126, in main
    c.get_orderbook_futures_stream_ws()
  File "D:\Code\Crypto\cryptotrading-python\binance_client.py", line 119, in get_orderbook_futures_stream_ws
    dcm_name = dcm.start_futures_depth_socket(handle_depth_cache, symbol='ETHBTC')
  File "D:\Code\Python\envs\cryptotrading-python\lib\site-packages\binance\depthcache.py", line 462, in start_futures_depth_socket
    return self._start_depth_cache(
  File "D:\Code\Python\envs\cryptotrading-python\lib\site-packages\binance\depthcache.py", line 430, in _start_depth_cache
    dcm = dcm_class(
  File "D:\Code\Python\envs\cryptotrading-python\lib\site-packages\binance\depthcache.py", line 161, in __init__
    self._bm = bm or BinanceSocketManager(self._client, self._loop)
  File "D:\Code\Python\envs\cryptotrading-python\lib\site-packages\binance\streams.py", line 319, in __init__
    self.STREAM_URL = self.STREAM_URL.format(client.tld)
AttributeError: 'NoneType' object has no attribute 'tld'

I'm using Python 3.9 and python-binance 1.0.15

oyku-gencay commented 2 years ago

I've stumbled to the same problem. The issue is that ThreadedDepthManager tries to use its own client but the futures_depth_socket requires an async client. Best approach would be using DepthManager with an async client instead of ThreadedDepthManager.

OpenCoderX commented 2 years ago

Look at this pull request, it solved this issue for me, I made the change in my local copy. https://github.com/sammchardy/python-binance/pull/1055

oliver-zehentleitner commented 2 years ago

Wanna try a new solution? https://github.com/LUCIT-Systems-and-Development/unicorn-binance-local-depth-cache

Not much tested till now, but you can help to improve with giving me feedback!

SpaceMonkeyForever commented 2 years ago

Thanks guys for all your answers, I really appreciate it. I moved to using cryptofeed soon after opening this, so I didn't try any of the given solutions.

maxmax1992 commented 2 years ago

I've stumbled to the same problem. The issue is that ThreadedDepthManager tries to use its own client but the futures_depth_socket requires an async client. Best approach would be using DepthManager with an async client instead of ThreadedDepthManager.

works like charm, thanks!