sammchardy / python-binance

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

ERROR: CANCEL read_loop, websocket updates loss #1465

Open Ramesses3 opened 1 week ago

Ramesses3 commented 1 week ago

Bug description I'm trying to get kline data via a web socket. Interested in closing candles. I noticed that some of the data is lost, although if you check in parallel through postman, then all updates come there. Also, my python script periodically throws a CANCEL read_loop error. I think the data loss is related to it.

To Reproduce Run my simple code:

#!/usr/bin/env python3
import re
import time
import asyncio
from datetime import datetime
from binance import AsyncClient, BinanceSocketManager

async def websocket(client=None, symbol=None):
  bm = BinanceSocketManager(client)
  symbol = symbol.lower()
  ms = bm.multiplex_socket([
    f'{symbol}@kline_1m',
    f'{symbol}@kline_5m',
    f'{symbol}@kline_15m'
  ])
  while True:
    async with ms as stream:
      msg = await stream.recv()
      if msg.get('data').get('k').get('x'):
        interval = re.search(r'kline_(.+)', msg.get('stream')).group(1)
        otime = datetime.fromtimestamp(msg.get('data').get('k').get('t')/1000)
        print(f'{interval} {otime}\n{msg}\n')

async def main():
  client = await AsyncClient.create(testnet=False)
  await asyncio.gather(
    websocket(client=client, symbol='TRBUSDT'),
  )

asyncio.run(main())

Expected behavior Continuous acquisition of all data from the stream(s) without loss.

Environment:

Ramesses3 commented 1 week ago

I just wrote my simple multiplex socket using pure websockets version 13.1. It works just great, no errors, no data loss. It turns out that the problem is in python-binance.

carlosmiei commented 1 week ago

@Ramesses3, can you assemble a simple script that uses your implementation and python-binance simultaneously, compares the output, and throws an error when one starts to lag behind?