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 api connection problem #92

Closed
Nurfen opened this issue Dec 28, 2021 · 5 comments
Closed

Binance api connection problem #92

Nurfen opened this issue Dec 28, 2021 · 5 comments

Comments

@Nurfen
Copy link

Nurfen commented Dec 28, 2021

Hello,
The following error pops up when I try to test one of the examples with the Binance interface:

INFO: No portfolio name to load specified, defaulting to the first in the file: (Pompom). This is fine if there is only one portfolio in use. Invalid API Key, IP, or permissions for action - are you trying to use your normal exchange keys while in sandbox mode? Try toggling the 'use_sandbox' setting in your settings.json or check if the keys were input correctly into your keys.json.
Any idea what it could be?

My api keys appear to be correct, because I get my account information using the following code (python-binance package):

from binance.client import Client

client = Client(keys['binance']["project"]["API_KEY"],
                keys['binance']["project"]["API_SECRET"],
                )

print(client.get_account())

If I look at the trace, it is on the self.calls.get_account() call, where the code breaks.
My settings file is as follows:

  "settings": {
    "account_update_time": 5000,
    "use_sandbox": true, (I also tried it with this as false, but then it suggests to put it to true)
    "use_sandbox_websockets": false,
    "websocket_buffer_size": 10000,
    "test_connectivity_on_auth": true,

    "coinbase_pro": {
      "cash": "USD"
    },
    "binance": {
      "cash": "USDT",
      "binance_tld": "us"
    },
    "alpaca": {
      "websocket_stream": "iex",
      "cash": "USD",
      "enable_shorting": true
    },
    "oanda": {
      "cash": "USD"
    }
  }
}

Thanks in advance!

@EmersonDove
Copy link
Member

Interesting, I'm assuming that we make an invalid assumption when Binance gives their first get_account response.

If you go to the file at .\blankly\exchanges\interfaces\binance\binance_interface.py

and paste this code over the init_exchange function on line 41:

    def init_exchange(self):
        try:
            symbols = self.calls.get_exchange_info()["symbols"]
        except binance.exceptions.BinanceAPIException as e:
            print(e)
            raise exceptions.APIException("Invalid API Key, IP, or permissions for action - are you trying "
                                          "to use your normal exchange keys while in sandbox mode? "
                                          "\nTry toggling the \'use_sandbox\' setting in your settings.json or check "
                                          "if the keys were input correctly into your keys.json.")

it will allow us to see what the binance package itself is saying about your keys.

It is interesting that it works inside the binance client package but not in the blankly framework.

We had someone who had a very similar error who fixed it by reinstalling the package: #41 (comment)

@Nurfen
Copy link
Author

Nurfen commented Dec 29, 2021

Thanks for the fast reply!

The script continued a litlle bit further, but then it failed again.
I started printing the uri.
With the settings.json "use_sandbox": true I get:
https://testnet.binance.vision/api/v3/accountwhich shouldn't work indeed because I didn't make an account there yet

I am trying out the backtesting so, I thought that it shouldn't be a big deal if I am not in the sanbox environment (correct me if I'm wrong), so I set the "use_sandbox" to false
The uri is then:
https://api.binance.us/api/v3/account
In my case, it should go to the .com domain and not the .us domain
replacing the binance settings to the below fixes that

"binance": {
     "cash": "EU",
     "binance_tld": "com"
   }

This also works without your fix, so as a conclusion we can say that I had my settings.json messed up...

Maybe not relevant for the thread anymore, but now the script is failing a little bit further:
Any Idea why that would be? I've added the prints of the last requests:

get https://api.binance.com/api/v3/account True {'data': {}}
No cached data found for BTC-USD from: 1609227635 to 1640677235 at a resolution of 86400 seconds.
klines False v3 {'data': {'symbol': 'BTCUSD', 'startTime': 1609227635000, 'endTime': 1640677235000, 'interval': '1d', 'limit': 1000}}
get https://api.binance.com/api/v3/klines False {'data': {'symbol': 'BTCUSD', 'startTime': 1609227635000, 'endTime': 1640677235000, 'interval': '1d', 'limit': 1000}}
Traceback (most recent call last):
  File "bot.py", line 75, in <module>
    history = s.backtest(to='1y', initial_values={'USD': 100})
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/blankly/frameworks/strategy/strategy_base.py", line 511, in backtest
    results = self.backtesting_controller.run()
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 536, in run
    prices = self.sync_prices()
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py", line 325, in sync_prices
    resolution)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py", line 624, in get_product_history
    return self.calls.get_product_history(symbol, epoch_start, epoch_stop, resolution)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/blankly/exchanges/interfaces/binance/binance_interface.py", line 654, in get_product_history
    limit=1000)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/binance/client.py", line 870, in get_klines
    return self._get('klines', data=params, version=self.PRIVATE_API_VERSION)
  File "/Users/pascal/anaconda3/envs/py37/lib/python3.7/site-packages/binance/client.py", line 373, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/binance/client.py", line 335, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/binance/client.py", line 315, in _request
    return self._handle_response(self.response)
  File "/Users/user1/anaconda3/envs/py37/lib/python3.7/site-packages/binance/client.py", line 324, in _handle_response
    raise BinanceAPIException(response, response.status_code, response.text)
binance.exceptions.BinanceAPIException: APIError(code=-1121): Invalid symbol.

@EmersonDove
Copy link
Member

Awesome I didn't notice the binance TLD setting and forgot to ask. I think the invalid symbol error is because binance uses USDT instead of USD so it should work if you set your symbol to BTC-USDT.

@EmersonDove
Copy link
Member

Just added a suggestion to check the binance_tld inside the binance auth error.

@Nurfen
Copy link
Author

Nurfen commented Dec 30, 2021

Changing the USD to USDT fixed it indeed.
Thanks a lot for the quick feedback!
From what I see so far, the project looks very nice

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