Browse Source

Add user parameter for Hyperliquid CCXT compatibility - Enhance position and order fetching methods to support user identification via wallet address, improving integration with CCXT standards and logging for better debugging.

Carles Sentis 6 days ago
parent
commit
11386a7766
2 changed files with 23 additions and 3 deletions
  1. 18 2
      src/hyperliquid_client.py
  2. 5 1
      src/trading_stats.py

+ 18 - 2
src/hyperliquid_client.py

@@ -189,11 +189,19 @@ class HyperliquidClient:
                 logger.error("❌ Client not initialized")
                 return None
             
-            positions = self.sync_client.fetch_positions([symbol] if symbol else None)
+            # Add user parameter for Hyperliquid CCXT compatibility
+            params = {}
+            if Config.HYPERLIQUID_PRIVATE_KEY:
+                wallet_address = Config.HYPERLIQUID_PRIVATE_KEY
+                params['user'] = f"0x{wallet_address}" if not wallet_address.startswith('0x') else wallet_address
+            
+            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'}")
             return positions
         except Exception as e:
             logger.error(f"❌ Error fetching positions: {e}")
+            logger.debug(f"💡 Attempted with params: {params}")
             return None
     
     def get_market_data(self, symbol: str) -> Optional[Dict[str, Any]]:
@@ -280,11 +288,19 @@ class HyperliquidClient:
                 logger.error("❌ Client not initialized")
                 return None
             
-            orders = self.sync_client.fetch_open_orders(symbol)
+            # Add user parameter for Hyperliquid CCXT compatibility
+            params = {}
+            if Config.HYPERLIQUID_PRIVATE_KEY:
+                wallet_address = Config.HYPERLIQUID_PRIVATE_KEY
+                params['user'] = f"0x{wallet_address}" if not wallet_address.startswith('0x') else wallet_address
+            
+            logger.debug(f"🔍 Fetching open orders with params: {params}")
+            orders = self.sync_client.fetch_open_orders(symbol, params=params)
             logger.info(f"✅ Successfully fetched open orders for {symbol or 'all symbols'}")
             return orders
         except Exception as e:
             logger.error(f"❌ Error fetching open orders: {e}")
+            logger.debug(f"💡 Attempted with params: {params}")
             return None
     
     def cancel_order(self, order_id: str, symbol: str, params: Optional[Dict] = None) -> bool:

+ 5 - 1
src/trading_stats.py

@@ -169,10 +169,14 @@ class TradingStats:
         if not self.data['trades']:
             return {
                 'total_trades': 0,
+                'buy_trades': 0,
+                'sell_trades': 0,
                 'initial_balance': self.data.get('initial_balance', 0),
                 'current_balance': 0,
                 'total_pnl': 0,
-                'days_active': 0
+                'days_active': 0,
+                'start_date': datetime.now().strftime('%Y-%m-%d') if 'start_time' not in self.data else datetime.fromisoformat(self.data['start_time']).strftime('%Y-%m-%d'),
+                'last_trade': None
             }
         
         trades_with_pnl = self.calculate_trade_pnl()