sammchardy / python-binance

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

Lagging issue on other threads when using start_depth_socket #1318

Open pazooki opened 1 year ago

pazooki commented 1 year ago

Describe the bug When starting a new thread; twm.start_depth_socket it blocks long enough that I get lagging issue on twm.start_aggtrade_socket and the price data becomes out of date. I've timed strategy.update_depth and reduced it to just setting an instance variable to store the data coming into twm.start_depth_socket but the problem still persists. I have also tried to use ThreadedDepthCacheManager and that had the same problem of causing a lag on other threads.

To Reproduce Code snippet to reproduce the behavior:

BTCUSDT = 'BTCUSDT'

def main(strategy, is_testing):
    config = TESTNET_CONFIG if is_testing else CONFIG

    twm = ThreadedWebsocketManager(api_key=config['API_KEY'], api_secret=config['API_SECRET'], testnet=is_testing)
    # dcm = ThreadedDepthCacheManager(api_key=config['API_KEY'], api_secret=config['API_SECRET'], testnet=is_testing)
    # dcm.start()
    twm.start()
    twm.start_depth_socket(callback=strategy.update_depth, symbol=BTCUSDT, depth=10, interval=1)
    twm.start_aggtrade_socket(strategy.next, symbol=BTCUSDT)
    twm.start_user_socket(strategy.update_balance)
    # dcm.join()
    twm.join()

Expected behavior No lagging on other threads when I use twm.start_*

Environment (please complete the following information):

Logs or Additional context

I am sharing the strategy object among twm.start_depth_socket and twm.start_aggtrade_socket and twm.start_user_socket however there is no conflict or race condition since they each update their own variables without updating variables belonging to other threads. The problem I'm seeing is that the price that I receive in start_aggtrade_socket is lagged behind and outdated and when I watch and compare it to the Binance Client price. I tried it only on TESTNET