sammchardy / python-binance

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

Mysteries "Remote end closed connection without response" - How to deal with it? #1480

Open trueToastedCode opened 1 day ago

trueToastedCode commented 1 day ago

Sometimes my bot fails simply fetching balance details although the internet connection is fine.

import os
from pathlib import Path
from binance.client import Client

client = Client(
    api_key='<your api key>',
    private_key=Path(
        os.path.dirname(os.path.realpath(__file__)),
        'your-prv-key.pem'
    )
)

# possibly wait for hours or days

client.get_asset_balance(asset='USDT')['free']
Traceback (most recent call last):
  File "/app/main.py", line 25, in on_candles
    strategy.on_candles(*args, **kwargs)
  File "/app/strategy/strategy.py", line 365, in on_candles
    quote_quantity = self.__broker.get_free_asset_balance(self.__quote_asset)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/strategy/binance_broker.py", line 158, in get_free_asset_balance
    return Decimal(self.__client.get_asset_balance(asset=asset)['free'])
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 2295, in get_asset_balance
    res = self.get_account(**params)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 2272, in get_account
    return self._get("account", True, data=params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 531, in _get
    return self._request_api("get", path, signed, version, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 475, in _request_api
    return self._request(method, uri, signed, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/binance/client.py", line 450, in _request
    self.response = getattr(self.session, method)(uri, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 602, in get
    return self.request("GET", url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/requests/adapters.py", line 682, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

This is bad, cz some strategies calculate signals rather rare and if it then can't open a position, it impacts overall performance. What's the best way to deal with it?

pcriadoperez commented 19 hours ago

hi @trueToastedCode , have you been able to find a way to reproduce it consitently? Would it work for your case if you put it in a try except clause, and retry on error?