|
| 1 | +# Trading Bot |
| 2 | + |
| 3 | +[](https://github.com/your-username/trading-bot/actions) |
| 4 | + |
| 5 | +A professional-grade Python framework for backtesting intraday trading strategies on US equities with comprehensive performance analysis and market microstructure simulation. |
| 6 | + |
| 7 | +## Key Features |
| 8 | + |
| 9 | +### Market Data & Infrastructure |
| 10 | +- **Multi-format data support**: CSV files, REST APIs (extensible) |
| 11 | +- **Microstructure simulation**: Realistic bid/ask order matching |
| 12 | +- **Latency modeling**: Configurable order-to-execution delay |
| 13 | +- **Transaction costs**: Variable fees and slippage simulation |
| 14 | + |
| 15 | +### Trading Strategies |
| 16 | +- **Mean reversion**: Z-score based entry/exit with lookback windows |
| 17 | +- **Momentum**: Trend-following based on price velocity |
| 18 | +- **Market making**: Two-sided limit order placement |
| 19 | +- **Extensible architecture**: Easy to add custom strategies via `BaseStrategy` |
| 20 | + |
| 21 | +### Backtesting Engine |
| 22 | +- **Real-time simulation**: Tick-by-tick order matching |
| 23 | +- **Position management**: Average cost accounting with realized/unrealized P&L |
| 24 | +- **Risk controls**: Maximum position limits per strategy |
| 25 | +- **Performance tracking**: Comprehensive equity curve and trade analytics |
| 26 | + |
| 27 | +### Analytics & Visualization |
| 28 | +- **Professional metrics**: Sharpe ratio, Calmar ratio, maximum drawdown, win rate |
| 29 | +- **Publication-quality plots**: Equity curves, drawdown analysis, P&L distributions |
| 30 | +- **Parameter optimization**: Grid search with performance ranking |
| 31 | +- **Trade analysis**: Per-fill P&L attribution and execution statistics |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +```bash |
| 36 | +git clone https://github.com/your-username/trading-bot.git |
| 37 | +cd trading-bot |
| 38 | +pip install -r requirements.txt |
| 39 | +``` |
| 40 | + |
| 41 | +## Quick Start |
| 42 | + |
| 43 | +### Basic Usage |
| 44 | +```bash |
| 45 | +# Run with synthetic data |
| 46 | +python main.py --synthetic --strategy mean_reversion |
| 47 | + |
| 48 | +# Use your own data |
| 49 | +python main.py --csv data/your_ticks.csv --strategy momentum --lookback 120 |
| 50 | +``` |
| 51 | + |
| 52 | +### Advanced Configuration |
| 53 | +```bash |
| 54 | +python main.py \ |
| 55 | + --csv data/AAPL_ticks.csv \ |
| 56 | + --strategy mean_reversion \ |
| 57 | + --lookback 60 \ |
| 58 | + --threshold 0.8 \ |
| 59 | + --max-position 200 \ |
| 60 | + --latency-ms 100 \ |
| 61 | + --fee-per-share 0.001 \ |
| 62 | + --slippage-bps 3 |
| 63 | +``` |
| 64 | + |
| 65 | +### Parameter Optimization |
| 66 | +```python |
| 67 | +from data_loader import make_synthetic_orderbook |
| 68 | +from tuning import grid_search_strategy |
| 69 | + |
| 70 | +data = make_synthetic_orderbook(periods=5000) |
| 71 | +results, best_metrics, best_bt = grid_search_strategy( |
| 72 | + data, 'mean_reversion', |
| 73 | + { |
| 74 | + 'lookback': [30, 60, 120], |
| 75 | + 'threshold': [0.5, 1.0, 1.5], |
| 76 | + 'max_position': [50, 100, 200] |
| 77 | + } |
| 78 | +) |
| 79 | +print(f"Best Sharpe: {best_metrics['sharpe_ratio']:.2f}") |
| 80 | +``` |
| 81 | + |
| 82 | +## Data Format |
| 83 | + |
| 84 | +Your CSV should contain these columns (case-insensitive): |
| 85 | +- `timestamp`: ISO format datetime (UTC preferred) |
| 86 | +- `bid`, `ask`: Best bid/offer prices |
| 87 | +- `bid_size`, `ask_size`: Quantities at best prices |
| 88 | +- `symbol` (optional): Security identifier |
| 89 | + |
| 90 | +Example: |
| 91 | +```csv |
| 92 | +timestamp,bid,ask,bid_size,ask_size,symbol |
| 93 | +2024-01-02T14:30:00Z,100.00,100.01,500,600,AAPL |
| 94 | +2024-01-02T14:30:01Z,100.01,100.02,450,550,AAPL |
| 95 | +``` |
| 96 | + |
| 97 | +## Architecture |
| 98 | + |
| 99 | +``` |
| 100 | +trading-bot/ |
| 101 | +├── main.py # CLI entry point with argparse |
| 102 | +├── data_loader.py # Data I/O and synthetic generation |
| 103 | +├── backtester.py # Core simulation engine |
| 104 | +├── strategies/ # Strategy implementations |
| 105 | +│ ├── base.py # Abstract base class |
| 106 | +│ ├── mean_reversion.py |
| 107 | +│ ├── momentum.py |
| 108 | +│ └── market_making.py |
| 109 | +├── metrics.py # Performance calculation |
| 110 | +├── plotting.py # Visualization utilities |
| 111 | +├── tuning.py # Parameter optimization |
| 112 | +└── tests/ # Unit tests |
| 113 | +``` |
| 114 | + |
| 115 | +## Performance Metrics |
| 116 | + |
| 117 | +- **Sharpe Ratio**: Risk-adjusted returns (annualized) |
| 118 | +- **Calmar Ratio**: Return/max drawdown ratio |
| 119 | +- **Maximum Drawdown**: Peak-to-trough equity decline |
| 120 | +- **Volatility**: Annualized return standard deviation |
| 121 | +- **Win Rate**: Percentage of profitable trades |
| 122 | +- **Average Trade P&L**: Mean per-trade profit/loss |
| 123 | + |
| 124 | +## Development |
| 125 | + |
| 126 | +### Testing |
| 127 | +```bash |
| 128 | +pytest -v |
| 129 | +``` |
| 130 | + |
| 131 | +### Code Quality |
| 132 | +```bash |
| 133 | +# Optional: Install dev dependencies |
| 134 | +pip install black ruff mypy |
| 135 | + |
| 136 | +# Format code |
| 137 | +black . |
| 138 | + |
| 139 | +# Lint |
| 140 | +ruff check . |
| 141 | + |
| 142 | +# Type checking |
| 143 | +mypy --strict main.py |
| 144 | +``` |
| 145 | + |
| 146 | +### Contributing |
| 147 | +1. Fork the repository |
| 148 | +2. Create a feature branch |
| 149 | +3. Add tests for new functionality |
| 150 | +4. Ensure CI passes |
| 151 | +5. Submit a pull request |
| 152 | + |
| 153 | +## License |
| 154 | + |
| 155 | +MIT License - see LICENSE file for details. |
| 156 | + |
| 157 | +## Disclaimer |
| 158 | + |
| 159 | +This software is for educational and research purposes only. It is not intended for live trading or as investment advice. Past performance does not guarantee future results. |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +*Developed for quantitative research and algorithmic trading education.* |
0 commit comments