PROJECT_SUMMARY.mdā¢11.6 kB
# IB Analytics - Project Summary
**Version**: 0.1.0
**Created**: 2025-10-06
**Author**: Kenichiro Nishioka
---
## š Project Overview
**IB Analytics** is a professional-grade portfolio analytics library for Interactive Brokers with comprehensive multi-account support. Built with modern Python best practices, type safety, and extensible architecture.
### Key Features
ā
**Multi-Account Support** - Analyze multiple IB accounts simultaneously
ā
**Type-Safe** - Pydantic v2 models with runtime validation
ā
**Async Support** - Parallel data fetching with httpx
ā
**Modular Design** - Easy to extend with new analyzers
ā
**CLI Tools** - User-friendly command-line interface
ā
**Rich Reports** - Beautiful console output with Rich library
ā
**Latest Dependencies** - Using stable 2024 versions
---
## šļø Architecture
### Layer Structure
```
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā CLI Layer (typer + rich) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Reports Layer (console/html) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Analyzers Layer (5 analyzers) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Core Logic (parser/calc/agg) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Models Layer (Pydantic v2) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā API Layer (sync + async) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
```
### Components
| Component | Files | Purpose |
|-----------|-------|---------|
| **API Client** | 3 | IB Flex Query API integration |
| **Models** | 4 | Type-safe domain models |
| **Core Logic** | 3 | Parsers, calculations, aggregation |
| **Analyzers** | 6 | Performance, cost, bond, tax, risk |
| **Reports** | 2 | Console and base report classes |
| **CLI** | 3 | Fetch, analyze, report commands |
| **Utils** | 2 | Config and validators |
**Total**: 23 Python modules, ~3,500 lines of code
---
## š¦ Dependencies (Latest Stable)
| Library | Version | Purpose |
|---------|---------|---------|
| **requests** | 2.32.5+ | HTTP API client |
| **pandas** | 2.2.3+ | Data analysis |
| **pydantic** | 2.10.0+ | Data validation |
| **httpx** | 0.27.0+ | Async HTTP |
| **rich** | 13.7.0+ | Console UI |
| **typer** | 0.12.0+ | CLI framework |
### Python Support
- ā
Python 3.9
- ā
Python 3.10
- ā
Python 3.11
- ā
Python 3.12
---
## š Available Analyzers
### 1. PerformanceAnalyzer
**Metrics**: Win rate, profit factor, ROI, risk/reward ratio
**Use Case**: Overall trading performance evaluation
### 2. CostAnalyzer
**Metrics**: Commission rates, cost efficiency, impact on P&L
**Use Case**: Cost optimization and broker comparison
### 3. BondAnalyzer
**Metrics**: YTM, duration, maturity analysis
**Use Case**: Zero-coupon bond (STRIPS) analytics
### 4. TaxAnalyzer
**Metrics**: Phantom income (OID), capital gains tax
**Use Case**: Tax liability estimation
### 5. RiskAnalyzer
**Metrics**: Interest rate scenarios, concentration risk
**Use Case**: Portfolio risk assessment
---
## š Quick Start
### Installation
```bash
# Clone or navigate to project
cd ib-sec
# Install dependencies
pip install -e .
# Verify installation
ib-sec-fetch --help
ib-sec-analyze --help
```
### Configuration
Create `.env` file:
```env
QUERY_ID=your_query_id_here
TOKEN=your_token_here
```
### Basic Usage
```bash
# 1. Fetch data
ib-sec-fetch --start-date 2025-01-01 --end-date 2025-10-05
# 2. Run analysis
ib-sec-analyze data/raw/U16231259_2025-01-01_2025-10-05.csv --all
# 3. View results in console
```
### Programmatic Usage
```python
from ib_sec_mcp import FlexQueryClient
from ib_sec_mcp.analyzers import PerformanceAnalyzer
from ib_sec_mcp.core.parsers import CSVParser
# Fetch data
client = FlexQueryClient(query_id="...", token="...")
statement = client.fetch_statement(start_date, end_date)
# Parse
account = CSVParser.to_account(statement.raw_data, start_date, end_date)
# Analyze
analyzer = PerformanceAnalyzer(account=account)
results = analyzer.analyze()
print(results)
```
---
## š Project Structure
```
ib-sec/
āāā .claude/ # Claude Code configuration
ā āāā CLAUDE.md # Project context (313 lines)
ā āāā README.md # .claude directory guide
ā āāā commands/ # Custom slash commands (5)
ā āāā analyze-all.md
ā āāā fetch-latest.md
ā āāā add-analyzer.md
ā āāā explain-architecture.md
ā āāā debug-api.md
āāā ib_sec_mcp/ # Main library package
ā āāā api/ # API client (3 files)
ā āāā core/ # Business logic (3 files)
ā āāā models/ # Data models (4 files)
ā āāā analyzers/ # Analyzers (6 files)
ā āāā reports/ # Report generators (2 files)
ā āāā utils/ # Utilities (2 files)
ā āāā cli/ # CLI tools (3 files)
āāā scripts/ # Example scripts
ā āāā example_usage.py
āāā tests/ # Test framework (to be implemented)
āāā legacy/ # Original scripts (10 files)
āāā data/ # Data storage
ā āāā raw/ # Raw CSV files
ā āāā processed/ # Analysis results
āāā pyproject.toml # Project metadata
āāā requirements.txt # Pip dependencies
āāā README.md # User documentation
āāā INSTALL.md # Installation guide
āāā CHANGELOG.md # Version history
āāā .env # API credentials (not committed)
```
---
## šÆ Design Patterns Used
### Template Method (BaseAnalyzer)
```python
class BaseAnalyzer(ABC):
@abstractmethod
def analyze(self) -> AnalysisResult:
pass
class PerformanceAnalyzer(BaseAnalyzer):
def analyze(self) -> AnalysisResult:
# Implementation
return self._create_result(...)
```
### Strategy (Reports)
```python
class BaseReport(ABC):
@abstractmethod
def render(self) -> str:
pass
class ConsoleReport(BaseReport):
def render(self) -> str:
# Console-specific rendering
```
### Factory (Parsers)
```python
account = CSVParser.to_account(csv_data, from_date, to_date)
```
### Builder (Portfolio)
```python
portfolio = Portfolio.from_accounts(accounts, base_currency="USD")
```
---
## š§ Development Workflow
### Adding New Features
1. **New Analyzer**
```bash
# Use custom command
/add-analyzer Sharpe "Calculate Sharpe ratio"
```
2. **Update Context**
```bash
# Press # in Claude Code
# "New analyzers must calculate annualized metrics"
```
3. **Format & Lint**
```bash
black ib_sec_mcp tests
ruff check ib_sec_mcp tests
mypy ib_sec_mcp
```
### Testing (Future)
```bash
# Run tests
pytest
# With coverage
pytest --cov=ib_sec_mcp --cov-report=html
# Specific test
pytest tests/test_analyzers/test_performance.py
```
---
## š Documentation
### User Documentation
- **README.md**: Quick start and usage guide
- **INSTALL.md**: Detailed installation instructions
- **CHANGELOG.md**: Version history and changes
### Developer Documentation
- **.claude/CLAUDE.md**: Project context for Claude Code
- **.claude/README.md**: Claude Code configuration guide
- **Inline Docstrings**: Google-style in all modules
### API Documentation (Future)
- Sphinx auto-generated docs
- GitHub Pages deployment
- Interactive examples
---
## šØ Code Quality Standards
### Style
- **Formatter**: Black (100 char line length)
- **Linter**: Ruff (E, F, I, N, W, UP, B, A, C4, T20, SIM)
- **Type Checker**: mypy (strict mode)
### Conventions
- **Classes**: PascalCase
- **Functions**: snake_case
- **Constants**: UPPER_SNAKE_CASE
- **Private**: _leading_underscore
### Documentation
- **Docstrings**: Required for all public APIs
- **Type Hints**: Required for all functions
- **Comments**: Explain why, not what
---
## š® Future Roadmap
### Phase 1 (Current) ā
- [x] Core library structure
- [x] Multi-account support
- [x] 5 core analyzers
- [x] CLI tools
- [x] Console reports
### Phase 2 (Next)
- [ ] Unit tests with pytest
- [ ] HTML reports with charts
- [ ] XML parser implementation
- [ ] Additional analyzers (Sharpe, Drawdown)
- [ ] Documentation site
### Phase 3 (Future)
- [ ] PDF report generation
- [ ] Web dashboard (Streamlit)
- [ ] Real-time data streaming
- [ ] Backtesting framework
- [ ] Portfolio optimization
### Phase 4 (Advanced)
- [ ] Machine learning predictions
- [ ] Automated trading signals
- [ ] Risk management alerts
- [ ] Mobile app integration
---
## š Migration from Legacy Scripts
### Before (Legacy Scripts)
```
ib-sec/
āāā analyze_performance.py # Standalone script
āāā trading_cost_analysis.py # Standalone script
āāā phantom_income_tax_analysis.py
āāā bond_analysis.py
āāā interest_rate_scenario_analysis.py
āāā comprehensive_summary_report.py
```
**Issues**:
- ā Code duplication
- ā No type safety
- ā Hard to test
- ā No multi-account support
- ā Monolithic structure
### After (New Library)
```
ib_sec_mcp/
āāā analyzers/
ā āāā performance.py # Modular
ā āāā cost.py # Reusable
ā āāā bond.py # Type-safe
ā āāā tax.py # Testable
ā āāā risk.py # Extensible
```
**Benefits**:
- ā
Modular architecture
- ā
Type safety (Pydantic)
- ā
Easy to test
- ā
Multi-account support
- ā
CLI + programmatic API
---
## š¤ Contributing
### Adding New Analyzer
1. Create `ib_sec_mcp/analyzers/your_analyzer.py`
2. Inherit from `BaseAnalyzer`
3. Implement `analyze()` method
4. Add to `__init__.py` exports
5. Update `ConsoleReport` rendering
6. Add CLI integration
7. Write tests
8. Update documentation
### Custom Slash Commands
1. Create `.claude/commands/your-command.md`
2. Write natural language instructions
3. Use `$ARGUMENTS` for parameters
4. Test with `/your-command`
5. Commit to git
---
## š Support
### Issues
- Check existing issues in git
- Include error messages and logs
- Provide minimal reproduction steps
### Questions
- Review `.claude/CLAUDE.md`
- Check README and INSTALL guides
- Use `/explain-architecture` command
---
## š License
MIT License - See LICENSE file for details
---
## š Acknowledgments
- **Interactive Brokers**: Flex Query API
- **Pydantic**: Type-safe data models
- **Rich**: Beautiful console output
- **Typer**: User-friendly CLI
- **Anthropic**: Claude Code integration
---
**Last Updated**: 2025-10-06
**Status**: Production Ready (v0.1.0)
**Maintainer**: Kenichiro Nishioka
For detailed information, see:
- [README.md](README.md) - User guide
- [INSTALL.md](INSTALL.md) - Installation
- [CHANGELOG.md](CHANGELOG.md) - Version history
- [.claude/CLAUDE.md](.claude/CLAUDE.md) - Developer context