Skip to content

Commit 2e333cf

Browse files
committed
Rewrite README to be concise and project-oriented
1 parent 6a998da commit 2e333cf

1 file changed

Lines changed: 30 additions & 259 deletions

File tree

README.md

Lines changed: 30 additions & 259 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,39 @@
1-
# TradingBot: Professional Quantitative Trading Framework
1+
# TradingBot
22

3-
A comprehensive Python framework for backtesting intraday trading strategies using real market data. Built with institutional-grade architecture and sophisticated market microstructure modeling, this project demonstrates advanced quantitative finance concepts through clean, production-ready code.
3+
A simple, practical Python framework for backtesting intraday trading strategies on real or synthetic data. It includes a small set of strategies, a realistic backtester (bid/ask, latency, fees), and basic analytics.
44

5-
## 🎯 What We Built
5+
## Features
6+
- **Data**: Yahoo Finance intraday download (no API key), CSV input, or synthetic data
7+
- **Strategies**: Mean reversion, momentum, market making (extensible via `BaseStrategy`)
8+
- **Backtester**: Bid/ask matching, latency, slippage, transaction fees, average-cost P&L
9+
- **Metrics & Plots**: Sharpe, drawdown, win rate, equity curve and drawdown charts
10+
- **CLI**: Single entry point with sensible defaults
611

7-
This isn't just another trading bot—it's a complete quantitative research platform that bridges the gap between academic finance theory and practical implementation. We've created a system that:
8-
9-
- **Fetches real market data** automatically from Yahoo Finance (no API keys needed)
10-
- **Models realistic market microstructure** with proper bid-ask spread dynamics
11-
- **Implements proven trading strategies** with rigorous backtesting methodology
12-
- **Provides institutional-quality metrics** including risk-adjusted performance measures
13-
- **Demonstrates professional software architecture** suitable for quantitative finance teams
14-
15-
## 🧮 Core Financial Concepts Implemented
16-
17-
### Market Microstructure Modeling
18-
Real markets don't just have prices—they have **bid-ask spreads** that reflect liquidity costs and information asymmetry. We model this by:
19-
20-
```python
21-
# Realistic spread calculation based on volatility and volume
22-
volatility_component = returns.std() * 10000 # Vol drives spreads
23-
volume_component = np.where(volume < volume_ma, 3, 1) # Low volume = wider spreads
24-
spread_bps = base_spread + volatility_component + volume_component
25-
```
26-
27-
This captures the fundamental relationship where spreads widen during:
28-
- High volatility periods (uncertainty increases trading costs)
29-
- Low volume periods (reduced market depth)
30-
- Market stress events (flight to liquidity)
31-
32-
### Order Matching Engine
33-
Our backtester simulates realistic order execution by modeling:
34-
35-
- **Latency effects**: Orders don't execute instantly in real markets
36-
- **Slippage**: Price impact from crossing the spread
37-
- **Transaction costs**: Realistic fee structures
38-
- **Average cost accounting**: Proper P&L attribution with FIFO/weighted average methods
39-
40-
### Risk-Adjusted Performance Metrics
41-
We implement the full suite of professional performance measures:
42-
43-
- **Sharpe Ratio**: `(Return - RiskFreeRate) / Volatility` - risk-adjusted returns
44-
- **Calmar Ratio**: `AnnualizedReturn / MaxDrawdown` - return per unit of downside risk
45-
- **Maximum Drawdown**: Peak-to-trough decline measuring worst-case scenario
46-
- **Win Rate**: Percentage of profitable trades (important for strategy psychology)
47-
48-
## 🏗️ System Architecture
49-
50-
### Modular Data Pipeline
51-
```
52-
Data Sources → Normalization → Quality Assessment → Strategy Engine → Backtester → Analytics
53-
```
54-
55-
**Real Market Data Integration**: We use Yahoo Finance's free API to download actual 1-minute intraday data, then apply sophisticated microstructure models to synthesize realistic bid-ask spreads. This gives us real price movements with realistic trading costs.
56-
57-
**Data Quality Framework**: Every dataset gets comprehensive quality scoring:
58-
- Spread analysis (checking for crossed markets, unrealistic spreads)
59-
- Price dynamics validation (volatility clustering, return distributions)
60-
- Microstructure health (order size distributions, tick frequency)
61-
62-
### Strategy Implementation
63-
Each strategy inherits from `BaseStrategy` and implements `on_tick()` for real-time decision making:
64-
65-
**Mean Reversion**:
66-
```python
67-
z_score = (current_price - rolling_mean) / rolling_std
68-
if z_score > threshold: sell_signal()
69-
if z_score < -threshold: buy_signal()
70-
```
71-
Captures the tendency for prices to revert to their statistical mean.
72-
73-
**Momentum**:
74-
```python
75-
price_velocity = (current_price - historical_price) / lookback_period
76-
if velocity > threshold: buy_signal() # Price acceleration
77-
```
78-
Exploits the behavioral finance phenomenon of trend continuation.
79-
80-
**Market Making**:
81-
```python
82-
place_bid(mid_price - half_spread)
83-
place_ask(mid_price + half_spread)
84-
```
85-
Provides liquidity while capturing the bid-ask spread as profit.
86-
87-
### Backtesting Engine
88-
Our simulation engine models realistic trading conditions:
89-
90-
- **Order book dynamics**: Proper limit order matching against historical bid/ask
91-
- **Execution timing**: Configurable latency between order submission and fill
92-
- **Cost modeling**: Variable transaction fees and market impact
93-
- **Position tracking**: Real-time portfolio valuation with mark-to-market
94-
95-
## 🚀 Getting Started
96-
97-
### Installation
12+
## Installation
9813
```bash
9914
git clone <your-repo-url>
10015
cd TradingBot
10116
pip install -r requirements.txt
10217
```
10318

