Jelajahi Sumber

Refactor TradingEngine to use a dictionary for market cache instead of a list

- Updated _markets_cache to be a dictionary to improve data structure and access.
- Adjusted logic for fetching market precision to accommodate the new dictionary format.
- Enhanced logging to provide clearer information when precision data is missing or malformed.
Carles Sentis 3 hari lalu
induk
melakukan
576c6e9e9b
1 mengubah file dengan 8 tambahan dan 6 penghapusan
  1. 8 6
      src/trading/trading_engine.py

+ 8 - 6
src/trading/trading_engine.py

@@ -129,22 +129,24 @@ class TradingEngine:
                     logger.info(f"Successfully cached {len(self._markets_cache)} markets.")
                 else:
                     logger.warning("get_markets() returned no data. Using empty cache.")
-                    self._markets_cache = [] # Set to empty list to avoid re-fetching immediately
+                    self._markets_cache = {} # Set to empty dict to avoid re-fetching immediately
                     self._markets_cache_timestamp = datetime.now(timezone.utc)
 
             except Exception as e:
                 logger.error(f"Error fetching markets for token info: {e}. Will use defaults.")
-                self._markets_cache = [] # Prevent re-fetching on immediate subsequent calls
+                self._markets_cache = {} # Prevent re-fetching on immediate subsequent calls
                 self._markets_cache_timestamp = datetime.now(timezone.utc)
         
         default_precision = {'amount': 6, 'price': 2} # Default if not found
         target_symbol_prefix = f"{base_asset.upper()}/"
 
         if self._markets_cache:
-            for market_details in self._markets_cache:
-                symbol = market_details.get('symbol')
+            # Assuming self._markets_cache is a Dict keyed by symbols, 
+            # and values are market detail dicts.
+            for market_details_dict in self._markets_cache.values(): 
+                symbol = market_details_dict.get('symbol')
                 if symbol and symbol.upper().startswith(target_symbol_prefix):
-                    precision = market_details.get('precision')
+                    precision = market_details_dict.get('precision')
                     if precision and isinstance(precision, dict) and \
                        'amount' in precision and 'price' in precision:
                         logger.debug(f"Found precision for {base_asset}: {precision}")
@@ -154,7 +156,7 @@ class TradingEngine:
                             'quote_precision': precision.get('price')  # For direct access
                         }
                     else:
-                        logger.warning(f"Market {symbol} found for {base_asset}, but precision data is missing or malformed: {precision}")
+                        logger.warning(f"Market {symbol} found for {base_asset}, but precision data is missing or malformed: {precision if 'precision' in locals() else market_details_dict.get('precision')}")
                         return { # Return default but log that market was found
                             'precision': default_precision,
                             'base_precision': default_precision['amount'],