project-structure.md 7.5 KB

🏗️ Project Structure

Complete directory structure and file organization for the Hyperliquid Trading Bot

📁 Directory Overview

trader_hyperliquid/
├── 📂 src/                     # Source code
│   ├── 🐍 telegram_bot.py     # Main Telegram bot
│   ├── 🔗 hyperliquid_client.py # Hyperliquid API client
│   ├── ⚙️ config.py           # Configuration management
│   └── 📊 trading_stats.py    # Statistics tracking
├── 📂 tests/                   # Test suite
│   ├── 📋 README.md           # Test documentation
│   ├── 🏃 run_all_tests.py    # Test runner
│   ├── 🧪 test_config.py      # Configuration tests
│   ├── 💰 test_balance.py     # Balance tests
│   ├── 📈 test_perps_commands.py # Trading tests
│   ├── 🚪 test_exit_command.py # Exit command tests
│   └── 📋 test_order_management.py # Order management tests
├── 📂 docs/                    # Documentation
│   ├── 📚 README.md           # Documentation index
│   ├── 📝 commands.md         # Command reference
│   ├── 🚀 setup.md            # Setup guide
│   ├── 🏗️ project-structure.md # This file
│   └── 🚀 deployment.md       # Deployment guide
├── 📂 config/                  # Configuration files
│   └── 📄 .env.example        # Environment template
├── 📂 logs/                    # Log files
├── 📂 utils/                   # Utility scripts
├── 📂 __pycache__/            # Python cache
├── 📂 venv/                   # Virtual environment
├── 📄 README.md               # Main project README
├── 📄 requirements.txt        # Python dependencies
├── 📄 .gitignore             # Git ignore rules
├── 🐍 telegram_bot.py         # Bot entry point (legacy)
├── 🐍 trading_bot.py          # Trading bot (legacy)
└── 📊 demo_stats.py           # Demo statistics

📝 Core Source Files

🐍 src/telegram_bot.py

Main Telegram bot implementation

  • Telegram bot interface
  • Command handlers (/start, /balance, /long, /short, etc.)
  • Callback query handling
  • Message formatting
  • Safety confirmations
  • Mobile-optimized interface

Key Classes:

  • TelegramTradingBot - Main bot class

Key Methods:

  • start_command() - Bot startup
  • long_command() - Long position
  • short_command() - Short position
  • exit_command() - Close positions
  • orders_command() - Order management
  • coo_command() - Cancel orders

🔗 src/hyperliquid_client.py

Hyperliquid API client using CCXT

  • Exchange connectivity
  • Order management
  • Balance fetching
  • Position tracking
  • Market data
  • Error handling

Key Classes:

  • HyperliquidClient - API wrapper

Key Methods:

  • get_balance() - Fetch account balance
  • get_positions() - Get open positions
  • get_open_orders() - Fetch orders
  • place_market_order() - Market orders
  • place_limit_order() - Limit orders
  • cancel_order() - Cancel orders

⚙️ src/config.py

Configuration management

  • Environment variable loading
  • Validation
  • Default settings
  • Security handling

Key Classes:

  • Config - Configuration manager

Key Methods:

  • validate() - Validate settings
  • get_hyperliquid_config() - CCXT config

📊 src/trading_stats.py

Trading statistics and performance tracking

  • Trade logging
  • Performance metrics
  • Risk analysis
  • JSON persistence

Key Classes:

  • TradingStats - Statistics manager

Key Methods:

  • record_trade() - Log trades
  • get_basic_stats() - Basic metrics
  • format_stats_message() - Telegram formatting

🧪 Test Suite

🏃 tests/run_all_tests.py

Test runner for all test modules

  • Auto-discovery of test files
  • Progress tracking
  • Summary reporting
  • Pass/fail statistics

Individual Test Files

🧪 test_config.py

  • Environment variable validation
  • Configuration loading
  • CCXT format verification
  • Security checks

💰 test_balance.py

  • Balance fetching
  • CCXT integration
  • Error handling
  • Alternative methods

📈 test_perps_commands.py

  • Trading logic
  • Symbol conversion
  • Amount calculations
  • Market data fetching

🚪 test_exit_command.py

  • Position detection
  • Exit logic
  • Market order placement
  • Token parsing

📋 test_order_management.py

  • Order filtering
  • Bulk cancellation
  • Token extraction
  • API integration

📚 Documentation

📚 docs/README.md

Documentation index and navigation

  • Quick links to all guides
  • Time estimates
  • Feature overview

📝 docs/commands.md

Complete command reference

  • All commands with examples
  • Expected responses
  • Pro tips and workflows
  • Mobile optimization notes

🚀 docs/setup.md

Quick setup guide

  • 5-minute installation
  • Environment configuration
  • First run instructions

🏗️ docs/project-structure.md

This file - complete project organization

🚀 docs/deployment.md

Production deployment guide

  • Server setup
  • Process management
  • Monitoring
  • Security

⚙️ Configuration

📄 config/.env.example

Environment variable template

# Hyperliquid API
HYPERLIQUID_PRIVATE_KEY=0x...
HYPERLIQUID_TESTNET=true

# Telegram Bot
TELEGRAM_BOT_TOKEN=...
TELEGRAM_CHAT_ID=...
TELEGRAM_ENABLED=true

# Trading Settings
DEFAULT_TRADING_SYMBOL=BTC/USDC:USDC
DEFAULT_TRADE_AMOUNT=100
LOG_LEVEL=INFO

📊 Data Files

📊 demo_stats.py

Demo statistics generator for testing

📊 demo_stats.json

Sample statistics data

📂 logs/

Application log files (created at runtime)

🔄 Legacy Files

🐍 telegram_bot.py (root)

Legacy bot entry point - use src/telegram_bot.py instead

🐍 trading_bot.py (root)

Legacy trading bot - functionality moved to src/

🚀 Running the Project

From Source Directory

# Main bot
python src/telegram_bot.py

# Tests
python tests/run_all_tests.py

From Project Root

# Main bot
python -m src.telegram_bot

# Individual tests
python tests/test_config.py

📦 Dependencies

📄 requirements.txt

ccxt>=4.2.0
python-telegram-bot>=20.0
python-dotenv>=1.0.0
requests>=2.31.0

Virtual Environment

python -m venv venv
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate     # Windows
pip install -r requirements.txt

🔐 Security

Environment Variables

  • All sensitive data in .env
  • Never commit API keys
  • Use testnet for development

API Key Handling

  • Masked in logs
  • Validation before use
  • Secure storage

📱 Mobile Optimization

Telegram Interface

  • Quick action buttons
  • Clean formatting
  • Emoji indicators
  • One-tap commands

Response Design

  • Mobile-friendly layout
  • Concise information
  • Visual hierarchy
  • Touch-optimized buttons

🔄 Development Workflow

  1. Setup: Follow docs/setup.md
  2. Test: Run tests/run_all_tests.py
  3. Develop: Edit files in src/
  4. Document: Update docs/
  5. Test Again: Verify all tests pass
  6. Deploy: Follow docs/deployment.md

🎯 Key Design Principles

Modularity

  • Separate concerns
  • Clear interfaces
  • Testable components

Safety

  • Confirmation dialogs
  • Error handling
  • Testnet default

User Experience

  • Mobile-first design
  • Clear feedback
  • Professional interface

Maintainability

  • Comprehensive tests
  • Clear documentation
  • Consistent structure

Happy coding! 🚀📱