浏览代码

Enhance margin mode configuration and logging in HyperliquidClient

- Added margin mode options to the configuration example and validation in config.py, ensuring users can select between ISOLATED and CROSS modes.
- Updated HyperliquidClient to log the selected margin mode during order placement, improving traceability and debugging capabilities.
- Enhanced configuration logging to include the margin mode, providing clearer insights into the current settings.
Carles Sentis 1 天之前
父节点
当前提交
50b0c6a0e4
共有 4 个文件被更改,包括 14 次插入2 次删除
  1. 3 1
      config/env.example
  2. 2 0
      src/clients/hyperliquid_client.py
  3. 8 0
      src/config/config.py
  4. 1 1
      trading_bot.py

+ 3 - 1
config/env.example

@@ -13,7 +13,9 @@ HYPERLIQUID_WALLET_ADDRESS=your_api_wallet_address_here
 # Network selection: true for testnet (safe), false for mainnet (real money!)
 HYPERLIQUID_TESTNET=true
 
-# Trading
+# Margin mode for all positions: ISOLATED (recommended) or CROSS
+# ISOLATED = Each position has its own margin (safer)
+# CROSS = All positions share the same margin pool (riskier)
 HYPERLIQUID_MARGIN_MODE=ISOLATED 
 
 # ========================================

+ 2 - 0
src/clients/hyperliquid_client.py

@@ -384,6 +384,7 @@ class HyperliquidClient:
             # Add margin mode from config
             if Config.HYPERLIQUID_MARGIN_MODE:
                 order_params['marginMode'] = Config.HYPERLIQUID_MARGIN_MODE.lower()
+                logger.debug(f"📊 Using margin mode: {Config.HYPERLIQUID_MARGIN_MODE} (sent as: {order_params['marginMode']})")
 
             logger.info(f"Placing limit order: {side} {amount} {symbol} @ {price} with params {order_params}")
             order = self.sync_client.create_limit_order(symbol, side, amount, price, params=order_params)
@@ -417,6 +418,7 @@ class HyperliquidClient:
             # Add margin mode from config
             if Config.HYPERLIQUID_MARGIN_MODE:
                 order_params['marginMode'] = Config.HYPERLIQUID_MARGIN_MODE.lower()
+                logger.debug(f"📊 Using margin mode: {Config.HYPERLIQUID_MARGIN_MODE} (sent as: {order_params['marginMode']})")
             
             # Hyperliquid requires a price for market orders for slippage protection.
             # This must be passed as the 'price' argument, not within 'params'.

+ 8 - 0
src/config/config.py

@@ -83,6 +83,13 @@ class Config:
         if not cls.HYPERLIQUID_SECRET_KEY:
             logger.error("❌ HYPERLIQUID_SECRET_KEY (API generator key) is required")
             return False
+        
+        # Validate margin mode
+        valid_margin_modes = ['ISOLATED', 'CROSS']
+        if cls.HYPERLIQUID_MARGIN_MODE not in valid_margin_modes:
+            logger.error(f"❌ HYPERLIQUID_MARGIN_MODE '{cls.HYPERLIQUID_MARGIN_MODE}' is not valid. Valid options: {valid_margin_modes}")
+            return False
+            
         return True
 
     @classmethod
@@ -172,6 +179,7 @@ class Config:
         logger.info(f"  📡 Hyperliquid Testnet: {cls.HYPERLIQUID_TESTNET}")
         logger.info(f"  🏠 Wallet Address: {'✅ Set' if cls.HYPERLIQUID_WALLET_ADDRESS else '❌ Missing'}")
         logger.info(f"  🔑 API Generator Key: {'✅ Set' if cls.HYPERLIQUID_SECRET_KEY else '❌ Missing'}")
+        logger.info(f"  ⚖️ Margin Mode: {cls.HYPERLIQUID_MARGIN_MODE}")
         logger.info(f"  📱 Telegram: {'✅ Enabled' if cls.TELEGRAM_ENABLED else '❌ Disabled'}")
         logger.info(f"  🔍 Log Level: {cls.LOG_LEVEL}")
         logger.info(f"  💾 Log to File: {cls.LOG_TO_FILE}")

+ 1 - 1
trading_bot.py

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