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

Binance private api issue #3057

Closed
hexeraction opened this issue Jun 6, 2018 · 40 comments
Closed

Binance private api issue #3057

hexeraction opened this issue Jun 6, 2018 · 40 comments
Assignees
Labels

Comments

@hexeraction
Copy link

Hello.
I've tried to place an order on Binance but got following error. My api key is correct and all permissions already done.
My code looks like

connection = ccxt.binance({'apiKey': api, 'secret': secret}) print(connection.fetch_open_orders(symbol="ETH/BTC"))

And errors looks like :
equests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.binance.com/api/v3/openOrders?timestamp=1528282115226&recvWindow=5000&symbol=ETHBTC&signature=mysignature
and
ccxt.base.errors.ExchangeError: binance: unknown error code: {"code":-2014,"msg":"API-key format invalid."} -2014

@kroitor
Copy link
Member

kroitor commented Jun 6, 2018

My api key is correct

Binance says that your apiKey format is invalid: {"code":-2014,"msg":"API-key format invalid."}

So, you should really check it first...

exchange = ccxt.binance ({
    'apiKey': 'YOUR_API_KEY',
    'secret': 'YOUR_SECRET',
    'enableRateLimit': True,  # ←---- in your best interest: https://github.com/ccxt/ccxt/wiki/Manual#rate-limit
})
print(exchange.fetch_balance())

Does this work with your keys? Make sure that you didn't misplace the apiKey and the secret, also make sure that both are 64-characters long.

@kroitor kroitor self-assigned this Jun 6, 2018
@hexeraction
Copy link
Author

I'm sorry. That was my fail.

@kroitor
Copy link
Member

kroitor commented Jun 6, 2018

@hexeraction thx for reporting back anyways )

@tye002
Copy link

tye002 commented Nov 6, 2018

I have encountered the same problem, what is the solution?

@kroitor
Copy link
Member

kroitor commented Nov 6, 2018

@tye002

#3057 (comment)

Make sure that you didn't misplace the apiKey and the secret, also make sure that both are 64-characters long.

@kroitor
Copy link
Member

kroitor commented Nov 6, 2018

@tye002 also, check that your the names for params that you're using for credentials are exactly apiKey (uppercase K, other letters lowercase), and secret (all letters lowercase).

@tye002
Copy link

tye002 commented Nov 6, 2018

@kroitor Thanks, I found the problem, it's about something related to the time mismatch. I tried this code, now it's working.
ccxt.binance({ 'options':{ 'adjustForTimeDifference':True}})

@kroitor
Copy link
Member

kroitor commented Nov 6, 2018

@tye002 thx for reporting back!

However, you said you have the same problem and the error message discussed in this issue: {"code":-2014,"msg":"API-key format invalid."} does not correspond to errors with time difference. It must have been a different error, like a signature error, a nonce error or timestamp-related error message. Anyways, we're glad you've figured it out.

@tye002
Copy link

tye002 commented Nov 6, 2018

@kroitor Correct, there were two errors, the other one is about the timestamp. I thought this one :{"code":-2014,"msg":"API-key format invalid."} is the root problem. Thanks

@rs-g
Copy link

rs-g commented Apr 15, 2020

I get the same error: {"code":-2014,"msg":"API-key format invalid."}.

The problem is that I don't always get the error, at the beginning it's working but after some time it returns that error.

Can anyone help with a solution?

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "", line 3, in raise_from
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 400, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/packages/six.py", line 734, in reraise
raise value.with_traceback(tb)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "", line 3, in raise_from
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 538, in fetch
response = self.session.request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/Test/Trading_TV_Binance.py", line 594, in
btcAmountUsed = get_max_position_available()
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/Test/Trading_TV_Binance.py", line 123, in get_max_position_available
to_use = float(exchange.fetch_balance().get('USDT').get('free'))
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/binance.py", line 556, in fetch_balance
response = getattr(self, method)(query)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 446, in inner
return entry(_self, **inner_kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/binance.py", line 1813, in request
response = self.fetch2(path, api, method, params, headers, body)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 465, in fetch2
return self.fetch(request['url'], request['method'], request['headers'], request['body'])
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 581, in fetch
raise NetworkError(method + ' ' + url + ' ' + error_string)

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g how do you initialize the exchange? Do you have the rate-limiter enabled? Can you paste the minimal smallest code to reproduce the issue (without your keys) ?

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor thanks alot for the quick reply

exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    'apiKey': 'key here',
    'secret': 'secret key here',
    'timeout': 30000,
    'enableRateLimit': True,
    # for futures
    'options': {
        'defaultType': 'future',
    },
})

