Просмотр исходного кода

Refactor monitoring components to use configurable heartbeat intervals

- Updated PendingOrdersManager, PositionTracker, and RiskManager to utilize a configurable heartbeat interval from the Config class, enhancing flexibility in monitoring frequency.
- Adjusted sleep intervals in monitoring loops to improve responsiveness and maintainability.
- Enhanced logging for error handling in monitoring processes, ensuring better traceability of issues.
Carles Sentis 18 часов назад
Родитель
Сommit
5adfcbe99d

+ 2 - 7
src/clients/hyperliquid_client.py

@@ -235,13 +235,8 @@ class HyperliquidClient:
             return None
     
     def get_positions(self, symbol: Optional[str] = None) -> Optional[List[Dict[str, Any]]]:
-        """Get current positions."""
+        """Get current positions for a symbol or all symbols."""
         try:
-            if not self.sync_client:
-                logger.error("❌ Client not initialized")
-                return None
-            
-            # Add user parameter for Hyperliquid CCXT compatibility
             params = {}
             if Config.HYPERLIQUID_WALLET_ADDRESS:
                 wallet_address = Config.HYPERLIQUID_WALLET_ADDRESS
@@ -249,7 +244,7 @@ class HyperliquidClient:
             
             logger.debug(f"🔍 Fetching positions with params: {params}")
             positions = self.sync_client.fetch_positions([symbol] if symbol else None, params=params)
-            logger.info(f"✅ Successfully fetched positions for {symbol or 'all symbols'}")
+            logger.debug(f"✅ Successfully fetched positions for {symbol or 'all symbols'}")
             return positions
         except Exception as e:
             error_message = self._extract_error_message(e)

+ 3 - 2
src/monitoring/pending_orders_manager.py

@@ -6,6 +6,7 @@ import sqlite3
 
 from ..clients.hyperliquid_client import HyperliquidClient
 from ..notifications.notification_manager import NotificationManager
+from ..config.config import Config
 
 logger = logging.getLogger(__name__)
 
@@ -98,10 +99,10 @@ class PendingOrdersManager:
             try:
                 await self._check_pending_orders()
                 await self._cleanup_expired_orders()
-                await asyncio.sleep(5)  # Check every 5 seconds
+                await asyncio.sleep(Config.BOT_HEARTBEAT_SECONDS)  # Use config heartbeat
             except Exception as e:
                 logger.error(f"Error in pending orders monitoring loop: {e}")
-                await asyncio.sleep(10)
+                await asyncio.sleep(Config.BOT_HEARTBEAT_SECONDS)
                 
     async def _check_pending_orders(self):
         """Check if any pending orders should be placed"""

+ 3 - 2
src/monitoring/position_tracker.py

@@ -5,6 +5,7 @@ from datetime import datetime, timezone
 
 from ..clients.hyperliquid_client import HyperliquidClient
 from ..notifications.notification_manager import NotificationManager
+from ..config.config import Config
 
 logger = logging.getLogger(__name__)
 
@@ -47,10 +48,10 @@ class PositionTracker:
         while self.is_running:
             try:
                 await self._check_position_changes()
-                await asyncio.sleep(2)  # Check every 2 seconds
+                await asyncio.sleep(Config.BOT_HEARTBEAT_SECONDS)  # Use config heartbeat
             except Exception as e:
                 logger.error(f"Error in position tracking loop: {e}")
-                await asyncio.sleep(5)
+                await asyncio.sleep(Config.BOT_HEARTBEAT_SECONDS)
                 
     async def _check_position_changes(self):
         """Check for any position changes"""

+ 2 - 2
src/monitoring/risk_manager.py

@@ -44,10 +44,10 @@ class RiskManager:
         while self.is_running:
             try:
                 await self._check_risk_thresholds()
-                await asyncio.sleep(3)  # Check every 3 seconds
+                await asyncio.sleep(self.config.BOT_HEARTBEAT_SECONDS)  # Use config heartbeat
             except Exception as e:
                 logger.error(f"Error in risk manager monitoring loop: {e}")
-                await asyncio.sleep(5)
+                await asyncio.sleep(self.config.BOT_HEARTBEAT_SECONDS)
                 
     async def _check_risk_thresholds(self):
         """Check if any positions need risk management"""

+ 1 - 1
trading_bot.py

@@ -14,7 +14,7 @@ from datetime import datetime
 from pathlib import Path
 
 # Bot version
-BOT_VERSION = "2.6.285"
+BOT_VERSION = "2.6.286"
 
 # Add src directory to Python path
 sys.path.insert(0, str(Path(__file__).parent / "src"))