Fix KeyError for prefixed coins in historical candle fetch

Bypass SDK's name_to_coin lookup in candles_snapshot by calling
http_info.post directly with the raw coin name. This fixes
KeyError for symbols like xyz:GOLD, xyz:CL, mkts:USTECH that
aren't in the name_to_coin dictionary.

The WebSocket subscription already bypassed this lookup (line 154),
but the historical fetch path did not.
This commit is contained in:
DiTus
2026-08-05 09:20:07 +02:00
parent 21be9b40b7
commit 967c86e8e9
4 changed files with 27 additions and 10 deletions

View File

@ -18,7 +18,7 @@ from logging_utils import setup_logging
class CandleFetcherDB:
"""
Fetches 1-minute candle data and saves/updates it directly in an SQLite database.
Fetches 1-minute candle data and saves/updates it directly in a PostgreSQL database.
"""
def __init__(self, coins_to_fetch: list, interval: str, days_back: int):
@ -112,7 +112,7 @@ class CandleFetcherDB:
df.sort_values(by='t', inplace=True)
if not df.empty:
return self._save_to_sqlite_with_pandas(df, coin, table_existed)
return self._save_to_db_with_pandas(df, coin, table_existed)
else:
logging.info(f"No new candles to append for {coin}.")
return 0
@ -138,7 +138,8 @@ class CandleFetcherDB:
max_retries = 3
for attempt in range(max_retries):
try:
return self.info.candles_snapshot(coin, self.interval, start_ms, end_ms)
req = {"coin": coin, "interval": self.interval, "startTime": start_ms, "endTime": end_ms}
return self.info.post("/info", {"type": "candleSnapshot", "req": req})
except ClientError as e:
if e.status_code == 429 and attempt < max_retries - 1:
logging.warning("Rate limited. Retrying...")
@ -148,7 +149,7 @@ class CandleFetcherDB:
return None
return None
def _save_to_sqlite_with_pandas(self, df: pd.DataFrame, coin: str, is_append: bool) -> int:
def _save_to_db_with_pandas(self, df: pd.DataFrame, coin: str, is_append: bool) -> int:
"""Saves a pandas DataFrame to a PostgreSQL table and returns the number of saved rows."""
table_name = db.sanitize_table_name(coin, self.interval)
try:
@ -175,7 +176,7 @@ class CandleFetcherDB:
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fetch historical candle data and save to SQLite.")
parser = argparse.ArgumentParser(description="Fetch historical candle data and save to PostgreSQL.")
parser.add_argument(
"--coins",
nargs='+',