104-
### Basic Usage - Real Market Data
105-
The system defaults to fetching real market data automatically:
106-
19+
## Quick Start
10720
```bash
108-
# Trade Apple stock with mean reversion strategy
21+
# Run with real market data from Yahoo Finance (e.g., AAPL)
10922
python main.py --symbol AAPL
11023

111-
# Tesla momentum trading with custom parameters
24+
# Choose a different strategy and parameters
11225
python main.py --symbol TSLA --strategy momentum --lookback 30 --threshold 0.002
11326

114-
# Market making on SPY with position limits
27+
# Market making with position limits
11528
python main.py --symbol SPY --strategy market_making --max-position 200
11629
```
11730

118-
### Advanced Configuration
31+
### More Options
11932
```bash
33+
# Show all CLI options
34+
python main.py -h
35+
36+
# Advanced configuration example
12037
python main.py \
12138
--symbol NVDA \
12239
--strategy mean_reversion \
@@ -128,169 +45,23 @@ python main.py \
12845
--period 5d
12946
```
13047

131-
### Multi-Asset Demo
132-
```bash
133-
python demo.py # Runs backtests across multiple symbols and strategies
134-
```
135-
136-
## 📊 Real Data Example
137-
138-
When you run the system, you'll see output like this:
139-
140-
```
141-
🔄 Fetching real market data for AAPL...
142-
✅ Downloaded 1,946 bars of real market data for AAPL
143-
144-
📊 MARKET DATA QUALITY ASSESSMENT
145-
============================================================
146-
📈 Dataset Overview:
147-
• Total ticks: 1,946
148-
• Time span: 6 days 06:29:00
149-
• Tick frequency: 0.2/min
150-
151-
💰 Price Dynamics:
152-
• Mean price: $233.04
153-
• Annualized volatility: 25.1%
154-
• Return skew: -0.15 (slightly left-tailed)
155-
• Return kurtosis: 4.2 (fat tails - typical for financial data)
156-
157-
📏 Spread Analysis:
158-
• Mean spread: 9.6 bps (realistic for large-cap stock)
159-
• Median spread: 8.7 bps
160-
• Tight spreads (<5 bps): 1.5%
161-
• Wide spreads (>20 bps): 2.2%
162-
163-
🎯 Overall Quality Score: 100/100 ✅ Excellent
164-
165-
📈 Performance Results:
166-
========================================
167-
Sharpe Ratio: 1.84 (good risk-adjusted returns)
168-
Max Drawdown: -12.3% (acceptable downside risk)
169-
Win Rate: 0.52 (slightly better than random)
170-
```
171-
172-
## � What Makes This Professional
173-
174-
### 1. Real Market Data Integration
175-
Instead of toy datasets, we use actual market data with realistic microstructure properties. This means spreads, volatility, and trading costs reflect real-world conditions.
176-
177-
### 2. Sophisticated Risk Management
178-
- Position limits prevent excessive exposure
179-
- Transaction cost modeling includes both fixed fees and market impact
180-
- Realistic latency simulation prevents unrealistic alpha generation
181-
182-
### 3. Proper Statistical Methods
183-
- Annualized Sharpe ratios with correct time-scaling
184-
- Drawdown analysis for tail risk assessment
185-
- Return distribution analysis (skewness, kurtosis) for model validation
186-
187-
### 4. Production-Ready Architecture
188-
- Modular design allows easy extension with new data sources
189-
- Comprehensive error handling and data validation
190-
- Professional logging and quality assessment
191-
- CLI interface suitable for automated trading research
192-
193-
## 🧪 For Quantitative Researchers
194-
195-
This framework demonstrates several advanced concepts:
196-
197-
**Volatility Clustering**: Our real data exhibits the stylized fact that high volatility periods cluster together, which our strategies can exploit.
198-
199-
**Bid-Ask Spread Dynamics**: We model spreads as functions of volatility and volume, capturing the inventory risk and adverse selection costs that market makers face.
200-
201-
**Statistical Arbitrage**: The mean reversion strategy exploits temporary mispricings that occur due to market microstructure noise and temporary supply/demand imbalances.
202-
203-
**Market Impact**: Our slippage modeling captures the reality that large orders move prices—a key consideration in institutional trading.
204-
205-
## 🔧 Extending the Framework
206-
207-
### Adding New Data Sources
208-
Implement the `DataSourceInterface`:
209-
210-
```python
211-
class YourDataSource(DataSourceInterface):
212-
def fetch_data(self, symbol, **kwargs):
213-
# Your data fetching logic
214-
return normalized_dataframe
215-
216-
def validate_data(self, df):
217-
# Your validation logic
218-
return is_valid
219-
```
220-
221-
### Creating Custom Strategies
222-
```python
223-
class YourStrategy(BaseStrategy):
224-
def on_tick(self, market_data, current_position):
225-
# Your trading logic
226-
return list_of_orders
227-
```
228-
229-
### Parameter Optimization
230-
```python
231-
from tuning import grid_search_strategy
232-
233-
results = grid_search_strategy(
234-
data, 'mean_reversion',
235-
{'lookback': [30, 60, 120], 'threshold': [0.5, 1.0, 1.5]}
236-
)
237-
```
238-
239-
## 📚 Mathematical Foundation
240-
241-
The framework implements several key financial mathematics concepts:
242-
243-
- **Geometric Brownian Motion**: Realistic price process modeling
244-
- **Ornstein-Uhlenbeck Process**: Mean-reverting dynamics in spread modeling
245-
- **Kelly Criterion**: Optimal position sizing (can be extended)
246-
- **Value at Risk**: Downside risk quantification through drawdown analysis
247-
- **Information Ratio**: Risk-adjusted alpha measurement
248-
249-
## ⚠️ Important Notes
250-
251-
This is a research and educational framework. While it uses real market data and implements professional-grade backtesting, it should not be used for live trading without extensive additional testing and risk management systems.
252-
253-
The performance results shown are historical simulations and do not guarantee future performance. All trading involves risk of loss.
254-
255-
---
256-
257-
**Built for quantitative researchers who demand both theoretical rigor and practical implementation quality.** 📈
258-
259-
## CLI Options
260-
Run `python main.py -h` for all options. Key flags:
261-
- `--strategy {mean_reversion,momentum,market_making}`
262-
- `--lookback INT`, `--threshold FLOAT`, `--max-position INT`
263-
- `--latency-ms INT`, `--fee-per-share FLOAT`, `--slippage-bps FLOAT`
264-
- `--csv PATH` or `--synthetic`
265-
- `--no-plot`
266-
267-
## Parameter Tuning
268-
See `tuning.py` for a simple grid search utility:
269-
```python
270-
from tuning import grid_search_strategy
271-
summary, best_metrics, best_results = grid_search_strategy(data, 'mean_reversion', {
272-
'lookback': [30, 60, 120],
273-
'threshold': [0.3, 0.5, 0.7],
274-
'max_position': [50, 100]
275-
})
276-
print(summary.head())
277-
```
278-
279-
## Architecture
48+
## Project Structure
28049
- `main.py` — CLI entry point
281-
- `data_loader.py` — I/O, CSV normalization, synthetic generator
282-
- `strategies/` — Strategy implementations inherit `BaseStrategy`
283-
- `backtester.py` — Matching on bid/ask, latency, fees, slippage, avg-cost P&L
284-
- `metrics.py` — Sharpe, drawdown, Calmar, win-rate
285-
- `plotting.py` — Equity, drawdown, exposure, P&L distribution, spread
286-
- `tests/` — Basic pytest to ensure runs complete
50+
- `data_loader.py` — Yahoo Finance download, CSV I/O, synthetic data
51+
- `backtester.py` — Core execution (bid/ask, latency, fees, slippage, P&L)
52+
- `strategies/``base.py`, `mean_reversion.py`, `momentum.py`, `market_making.py`
53+
- `metrics.py` — Sharpe, drawdown, win rate, etc.
54+
- `plotting.py` — Equity, drawdown, exposure, P&L distribution
55+
- `tests/` — Basic pytest for a synthetic run
56+
57+
## Extending
58+
- Add a strategy by subclassing `strategies/base.py` and implementing `on_tick()`
59+
- Add a data source under `data_sources/` implementing the base interface
28760

28861
## Development
289-
- Lint/type-check as desired (ruff/mypy can be added)
290-
- Run tests:
29162
```bash
29263
pytest -q
29364
```
29465

29566
## Disclaimer
296-
This project is for educational and research purposes only; not investment advice or for live trading.
67+
This project is for educational and research purposes only. It is not investment advice and not intended for live trading.

0 commit comments

Comments
 (0)