exchange.fetch_balance().get('USDT').get('free')

Like I said, I don't always get the error and I can't reproduce it, I get the error at random times

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g

This error could indicate a networking glitch on the client-side:

ConnectionResetError: [Errno 54] Connection reset by peer

It can be caused by PyCharm if you're launching the code from the IDE. Is that the case? If yes, can you do multiple retries to try and trigger it on purpose? Also, you can do multiple tries running that same script outside of PyCharm (with pure Python) to see if it throws that error at some point.

Apart from that, it may also be related to the frequency of launching that script. Because if the error is thrown after several successful consequent attempts, that could mean that you're not respecting the rate-limit. So, it also depends on whether the script is working continuously with the same exchange instance, or if the script is always recreating the exchange instance (effectively dropping the rate-limiting). In short, as documented in the Manual, CCXT will first try to load the list of markets and currencies from the exchange upon your first call to a unified method like fetch_balance. If you then relaunch the same script – the old rate-limit params are discarded and the new exchange instance is created that loads the markets again... If that happens too often – Binance may temporarily block the access to its endpoints to protect itself from a DDoS. To avoid it, you can reuse the same exchange instance, without dropping and recreating it upon each run.

So, in order to answer your question we need a lot more details (much more info, everything you can share). Also, reaching out to Binance techsupport might help as well, since they know better if they blocked you occasionally for any reason.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor

Yes I was using PyCharm but I have tried with pure Python and I got the same error

I have managed to reproduce the error above with this code:

def test():
    return exchange.fetchBalance().get('USDT').get('free')


while True:
    print(test())
    time.sleep(16*60)

And right now I get another error:

ccxt.base.errors.ExchangeError: GET https://fapi.binance.com/fapi/v1/exchangeInfo HTTPSConnectionPool(host='fapi.binance.com', port=443): Max retries exceeded with url: /fapi/v1/exchangeInfo (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x1031d7100>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))

and I cannot access binance.com

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g it looks like an issue on binance's side, i can't open that URL on my machine either... Must be a temporary glitch or something. I can confirm, it's not available right now.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor thank you for the support.

What about my above code, seems like if I wait a little more time without doing any action on exchange. and then try to exchange.fetchBalance I get that error from my first post.

Any thoughts?

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g it fails because of this other internal error, it's a cascade of errors that is initiated by the unavailability of the markets in the first place. So, fetch_balance calls load_markets (if markets and currencies are not loaded yet), and if load_markets fails – fetch_balance will also fail. The market/currency info is needed to properly unify currency codes, symbols and exchange response formats. So, basically, if the exchange API is not fully-functioning, the error may indicate connection problems.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor I will rephrase,

I have tried this code when binance was working:

def test():
    return exchange.fetchBalance().get('USDT').get('free')


while True:
    print(test())
    time.sleep(16*60)

I got the following result:

first time when it entered in the while loop, test() returned the correct balance
then wait 16*60 secs
then when it goes to test() i got this error #3057 (comment)

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g are you having the same error at the moment? Because if the exchange is not fully available, one request can succeed and the other one can fail, randomly.
In other words – is it reproducible in this exact sequence on your side? ↓

  1. success of fetch_balance
  2. wait 16 * 60 sec
  3. failure of fetch_balance

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor at this moment I can not test it at all because binance is not accessible. I will try when it goes back online.

but the last working test (before binance went down) was this:

  1. success of fetch_balance
  2. wait 16 * 60 sec
  3. failure of fetch_balance

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g ok, let me know if you can reproduce it after a while, I will look into it. Unfortunately, I can't debug it now due to the unavailable API. I suspect a tmp connectivity issue on Binance's side. If it does not go away after a while – plz report back. Thx!

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g looks like it's up again, though, i'm not sure if it's stabilized: https://fapi.binance.com/fapi/v1/exchangeInfo. Anyways, let me know if you still experience difficulties with it.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor

