Skip to main content
Glama
knishioka

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/U1234567_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

Latest Blog Posts

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