binance-trade-bot icon indicating copy to clipboard operation
binance-trade-bot copied to clipboard

APIError(code=-1121): Invalid symbol

Open Amraki opened this issue 3 years ago • 3 comments

Github and Python noob here so I hope this is correct.

Reference previous #279. This appears to occur if the symbol/ticker isn't valid. This may even be a discrepancy between 'com' and 'us' servers. In this case, BTTUSDT does not show up for me on https://www.binance.us/en/markets Are validation checks being made before attempting to fetch prices?

Click to expand console log
2021-05-14 02:52:50,833 - backtesting_logger - INFO - Fetching prices for ADAUSDT between 2021-01-01 00:00:00 and 01 Jan 2021 16:40:00
2021-05-14 02:52:58,782 - backtesting_logger - INFO - Bought ADA, balance now: 548.4555 - bridge: 
0.007030000000000314
2021-05-14 02:53:01,135 - backtesting_logger - INFO - Initializing <ADA> vs <ATOM>
2021-05-14 02:53:01,173 - backtesting_logger - INFO - Fetching prices for ATOMUSDT between 2021-01-01 00:00:00 and 01 Jan 2021 16:40:00
2021-05-14 02:53:16,373 - backtesting_logger - INFO - Initializing <ADA> vs <BAT>
2021-05-14 02:53:16,406 - backtesting_logger - INFO - Fetching prices for BATUSDT between 2021-01-01 00:00:00 and 01 Jan 2021 16:40:00
2021-05-14 02:53:30,865 - backtesting_logger - INFO - Initializing <ADA> vs <BTT>
2021-05-14 02:53:30,892 - backtesting_logger - INFO - Fetching prices for BTTUSDT between 2021-01-01 00:00:00 and 01 Jan 2021 16:40:00
Traceback (most recent call last):
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\backtest.py", line 7, in <module>
   for manager in backtest(datetime(2021, 1, 1), datetime.now()):
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\backtest.py", line 186, in backtest
   trader.initialize()
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\strategies\default_strategy.py", line 10, in initialize
   super().initialize()
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\auto_trader.py", line 21, in initialize
   self.initialize_trade_thresholds()
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\auto_trader.py", line 92, in initialize_trade_thresholds
   to_coin_price = all_tickers.get_price(pair.to_coin + self.config.BRIDGE)
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\backtest.py", line 22, in get_price
   return self.manager.get_market_ticker_price(ticker_symbol)
 File "c:\Users\Amraki\OneDrive\Cody\VS Code Projects\binance-trade-bot_edeng23\binance_trade_bot\backtest.py", line 64, in get_market_ticker_price
   for result in self.binance_client.get_historical_klines(
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 849, in get_historical_klines
   first_valid_ts = self._get_earliest_valid_timestamp(symbol, interval)
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 802, in _get_earliest_valid_timestamp
   kline = self.get_klines(
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 789, in get_klines
   return self._get('klines', data=params, version=self.PRIVATE_API_VERSION)
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 292, in _get
   return self._request_api('get', path, signed, version, **kwargs)
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 242, in _request_api
   return self._request(method, uri, signed, **kwargs)
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 237, in _request
   return self._handle_response()
 File "C:\Users\Amraki\AppData\Local\Programs\Python\Python39\lib\site-packages\binance\client.py", line 285, in _handle_response
   raise BinanceAPIException(self.response)
binance.exceptions.BinanceAPIException: APIError(code=-1121): Invalid symbol.

Amraki avatar May 14 '21 07:05 Amraki

I've managed to circumvent the error with the following code added to binance_trade_bot\backtest.py:

if self.binance_client.get_symbol_info(ticker_symbol) is None:
            return None

I have no idea how efficient this is to check each time. It may be better to cache confirmed symbol pairs. I'll leave that decision to someone more qualified.

Click for new get_market_ticker_price function:
def get_market_ticker_price(self, ticker_symbol: str):
        """
        Get ticker price of a specific coin
        """

        if self.binance_client.get_symbol_info(ticker_symbol) is None:
            return None

        target_date = self.datetime.strftime("%d %b %Y %H:%M:%S")
        key = f"{ticker_symbol} - {target_date}"
        val = cache.get(key, None)
        if val is None:
            end_date = self.datetime + timedelta(minutes=1000)
            if end_date > datetime.now():
                end_date = datetime.now()
            end_date = end_date.strftime("%d %b %Y %H:%M:%S")
            self.logger.info(f"Fetching prices for {ticker_symbol} between {self.datetime} and {end_date}")
            for result in self.binance_client.get_historical_klines(
                ticker_symbol, "1m", target_date, end_date, limit=1000
            ):
                date = datetime.utcfromtimestamp(result[0] / 1000).strftime("%d %b %Y %H:%M:%S")
                price = float(result[1])
                cache[f"{ticker_symbol} - {date}"] = price
            cache.commit()
            val = cache.get(key, None)
        return val

Amraki avatar May 15 '21 04:05 Amraki

worked for me as well.

levdavid avatar Oct 24 '21 21:10 levdavid

seem to happen again and the solution @Amraki suggested doesn't work. any new suggestions?

Edit: make sure you put a valid coin in backtest.py: for manager in backtest(datetime(2022, 1, 1), datetime.now()): btc_value = manager.collate_coins("BTC")

lidorbt avatar Feb 22 '22 18:02 lidorbt