here is the full code:

import ccxt
import time

exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    'apiKey': 'api',
    'secret': 'secret',
    'timeout': 30000,
    'enableRateLimit': True,
    # for futures
    'options': {
        'defaultType': 'future',
    },
})


def get_max_position_available():
    return exchange.fetchBalance().get('USDT').get('free')


while True:
    print(get_max_position_available())
    time.sleep(16*60)

here is the output:

/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/bin/python /Users/rs.gabriel/PycharmProjects/TvScrapper/Test/binance_API_test.py
0
Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "", line 3, in raise_from
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 400, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/packages/six.py", line 734, in reraise
raise value.with_traceback(tb)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "", line 3, in raise_from
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
response.begin()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
version, status, reason = self._read_status()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
return self.read(nbytes, buffer)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
return self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 538, in fetch
response = self.session.request(
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/Test/binance_API_test.py", line 73, in
print(get_max_position_available())
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/Test/binance_API_test.py", line 69, in get_max_position_available
return exchange.fetchBalance().get('USDT').get('free')
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/binance.py", line 556, in fetch_balance
response = getattr(self, method)(query)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 446, in inner
return entry(_self, **inner_kwargs)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/binance.py", line 1813, in request
response = self.fetch2(path, api, method, params, headers, body)
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 465, in fetch2
return self.fetch(request['url'], request['method'], request['headers'], request['body'])
File "/Users/rs.gabriel/PycharmProjects/TvScrapper/venv/lib/python3.8/site-packages/ccxt/base/exchange.py", line 581, in fetch
raise NetworkError(method + ' ' + url + ' ' + error_string)
ccxt.base.errors.NetworkError: GET https://fapi.binance.com/fapi/v1/account?timestamp=1586968784861&recvWindow=5000&signature=806ed79e0ade211c0f0592804db0512e93e6dfab0924959eb4d17e8288830860 ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

Process finished with exit code 1

Am I doing something wrong?

PS: If I change the sleep to 60, it's working

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g what's your version of CCXT? (it does work for me with proper keys + the most recent version of the lib + python3.7)

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor when I go to project interpreter in PyCharm, ccxt version is 1.26.9 and Python version is 3.8

You had the same sleep time as me and you had 2 results in a raw in the while loop?

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g i mean the actual runtime version:

import ccxt
import time

exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    'apiKey': 'api',
    'secret': 'secret',
    'timeout': 30000,
    'enableRateLimit': True,
    # for futures
    'options': {
        'defaultType': 'future',
    },
})

def get_max_position_available():
    return exchange.fetchBalance().get('USDT').get('free')

print('CCXT version:', ccxt.__version__)  # ←--------------------- add this

while True:
    print(get_max_position_available())
    time.sleep(16*60)

You had the same sleep time as me and you had 2 results in a raw in the while loop?

I had the same sleep as you, and now it's sleeping for 16 minutes (your delay specifically) before doing the second iteration of the loop, so i have to wait a bit before i can confirm. It did return the first response properly on the first iteration.

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g yes, it returned the second response properly to me:

mbp:python igorkroitor$ python3 test-binance-future-fetch-balance.py
0.0
0.0

You may want to check your IP address whitelists – do you have IP whitelisting enabled for the API keypair in question? (the IPs are configurable with binance website)

@rs-g
Copy link

rs-g commented Apr 15, 2020

I have updated the CCXT.

it prints: CCXT version: 1.26.43

I will do one more test with this version, thanks a lot for your result

I have the IP whitelisted because the first iteration of while prints the correct result

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g in that case I suspect PyCharm – try running the same script in pure python in console, outside of PyCharm to see if it makes a difference. You tried with pure python already, but that was a different connectivity error due to the exchange unavailability. I mean retry in pure python again from here.

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g let me know if this helps:

pip3 install certifi -U
pip3 install requests -U

?

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g other people also report the same issues here: #6807 And apparently, it is not python-specific, it happens in JavaScript as well. So, that indicates some issue on Binance's side still going on, if it breaks in all languages at once.

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g lol, it crashed for me on the third iteration in pure python:

