A professional-grade Python framework for backtesting intraday trading strategies on US equities with comprehensive performance analysis and market microstructure simulation.
- Multi-format data support: CSV files, REST APIs (extensible)
- Microstructure simulation: Realistic bid/ask order matching
- Latency modeling: Configurable order-to-execution delay
- Transaction costs: Variable fees and slippage simulation
- Mean reversion: Z-score based entry/exit with lookback windows
- Momentum: Trend-following based on price velocity
- Market making: Two-sided limit order placement
- Extensible architecture: Easy to add custom strategies via
BaseStrategy
- Real-time simulation: Tick-by-tick order matching
- Position management: Average cost accounting with realized/unrealized P&L
- Risk controls: Maximum position limits per strategy
- Performance tracking: Comprehensive equity curve and trade analytics
- Professional metrics: Sharpe ratio, Calmar ratio, maximum drawdown, win rate
- Publication-quality plots: Equity curves, drawdown analysis, P&L distributions
- Parameter optimization: Grid search with performance ranking
- Trade analysis: Per-fill P&L attribution and execution statistics
git clone https://github.com/your-username/trading-bot.git
cd trading-bot
pip install -r requirements.txt# Run with synthetic data
python main.py --synthetic --strategy mean_reversion
# Use your own data
python main.py --csv data/your_ticks.csv --strategy momentum --lookback 120python main.py \
--csv data/AAPL_ticks.csv \
--strategy mean_reversion \
--lookback 60 \
--threshold 0.8 \
--max-position 200 \
--latency-ms 100 \
--fee-per-share 0.001 \
--slippage-bps 3from data_loader import make_synthetic_orderbook
from tuning import grid_search_strategy
data = make_synthetic_orderbook(periods=5000)
results, best_metrics, best_bt = grid_search_strategy(
data, 'mean_reversion',
{
'lookback': [30, 60, 120],
'threshold': [0.5, 1.0, 1.5],
'max_position': [50, 100, 200]
}
)
print(f"Best Sharpe: {best_metrics['sharpe_ratio']:.2f}")Your CSV should contain these columns (case-insensitive):
timestamp: ISO format datetime (UTC preferred)bid,ask: Best bid/offer pricesbid_size,ask_size: Quantities at best pricessymbol(optional): Security identifier
Example:
timestamp,bid,ask,bid_size,ask_size,symbol
2024-01-02T14:30:00Z,100.00,100.01,500,600,AAPL
2024-01-02T14:30:01Z,100.01,100.02,450,550,AAPLtrading-bot/
├── main.py # CLI entry point with argparse
├── data_loader.py # Data I/O and synthetic generation
├── backtester.py # Core simulation engine
├── strategies/ # Strategy implementations
│ ├── base.py # Abstract base class
│ ├── mean_reversion.py
│ ├── momentum.py
│ └── market_making.py
├── metrics.py # Performance calculation
├── plotting.py # Visualization utilities
├── tuning.py # Parameter optimization
└── tests/ # Unit tests
- Sharpe Ratio: Risk-adjusted returns (annualized)
- Calmar Ratio: Return/max drawdown ratio
- Maximum Drawdown: Peak-to-trough equity decline
- Volatility: Annualized return standard deviation
- Win Rate: Percentage of profitable trades
- Average Trade P&L: Mean per-trade profit/loss
pytest -v# Optional: Install dev dependencies
pip install black ruff mypy
# Format code
black .
# Lint
ruff check .
# Type checking
mypy --strict main.py- Fork the repository
- Create a feature branch
- Add tests for new functionality
- Ensure CI passes
- Submit a pull request
MIT License - see LICENSE file for details.
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.
Developed for quantitative research and algorithmic trading education.