edeng23 / binance-trade-bot

Automated cryptocurrency trading bot
GNU General Public License v3.0
7.78k stars 2.18k forks source link

APIError(code=-1121): Invalid symbol #334

Open Amraki opened 3 years ago

Amraki commented 3 years ago

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 ```py 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 vs 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 vs 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 vs 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 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 commented 3 years ago

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: ```py 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 ```
levdavid commented 2 years ago

worked for me as well.

lidorbt commented 2 years ago

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")