Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
trueToastedCode opened this issue Nov 26, 2024 · 2 comments
Assignees

Comments

@trueToastedCode
Copy link

trueToastedCode commented Nov 26, 2024

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
Copy link
Collaborator

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?

@pcriadoperez pcriadoperez self-assigned this Nov 30, 2024
@trueToastedCode
Copy link
Author

trueToastedCode commented Dec 13, 2024

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?

Yes, it turned out that this actually happens every single time in my strategy... it holds a client instance which after some time just doesn't work anymore. Maybe as some sideffect? My strategy also holds a second client Instance (in a library) without credentials which is just needed to fetch some history as well as a WebSocket connection.

Simply re-instantiating the client when needed seemed to fix the issue for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants