A sophisticated, enterprise-grade AI-powered cryptocurrency trading bot built with Python. This system combines advanced grid trading strategies, machine learning optimization, and comprehensive risk management to deliver professional-level automated trading capabilities for the Binance exchange.
- π Grid Trading: Automated grid trading with manual and AI-assisted parameter optimization
- π§ Machine Learning: Bayesian optimization for strategy parameter tuning
- π‘οΈ Risk Management: Multi-layer safety systems with circuit breakers and position limits
- π Data Management: Efficient data loading with Parquet caching and validation
- π₯οΈ Web Interface: Real-time monitoring dashboard and command-line interface
- π Secure Storage: Encrypted credential management and hybrid data persistence
- π Backtesting: Comprehensive strategy testing with walk-forward validation
graph TB
subgraph "User Interface Layer"
CLI[ui/cli.py - Command Interface]
DASH[ui/dashboard.py - Web Dashboard]
SEC[ui/security.py - Security]
SESS[ui/session_manager.py - Sessions]
end
subgraph "Core Trading Engine"
EXEC[core/executor.py - Trading Executor]
SAFETY[core/safety.py - Safety Manager]
STATE[core/state.py - State Management]
GRID[core/grid_engine.py - Grid Engine]
end
subgraph "AI & Strategy Layer"
BASE[ai/baseline.py - Strategy Engine]
BO[ai/bo_suggester.py - Bayesian Optimizer]
VALID[ai/validation.py - Model Validation]
end
subgraph "Simulation & Reporting"
ENGINE[sim/engine.py - Backtest Engine]
EVAL[sim/evaluation.py - Performance Analysis]
REPORTS[reports/end_of_run.py - Report Generation]
HODL[reports/hodl_benchmark.py - Benchmarks]
end
subgraph "Data Layer"
SCHEMA[data/schema.py - Data Structures]
LOADER[data/loader.py - Data Loading]
end
subgraph "Storage Layer"
REPO[storage/repo.py - Data Repository]
ARTIFACTS[storage/artifacts.py - ML Artifacts]
end
subgraph "Exchange Integration"
BINANCE[exec/binance.py - Binance API]
PAPER[exec/paper_broker.py - Paper Trading]
end
CLI --> EXEC
DASH --> REPO
EXEC --> SAFETY
EXEC --> STATE
EXEC --> GRID
EXEC --> BINANCE
BASE --> BO
BASE --> VALID
ENGINE --> EVAL
EVAL --> REPORTS
REPORTS --> HODL
LOADER --> SCHEMA
REPO --> SCHEMA
ARTIFACTS --> VALID
SAFETY --> STATE
This trading bot is built with a modular, enterprise-grade architecture that separates concerns and ensures reliability, testability, and maintainability.
py-binance-bot/
βββ π― core/ # Core Trading Engine
β βββ executor.py # Main trading execution coordinator
β βββ safety.py # Risk management & circuit breakers
β βββ sizing.py # Position sizing & fee calculations
β βββ flatten.py # Emergency position flattening
β βββ grid_engine.py # Grid trading strategy engine
β βββ state.py # State persistence & management
βββ π exec/ # Exchange Interface Layer
β βββ binance.py # Binance API client with retry logic
β βββ rate_limit.py # Smart rate limiting & request throttling
β βββ paper_broker.py # Paper trading simulation
βββ π€ ai/ # AI & Machine Learning
β βββ baseline.py # Technical indicators & baseline strategies
β βββ bo_suggester.py # Bayesian optimization for hyperparameters
β βββ validation.py # Model validation & walk-forward analysis
β βββ README.md # AI module documentation
βββ π data/ # Data Management
β βββ loader.py # Historical data fetching & caching
β βββ schema.py # Data models & validation schemas
βββ π§ͺ sim/ # Simulation & Backtesting
β βββ engine.py # Backtesting simulation engine
β βββ evaluation.py # Performance metrics & analysis
β βββ slippage.py # Realistic slippage modeling
βββ πΎ storage/ # Data Persistence
β βββ artifacts.py # Model & config artifact management
β βββ repo.py # Data repository layer
βββ π reports/ # Analytics & Reporting
β βββ end_of_run.py # Comprehensive trading reports
β βββ hodl_benchmark.py # Buy-and-hold benchmark comparison
βββ π₯οΈ ui/ # User Interface
β βββ cli.py # Command-line interface
β βββ dashboard.py # Web dashboard (future)
βββ π§ͺ tests/ # Comprehensive Test Suite (167+ tests)
β βββ test_safety.py # Risk management tests (78% coverage)
β βββ test_executor.py # Trading execution tests
β βββ test_sizing.py # Position sizing tests (86% coverage)
β βββ test_rate_limiting.py # Rate limiting tests (69% coverage)
β βββ test_binance_client.py # API client tests (68% coverage)
β βββ test_safety_clean.py # Additional safety system tests
β βββ test_executor_simple.py # Simplified executor tests
βββ main.py # Application entry point
βββ config.py # Configuration management
βββ GLOSSARY.md # Technical terms and definitions
βββ requirements.txt # Python dependencies- Python 3.10+: Modern Python version for optimal performance
- API Access: Binance account with API credentials (or compatible exchange)
- Optional: GPU for accelerated ML training and optimization
- Clone the repository:
git clone https://github.com/michaelandrewrm/py-binance-bot.git
cd py-binance-bot- Set up virtual environment:
# Create virtual environment
python -m venv trading_bot_env
# Activate (Linux/macOS)
source trading_bot_env/bin/activate
# Activate (Windows)
trading_bot_env\Scripts\activate- Install dependencies:
pip install -r requirements.txt
# Or for minimal installation
pip install -r requirements-minimal.txt- Configure credentials:
python -m ui.cli config initpython -m ui.cli config show- Validate configuration:
python -m ui.cli config validateStart with risk-free paper trading to familiarize yourself with the system:
# Start grid trading with manual parameters
python -m ui.cli grid start BTCUSDC --mode manual
# Check trading status
python -m ui.cli grid status
# View dashboard
python -m ui.dashboard# AI-assisted parameter selection
python -m ui.cli grid start BTCUSDC --mode ai --confirm-live
# Monitor with web dashboard
streamlit run ui/dashboard.pyTest and optimize your strategies using historical data:
# Run backtest on historical data
python -m ui.cli backtest run --strategy grid --symbol BTCUSDC --days 30
# Optimize parameters
python -m ui.cli model optimize --strategy baseline --symbol BTCUSDCThe following table provides an overview of the project's modular architecture:
| Directory | Purpose | Key Features |
|---|---|---|
π€ ai/ |
Machine learning and strategy intelligence | Bayesian optimization, baseline strategies, model validation |
π― core/ |
Core trading infrastructure | Trading executor, safety systems, grid engine, state management |
π data/ |
Data structures and loading | Market data schemas, caching, validation |
π exec/ |
Exchange integration | Binance API client, paper trading, rate limiting |
π reports/ |
Performance analysis and reporting | Comprehensive reports, HODL benchmarks, chart generation |
π§ͺ sim/ |
Backtesting and simulation | Event-driven backtesting, performance evaluation, slippage models |
πΎ storage/ |
Data persistence and ML artifacts | SQLite repository, ML model storage, hybrid data management |
π₯οΈ ui/ |
User interfaces | CLI, web dashboard, session management, security |
Create a .env file in the project root with your API credentials:
# API Configuration
BINANCE_API_KEY=your_api_key_here
BINANCE_API_SECRET=your_api_secret_here
BINANCE_TESTNET=true # Use testnet for testingUpdate the config.yaml file with your trading preferences:
trading:
default_symbol: "BTCUSDC"
default_timeframe: "5m"
max_position_size: 1000.0
risk_per_trade: 0.02
grid:
n_grids: 12
invest_per_grid: 50.0
grid_spacing_pct: 0.01
upper_price: null
lower_price: nullConfigure risk limits in the risk_limits.json file to protect your capital:
{
"max_daily_loss": 100.0,
"max_position_size": 1000.0,
"max_open_orders": 20,
"circuit_breaker_threshold": 0.05
}The trading bot includes multiple layers of protection to safeguard your investments:
- π¨ Multi-layer Risk Management: Position limits, daily loss limits, and automated circuit breakers
- β‘ Emergency Flatten: Instant position closure with
python -m ui.cli trade flatten - π Paper Trading Default: All operations default to paper trading mode for safety
- πΎ State Persistence: Automatic state saving and recovery for system continuity
- β Input Validation: Comprehensive validation of all user inputs and commands
Ensure code quality and reliability with our comprehensive test suite:
# Run all tests
python -m pytest
# Run specific test category
python -m pytest tests/test_core/
# Run with coverage
python -m pytest --cov=. --cov-report=htmlMaintain high code standards with automated tools:
# Format code
python -m black .
# Type checking
python -m mypy .
# Linting
python -m flake8 .- π¨βπ» Developer Docs: Detailed API documentation and development guides
- π Full Glossary: Complete terminology reference for trading and technical terms
- π Directory READMEs: See individual directory documentation for module-specific information
We welcome contributions from the community! To contribute:
- Fork the repository on GitHub
- Create a feature branch:
git checkout -b feature-name - Make your changes and add comprehensive tests
- Run the test suite:
python -m pytestto ensure everything works - Submit a pull request with a clear description of your changes
This project is licensed under the MIT License - see the LICENSE file for details.
This software is for educational and research purposes only. Cryptocurrency trading involves significant financial risk. The authors are not responsible for any financial losses incurred through the use of this software. Always test thoroughly with paper trading before using real funds.
- π Issues: Report bugs and feature requests on GitHub Issues
- π Documentation: See directory-specific READMEs for detailed information
- π Security: Report security vulnerabilities privately to the maintainers