mbp:python igorkroitor$ python3 test-binance-future-fetch-balance.py
0.0
0.0
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/connection.py", line 344, in connect
    ssl_context=context)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
    session=session
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 853, in _create
    self.do_handshake()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1117, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:1056)

I suggest to move this discussion here: #6807 ← let's proceed to resolving this issue, it's the same connectivity problem again.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor you guys have other errors not the same as mine. Yours is because of the binance past downtime I guess

pip3 install certifi -U
pip3 install requests -U

I did these already.

I have the same problem when I started the script with pure Python.

  1. success of fetch_balance
  2. wait 16 * 60 sec
  3. failure of fetch_balance

I can't get past that 16 mins sleep, it always chases after the sleep.

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g https://twitter.com/cz_binance/status/1250411130289152001

The unavailability can manifest itself in many different ways, including random error messages on a breakdown. This really is an issue on Binance's side. We just need to wait until they resolve it.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor it's just too strange that I get this error each time after sleep (16 mins) and I don't get any error if the sleep is 60 seconds, it's working perfectly fine :|

Do I need to create another instance if I don't use the old one for 16 mins?

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g

it's just too strange that I get this error each time after sleep (16 mins) and I don't get any error if the sleep is 60 seconds, it's working perfectly fine :|

Yes, that is strange, however, I cannot explain that other than with just random chance (like 2 of every 5 handshake requests work, other 3 requests fail, for example).

The first iteration works for me the first time, but not always.

Do I need to create another instance if I don't use the old one for 16 mins?

No. You should reuse the existing instance, like you do. In other words, your code should work regardless of the sleeping timeout, because even if you remove it completely – you have the built-in rate-limiter enabled.

The problem is related to the underlying HTTPS transport, and it is most likely caused by the internal thresholds and timers inside the HTTPS implementation, originating with a bad response or no proper response from the exchange in the first place.

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g

it's just too strange that I get this error each time after sleep (16 mins) and I don't get any error if the sleep is 60 seconds, it's working perfectly fine :|

Actually, it can be explained rather easily: if your handshake succeeds, the established connection is kept-alive (the keep-alive option is enabled for a few minutes by default by the underlying OSI stack). If you then keep sending requests each new request also extends the lifetime of the connection and keeps the connection alive. However, if you wait for 16 minutes – the exchange or the TCP/HTTPS client or server can drop it. So, on your second call the handshake that has to be done again can fail. This is why multiple continuous frequent requests work (if the handshake was successful), but some rare occasional requests can fail (this is random since binance is not fully available for SSL handshakes).

To check the theory, you can run this:

import ccxt
import time

exchange_id = 'binance'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    'apiKey': 'api',
    'secret': 'secret',
    'timeout': 30000,
    'enableRateLimit': True,
    # for futures
    'options': {
        'defaultType': 'future',
    },
})

def get_max_position_available():
    return exchange.fetchBalance().get('USDT').get('free')

while True:
    try:    # ←--------------------- add this
        print(get_max_position_available())
    except Exception as e:  # ←--------------------- add this
        print(e)  # print the error and retry on next iteration using the same exchange instance
    print('sleeping...')
    time.sleep(16*60)

↑ If it first works, then fails, then works again in a loop – that proves the above theory.

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor makes sense, I'm trying this.

Result:

0.0
sleeping...

GET https://fapi.binance.com/fapi/v1/account?timestamp=1586979168627&recvWindow=5000&signature=46135899f3e95ca8257eb41c3f1ab9f8bde972b5b03addf8557a7bb5ef16d8b9 ('Connection aborted.', OSError("(54, 'ECONNRESET')"))
sleeping...

0.0
sleeping...

@kroitor
Copy link
Member

kroitor commented Apr 15, 2020

@rs-g yeah, that's exactly what we thought, right? But it seems that they have mostly fixed it already, so you just want to wait a bit until the updates propagate over the globe and reach your ISP. https://twitter.com/cz_binance/status/1250432336774086660

@rs-g
Copy link

rs-g commented Apr 15, 2020

@kroitor yep, thanks a lot for your help

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

No branches or pull requests

4 participants