UPetit / python-binance-profit

Script to submit a buy order followed by an automatic sell OCO order
MIT License
47 stars 15 forks source link

ECONNRESET while checking current price #1

Closed JulienDev closed 3 years ago

JulienDev commented 3 years ago

pi@raspberrypi:~/python-binance-profit $ python3 execute_orders.py --symbol ATOMUSDT --quantity 125 --price 7.721 --profit 0.03 --loss 0.03 Load client Client authenticated Binance API Time: 2021-01-28 20:59:26Z Binance API status: normal Trading allowed OCO orders allowed

Step 1 - Buy order execution Order validation in progress... Quantity (limit order) is validated Quantity: 125.0 Price is validated Price: 7.721 -> The limit buy order has been sent The order is not filled yet... The order is not filled yet... The order is not filled yet... The order is not filled yet... Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen chunked=chunked) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 3, in raise_from File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "/usr/lib/python3.7/http/client.py", line 1336, in getresponse response.begin() File "/usr/lib/python3.7/http/client.py", line 306, in begin version, status, reason = self._read_status() File "/usr/lib/python3.7/http/client.py", line 267, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib/python3.7/socket.py", line 589, in readinto return self._sock.recv_into(b) File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 309, in recv_into return self.recv_into(*args, **kwargs) File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 299, in recv_into raise SocketError(str(e)) OSError: (104, 'ECONNRESET')

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 367, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/lib/python3/dist-packages/six.py", line 692, in reraise raise value.with_traceback(tb) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen chunked=chunked) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request six.raise_from(e, None) File "", line 3, in raise_from File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request httplib_response = conn.getresponse() File "/usr/lib/python3.7/http/client.py", line 1336, in getresponse response.begin() File "/usr/lib/python3.7/http/client.py", line 306, in begin version, status, reason = self._read_status() File "/usr/lib/python3.7/http/client.py", line 267, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib/python3.7/socket.py", line 589, in readinto return self._sock.recv_into(b) File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 309, in recv_into return self.recv_into(*args, **kwargs) File "/usr/lib/python3/dist-packages/urllib3/contrib/pyopenssl.py", line 299, in recv_into raise SocketError(str(e)) urllib3.exceptions.ProtocolError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "execute_orders.py", line 509, in float(args.profit), float(args.loss)) File "execute_orders.py", line 469, in main qty_round, price_round) File "execute_orders.py", line 374, in execute_buy_strategy orderId=buy_order_id) File "/home/pi/.local/lib/python3.7/site-packages/binance/client.py", line 1617, in get_order return self._get('order', True, data=params) File "/home/pi/.local/lib/python3.7/site-packages/binance/client.py", line 237, in _get return self._request_api('get', path, signed, version, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/binance/client.py", line 202, in _request_api return self._request(method, uri, signed, kwargs) File "/home/pi/.local/lib/python3.7/site-packages/binance/client.py", line 196, in _request self.response = getattr(self.session, method)(uri, kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 546, in get return self.request('GET', url, kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request resp = self.send(prep, send_kwargs) File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send r = adapter.send(request, kwargs) File "/usr/lib/python3/dist-packages/requests/adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', OSError("(104, 'ECONNRESET')"))

PhilippeGalvan commented 3 years ago

It looks like this is an error server side, probably because the API can be accessed without a key. Thus it might reject some calls when it is overloaded and an option might be to continue the DDOS by retrying 🤪 !

A solution might be to retry n times with something like:

for retry_number in range(nb_max_tries):
  try:
    request
  except ECONNRESET as e:
    continue
  else:
    break
else:
  print("So bad try again later on !")
UPetit commented 3 years ago

It looks like this is an error server side, probably because the API can be accessed without a key. Thus it might reject some calls when it is overloaded and an option might be to continue the DDOS by retrying 🤪 !

A solution might be to retry n times with something like:

for retry_number in range(nb_max_tries):
  try:
    request
  except ECONNRESET as e:
    continue
  else:
    break
else:
  print("So bad try again later on !")

I'll add what @PhilippeGalvan suggested very soon ;)

UPetit commented 3 years ago

Multiple exceptions is not working properly with the previous fix: TypeError: catching classes that do not inherit from BaseException is not allowed I'll update it on dev branch

PhilippeGalvan commented 3 years ago

It's not the first time this problem arise with binance, they have encoded Exceptions with codes instead of python Exceptions which is a bad practice... Here is the proposed solution with a link to the list of error codes: https://stackoverflow.com/a/53512213/10810425

UPetit commented 3 years ago

Thanks @PhilippeGalvan for the link. Actually this error is not related to a clear Binance API error but rather a connection problem with the API which is raising a Connection error with Binance code... I can't catch the exception if it's not inherited from BaseException so I'm working on it.

UPetit commented 3 years ago

I've pushed a fix: commit on dev branch

UPetit commented 3 years ago

New fix that has been tested to catch the Connection Error exception (dev branch): link

UPetit commented 3 years ago

This is resolved in the main branch: included in the current release (v.0.0.3)