A modular trading bot for cryptocurrency trading using Python, CCXT for exchange connectivity, and comprehensive backtesting capabilities.
- Modular Architecture: Separate components for Strategy, Exchange Interface, and Risk Management
- Robust Backtesting Engine: Simulate trades with historical data, commission fees, and slippage
- Multiple Strategies: EMA Crossover (with more to come)
- Performance Metrics: Sharpe Ratio, Max Drawdown, Win Rate, Profit Factor
- Risk Management: Hard-coded Stop Loss and Take Profit with position sizing
- CLI Dashboard: Real-time terminal output and trade logging
- TDD Approach: Test-driven development with comprehensive test suite
- Language: Python 3.10+
- Libraries:
ccxt- Exchange connectivitypandas&pandas_ta- Data analysis and indicatorsnumpy- Numerical computationstabulate- CLI formattingpytest- Testing framework
- Database: SQLite (for trade logging)
test-tradebot/
├── src/
│ ├── config.py # Global configuration
│ ├── data/
│ │ └── ccxt_loader.py # OHLCV data fetching
│ ├── indicators/
│ │ └── ema_calculator.py # EMA calculations
│ ├── strategy/
│ │ └── ema_crossover.py # EMA crossover strategy
│ ├── backtesting/
│ │ ├── backtest_engine.py # Backtesting engine
│ │ └── performance_metrics.py # Metrics calculation
│ ├── risk/ # Risk management (coming soon)
│ └── database/ # Database operations (coming soon)
├── tests/
│ ├── test_indicators.py
│ ├── test_data_loader.py
│ ├── test_strategy.py
│ └── test_backtester.py
├── backtest_runner.py # Main backtest script
├── requirements.txt
└── README.md
- Clone the repository:
git clone <repository-url>
cd test-tradebot- Create a virtual environment:
python -m venv venv
source venv/Scripts/activate # On Windows- Install dependencies:
pip install -r requirements.txtEdit src/config.py to customize:
- Trading Pair: Symbol (e.g., "ETH/USDT")
- Timeframe: Candle size (e.g., "4h")
- Backtesting Period: Number of days of historical data
- Risk Parameters: Stop Loss, Take Profit, Max Position Size
- Strategy Parameters: EMA periods (fast/slow)
- Commission & Slippage: Exchange-specific fees
Run the complete test suite:
pytestRun specific test file:
pytest tests/test_backtester.py -vRun with coverage:
pytest --cov=src tests/Run the backtester with the configured strategy:
python backtest_runner.pyThis will:
- Fetch historical OHLCV data from Binance via CCXT
- Calculate technical indicators (EMA)
- Generate trading signals based on strategy
- Simulate trade execution with realistic commission/slippage
- Calculate performance metrics
- Display results in a formatted table
================================================================================
TRADING BOT - BACKTESTING ENGINE
================================================================================
Configuration:
Symbol: ETH/USDT
Timeframe: 4h
Backtest Period: 90 days
Initial Capital: $1,000.00
Strategy: EMA Crossover (9/21)
Stop Loss: 3%
Take Profit: 8%
[1] Loading data from binance...
Loaded 540 candles
Date range: 2025-11-03 to 2026-03-01
[2] Generating trading signals...
Buy signals: 8
Sell signals: 7
[3] Running backtest...
================================================================================
BACKTEST RESULTS
================================================================================
Performance Summary:
┌──────────────────┬─────────────────┐
│ Metric │ Value │
├──────────────────┼─────────────────┤
│ Initial Capital │ $1,000.00 │
│ Final Value │ $1,245.32 │
│ Total Return │ $245.32 │
│ Return % │ 24.53% │
│ Total Trades │ 7 │
│ Winning Trades │ 5 │
│ Losing Trades │ 2 │
│ Win Rate │ 71.43% │
│ Max Drawdown │ -8.32% │
│ Sharpe Ratio │ 1.85 │
└──────────────────┴─────────────────┘
- Signal: When the fast EMA (9) crosses above the slow EMA (21), a BUY signal is generated
- Exit: When the fast EMA crosses below the slow EMA, a SELL signal is generated
- Risk Management: Every position has a hard stop loss (3%) and take profit (8%)
- Implement Paper Trading Mode (real-time with virtual balance)
- Add Live Trading Mode with API key support
- Implement additional strategies (RSI, Bollinger Bands, etc.)
- Add CLI dashboard for real-time monitoring
- Database logging for all trades
- Multi-symbol backtesting
- Walk-forward analysis
- Monte Carlo analysis
This trading bot is provided for educational purposes only. Cryptocurrency trading carries significant risk. Always:
- Use small amounts when testing with live trading
- Thoroughly backtest any strategy before live deployment
- Implement proper risk management
- Never trade capital you cannot afford to lose
Pull requests are welcome. For major changes, please open an issue first.
MIT License - See LICENSE file for details