Skip to main content
Glama

IB Analytics MCP Server

by knishioka
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

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/knishioka/ib-sec-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server