whittlem / pycryptobot

Python Crypto Bot (PyCryptoBot)
Apache License 2.0
1.96k stars 736 forks source link

Binance: An existing connection was forcibly closed by the remote host #78

Closed denissonpaz closed 3 years ago

denissonpaz commented 3 years ago

Hi, I think I need some help here...

I keep getting this error whenever I try to go live (it happens after around 7 min I start the bot):

2021-04-27 15:35:00 | XRPUSDT (BEAR) | 5m | Close: 1.4 | ^ EMA12/26: 1.39 > 1.39 ^ | ^ MACD: 0.0 > -0.01 ^ | ^ OBV: 59019460.33 (3.66%) ^ | ERI: sell | WAIT | Last Action: BUY | 0% (delta: 1.41) 2021-04-27 15:40:00 | XRPUSDT (BEAR) | 5m | Close: 1.42 | ^ EMA12/26: 1.4 > 1.39 ^ | ^ MACD: 0.0 > -0.01 ^ | ^ OBV: 61230004.87 (7.12%) ^ | ERI: buy | WAIT | Last Action: BUY | 0% (delta: 1.42) Traceback (most recent call last): File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "<string>", line 3, in raise_from File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request httplib_response = conn.getresponse() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1345, in getresponse response.begin() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin version, status, reason = self._read_status() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 268, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto return self._sock.recv_into(b) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1241, in recv_into return self.read(nbytes, buffer) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1099, in read return self._sslobj.read(len, buffer) ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

Here is my config file:

{ "binance" : { "api_url" : "https://api.binance.com", "api_key" : "", "api_secret" : "", "config" : { "base_currency" : "XRP", "quote_currency" : "BRL", "granularity" : "15m", "live" : 1, "verbose" : 0 } }, "telegram" : { "token" : "", "client_id" : "" } }

I changed the quote_currenct to USDT but still got the same error.

denissonpaz commented 3 years ago

I guess this could be the reason:

"Why are my WebSocket connections being dropped/disconnected? Every 3 minutes, active WebSocket connections will receive a ping. If you do not respond with a properly framed pong, your connection will be terminated." https://www.binance.com/en/support/faq/360004492232

whittlem commented 3 years ago

I'm not using WebSockets, so I don't think this is related. I'n not sure if your config and your debug output are related. In your config you are trying 15 minute intervals but in your output you are trading 5 minute intervals. I personally would not trade anything less than 15 minute intervals (and only in really good market conditions). With such short intervals you probably would make losses from not covering your fees. The best option is to actually not specify the granularity at all. The bot will then "smart switch" between the 1 hour and 15 minute intervals automatically. This gives the best results.

denissonpaz commented 3 years ago

Hi @whittlem , thanks for the hint with eh granularity and the smart switch.

I've changed it and I still got the error though.

I did some tests here, and it seems the poll request takes too long to happen. it's currently set to 300 sec. I just tested it for 180 seconds and now the code is not raising any run-time error.

# poll every 5 minute list(map(s.cancel, s.queue)) s.enter(180, 1, executeJob, (sc, app))

Log: 2021-04-28 11:00:00 | XRPBRL (BULL) | 1h | Close: 7.46 | v EMA12/26: 7.49 < 7.51 v | v MACD: -0.03 < 0.02 v | v OBV: -377454.6800 (-10.64%) v | ERI: buy | WAIT | Last Action: SELL 2021-04-28 07:27:51 | XRPBRL (BULL) | 1h | Current Price: 7.513 2021-04-28 07:30:56 | XRPBRL (BULL) | 1h | Current Price: 7.499

You may check the configuration and the error below: when running with 300 seconds for the poll { "binance" : { "api_url" : "https://api.binance.com", "api_key" : "", "api_secret" : "", "config" : { "base_currency" : "XRP", "quote_currency" : "BRL", "live" : 1, "verbose" : 0, "smartswitch" : 1 } }, "telegram" : { "token" : "", "client_id" : "" } }

Error: `2021-04-28 11:00:00 | XRPBRL (BULL) | 1h | Close: 7.5 | v EMA12/26: 7.5 < 7.52 v | v MACD: -0.03 < 0.02 v | v OBV: -393008.3400 (-6.95%) v | ERI: buy | WAIT | Last Action: SELL 2021-04-28 07:23:36 | XRPBRL (BULL) | 1h | Current Price: 7.459 Traceback (most recent call last): File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "", line 3, in raise_from File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request httplib_response = conn.getresponse() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1345, in getresponse response.begin() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin version, status, reason = self._read_status() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 268, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto return self._sock.recv_into(b) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1241, in recv_into return self.read(nbytes, buffer) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1099, in read return self._sslobj.read(len, buffer) ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Programs\Python\3.9\env\lib\site-packages\requests\adapters.py", line 439, in send resp = conn.urlopen( File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen retries = retries.increment( File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\util\retry.py", line 532, in increment raise six.reraise(type(error), error, _stacktrace) File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\packages\six.py", line 734, in reraise raise value.with_traceback(tb) File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 445, in _make_request six.raise_from(e, None) File "", line 3, in raise_from File "C:\Programs\Python\3.9\env\lib\site-packages\urllib3\connectionpool.py", line 440, in _make_request httplib_response = conn.getresponse() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 1345, in getresponse response.begin() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 307, in begin version, status, reason = self._read_status() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\http\client.py", line 268, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\socket.py", line 704, in readinto return self._sock.recv_into(b) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1241, in recv_into return self.read(nbytes, buffer) File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1099, in read return self._sslobj.read(len, buffer) urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Programs\Python\cryptobot\pycryptobot\pycryptobot.py", line 1063, in s.run() File "C:\Users\al\AppData\Local\Programs\Python\Python39\lib\sched.py", line 151, in run action(*argument, kwargs) File "C:\Programs\Python\cryptobot\pycryptobot\pycryptobot.py", line 1019, in executeJob account.saveTrackerCSV(app.getMarket()) File "C:\Programs\Python\cryptobot\pycryptobot\models\TradingAccount.py", line 293, in saveTrackerCSV df = self.getOrders(market, '', 'done') File "C:\Programs\Python\cryptobot\pycryptobot\models\TradingAccount.py", line 94, in getOrders resp = self.client.get_all_orders(symbol=market) File "C:\Programs\Python\3.9\env\lib\site-packages\binance\client.py", line 1803, in get_all_orders return self._get('allOrders', True, data=params) File "C:\Programs\Python\3.9\env\lib\site-packages\binance\client.py", line 320, in _get return self._request_api('get', path, signed, version, kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\binance\client.py", line 265, in _request_api return self._request(method, uri, signed, kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\binance\client.py", line 259, in _request self.response = getattr(self.session, method)(uri, kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\requests\sessions.py", line 555, in get return self.request('GET', url, kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\requests\sessions.py", line 542, in request resp = self.send(prep, send_kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\requests\sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "C:\Programs\Python\3.9\env\lib\site-packages\requests\adapters.py", line 498, in send raise ConnectionError(err, request=request) requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))`

JCoyk commented 1 year ago

Did this process get fixed, I'm having the same issue, I'm on UM_Futures and the same things is happening. I will get a buy trade go thru and then I will wait unitl a cross happens and then it will give me the connection reset error. Please help !