sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.06k stars 2.21k forks source link

BinanceAPIException: APIError(code=-1100): Illegal characters found in parameter 'symbol' #651

Open ufotje opened 3 years ago

ufotje commented 3 years ago

Describe the bug A clear and concise description of what the bug is. I saw this error for many of the other parameters too. I'm trying to get historical_klines Before I used a hardcode value and it worked fine, now I get the symbol from a querystring, which results in this error

To Reproduce @app.route('/history') def history(): symbol = request.args.get('symbol') candlesticks = client.get_historical_klines(symbol=symbol, interval=Client.KLINE_INTERVAL_5MINUTE, start_str="10 Jan, 2021")

Expected behavior To get the klines from jan 10th till know with 5min candlesticks

Environment (please complete the following information):

Logs or Additional context File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 2464, in call return self.wsgi_app(environ, start_response) File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\sideways8\coinview\venv\Lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\sideways8\coinview\venv\Lib\site-packages\flask_compat.py", line 39, in reraise raise value File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\sideways8\coinview\venv\Lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functionsrule.endpoint File "C:\sideways8\coinview\coinview\app.py", line 53, in history for data in candlesticks: File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 874, in get_historical_klines_generator first_valid_ts = self._get_earliest_valid_timestamp(symbol, interval) File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 747, in _get_earliest_valid_timestamp kline = self.get_klines( File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 734, in get_klines return self._get('klines', data=params, version=self.PRIVATE_API_VERSION) File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 237, in _get return self._request_api('get', path, signed, version, kwargs) File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 202, in _request_api return self._request(method, uri, signed, kwargs) File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 197, in _request return self._handle_response() File "C:\sideways8\coinview\venv\Lib\site-packages\binance\client.py", line 230, in _handleresponse raise BinanceAPIException(self.response) binance.exceptions.BinanceAPIException: APIError(code=-1100): Illegal characters found in parameter 'symbol'; legal range is '^[A-Z0-9-.]{1,20}$'.

markeyev commented 3 years ago

Dear ufotje,

There is no actual value of the symbol in the provided logs. Please use logger, print, PDB, or any other approach to figure out the actual value of the symbol you're receiving in symbol = request.args.get('symbol').

ufotje commented 3 years ago

This is the method I use to ge the historical klines: @app.route('/history') def history(): symbol = str(request.args.get('pair')) if request.args: candlesticks = client.get_historical_klines(symbol=symbol, interval=Client.KLINE_INTERVAL_30MINUTE, start_str="10 Jan, 2021") else: candlesticks = client.get_historical_klines(symbol="btceur", interval=Client.KLINE_INTERVAL_30MINUTE, start_str="10 Jan, 2021")

    processed_candlesticks = []

    for data in candlesticks:
        candlestick = {
            "time": data[0] / 1000,
            "open": data[1],
            "high": data[2],
            "low": data[3],
            "close": data[4],
    }
    processed_candlesticks.append(candlestick)

    return jsonify(processed_candlesticks)

This is where it's being called: fetch('http://localhost:5000/history') .then((r) => r.json()) .then((response) => { console.log(response) candleSeries.setData(response); }) I subscribe to a webstream later in the code using the same variable and that works, so I know that the variable is being set in the html var binanceSocket = new WebSocket("wss://stream.binance.com:9443/ws/" + pair +"@kline_5m");

markeyev commented 3 years ago

Oh, I think I know what is the problem here. Please do next

client.get_historical_klines(symbol=symbol.uppper(), interval=Client.KLINE_INTERVAL_30MINUTE,
start_str="10 Jan, 2021")

If you're using a non-uppercase symbol definition, you're receiving this error.

ufotje commented 3 years ago

As we say in Belgium: "Toppie!". Silly mistake, but that fixed it, weird that you need an uppercase symbol for the historcal trades, but can subscribe to a stream with a lowercase. Anyways thanks for the assistance

Op ma 25 jan. 2021 20:07 schreef Ivan Markeev notifications@github.com:

Oh, I think I know what is the problem here. Please do next

client.get_historical_klines(symbol=symbol.uppper(), interval=Client.KLINE_INTERVAL_30MINUTE, start_str="10 Jan, 2021")

If you're using a non-uppercase symbol definition, you're receiving this error.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sammchardy/python-binance/issues/651#issuecomment-767044076, or unsubscribe https://github.com/notifications/unsubscribe-auth/AG6PMPN3UM2PIWOFZK6AGUDS3W6NZANCNFSM4WQBV6YA .