#!/usr/bin/env python3 """ Test script for new exit command functionality """ import sys import os from pathlib import Path # Add the project root and src directory to the path project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) sys.path.insert(0, str(project_root / 'src')) from hyperliquid_client import HyperliquidClient from config import Config def test_exit_command(): """Test the exit command functionality.""" print("๐Ÿงช Testing Exit Command Functionality") print("=" * 50) try: # Test configuration if not Config.validate(): print("โŒ Configuration validation failed!") return False print(f"โœ… Configuration valid") print(f"๐ŸŒ Network: {'Testnet' if Config.HYPERLIQUID_TESTNET else 'Mainnet'}") print() # Initialize client print("๐Ÿ”ง Initializing Hyperliquid client...") client = HyperliquidClient(use_testnet=Config.HYPERLIQUID_TESTNET) if not client.sync_client: print("โŒ Failed to initialize client!") return False print("โœ… Client initialized successfully") print() # Test position fetching (required for exit command) print("๐Ÿ“Š Testing position fetching...") positions = client.get_positions() if positions is not None: print(f"โœ… Successfully fetched positions: {len(positions)} total") # Show open positions open_positions = [p for p in positions if float(p.get('contracts', 0)) != 0] if open_positions: print(f"๐Ÿ“ˆ Found {len(open_positions)} open positions:") for pos in open_positions: symbol = pos.get('symbol', 'Unknown') contracts = float(pos.get('contracts', 0)) entry_price = float(pos.get('entryPx', 0)) unrealized_pnl = float(pos.get('unrealizedPnl', 0)) position_type = "LONG" if contracts > 0 else "SHORT" print(f" โ€ข {symbol}: {position_type} {abs(contracts)} @ ${entry_price:.2f} (P&L: ${unrealized_pnl:.2f})") # Test token extraction if '/' in symbol: token = symbol.split('/')[0] print(f" โ†’ Token for exit command: {token}") print(f" โ†’ Exit command would be: /exit {token}") # Test what exit would do exit_side = "sell" if contracts > 0 else "buy" print(f" โ†’ Would place: {exit_side.upper()} {abs(contracts)} {token} (market order)") print() else: print("๐Ÿ“ญ No open positions found") print("๐Ÿ’ก To test /exit command, first open a position with /long or /short") print() else: print("โŒ Could not fetch positions") return False # Test market data fetching (required for current price in exit) print("๐Ÿ’ต Testing market data fetching...") test_tokens = ['BTC', 'ETH'] for token in test_tokens: symbol = f"{token}/USDC:USDC" market_data = client.get_market_data(symbol) if market_data: price = float(market_data['ticker'].get('last', 0)) print(f" โœ… {token}: ${price:,.2f}") else: print(f" โŒ Failed to get price for {token}") print() print("๐ŸŽ‰ Exit command tests completed!") print() print("๐Ÿ“ Exit Command Summary:") print(" โ€ข โœ… Position fetching: Working") print(" โ€ข โœ… Market data: Working") print(" โ€ข โœ… Token parsing: Working") print(" โ€ข โœ… Exit logic: Ready") print() print("๐Ÿš€ Ready to test /exit commands:") print(" /exit BTC # Close Bitcoin position") print(" /exit ETH # Close Ethereum position") return True except Exception as e: print(f"๐Ÿ’ฅ Test failed with error: {e}") import traceback traceback.print_exc() return False if __name__ == "__main__": success = test_exit_command() if success: print("\n๐ŸŽ‰ Exit command test PASSED!") print("\n๐Ÿ“ฑ Ready to test on Telegram:") print(" /exit BTC") print(" /exit ETH") sys.exit(0) else: print("\n๐Ÿ’ฅ Exit command test FAILED!") sys.exit(1)