{"id":30141275,"url":"https://github.com/edwinhere/ib","last_synced_at":"2025-08-11T04:36:19.454Z","repository":{"id":300697257,"uuid":"1006472983","full_name":"edwinhere/ib","owner":"edwinhere","description":"Interactive Brokers API Haskell Implementation","archived":false,"fork":false,"pushed_at":"2025-06-23T05:38:51.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-23T06:19:53.586Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edwinhere.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-22T10:58:47.000Z","updated_at":"2025-06-23T05:38:54.000Z","dependencies_parsed_at":"2025-06-23T06:29:56.695Z","dependency_job_id":null,"html_url":"https://github.com/edwinhere/ib","commit_stats":null,"previous_names":["edwinhere/ib"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/edwinhere/ib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edwinhere%2Fib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edwinhere%2Fib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edwinhere%2Fib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edwinhere%2Fib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edwinhere","download_url":"https://codeload.github.com/edwinhere/ib/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edwinhere%2Fib/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269832167,"owners_count":24482301,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-11T04:36:15.597Z","updated_at":"2025-08-11T04:36:19.444Z","avatar_url":"https://github.com/edwinhere.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Interactive Brokers Haskell Client\n\nA Haskell implementation of the Interactive Brokers TWS/Gateway API client with comprehensive testing.\n\n## Features\n\nThis client implements the core Interactive Brokers API protocol, including:\n\n### Core Functionality\n- **Connection \u0026 Handshake**: Establishes TCP connection with proper protocol handshake\n- **Message Framing**: Handles length-prefixed message encoding/decoding\n- **Basic Requests**: Current time, contract details, historical data, market data\n\n### Market Data Features\n- **Market Data Streaming**: Real-time price and size updates\n- **Market Depth (Order Book)**: Level 2 market depth data\n- **Real-time Bars**: 5-second real-time bar data\n- **Tick-by-Tick Data**: High-resolution tick data (Last, AllLast, BidAsk, MidPoint)\n\n### Trading \u0026 Analysis Features\n- **Option Calculations**: \n  - Calculate implied volatility from option price\n  - Calculate option price from volatility\n  - Greeks calculation (Delta, Gamma, Vega, Theta)\n- **Account Information**: Account summaries, positions, P\u0026L data\n\n### Advanced Features\n- **Contract Details**: Full contract specification and lookup\n- **Historical Data**: Time-series data with various bar sizes\n- **Managed Accounts**: Support for Financial Advisor accounts\n\n## Usage\n\n### Prerequisites\n- Interactive Brokers TWS or IB Gateway running\n- Paper trading account recommended for testing\n- Haskell Stack build system\n\n### Building and Running\n\n```bash\n# Build the project\nstack build\n\n# Run the client (connects to paper trading port 7497)\nstack run\n```\n\n### Configuration\n\nThe client connects to:\n- **Paper Trading**: `localhost:7497` (default)\n- **Live Trading**: `localhost:7496`\n\nEdit `src/Lib.hs` to change connection settings.\n\n## Testing\n\nThe project includes comprehensive tests that convert the main application functionality to testable assertions:\n\n### Running Tests\n\n```bash\n# Run unit tests only\nstack test ib:test:ib-test\n\n# Run integration tests only (requires IB Gateway/TWS running)\nstack test ib:test:ib-integration-test\n\n# Run all tests\nstack test\n```\n\n### Test Strategy\n\n#### Unit Tests (`test/Spec.hs`)\n- **Codec Testing**: Message encoding/decoding validation\n- **Framing Testing**: Network message framing\n- **Protocol Testing**: Individual message type validation\n\n#### Integration Tests (`test/IntegrationTest.hs`)\nIntegration tests convert the main application functionality to testable assertions:\n\n- **Connection Testing**: Handshake and connection establishment\n- **Contract Details**: Request and validate contract information\n- **Historical Data**: Request and validate historical price data\n- **Market Data**: Real-time market data subscription\n- **Market Depth**: Order book data validation\n- **Real-time Bars**: Time-based bar data\n- **Tick-by-Tick Data**: High-frequency tick data\n- **Account Summary**: Account information retrieval\n- **Positions**: Current position data\n- **Option Calculations**: Implied volatility and option pricing\n- **Current Time**: Server time synchronization\n\n### Test Best Practices\n\n1. **Assertion-Based Testing**: All tests use proper assertions (`shouldBe`, `shouldSatisfy`)\n2. **Timeout Handling**: Integration tests use timeouts to prevent hanging\n3. **Error Handling**: Tests validate both success and error conditions\n4. **Message Collection**: Tests collect and validate all response types\n5. **Connection Management**: Proper setup and teardown of connections\n\n### Test Configuration\n\n```haskell\n-- Test configuration\ntestHost :: ByteString\ntestHost = \"127.0.0.1\"\n\ntestPort :: Int\ntestPort = 7497\n\ntestTimeout :: Int\ntestTimeout = 30000000 -- 30 seconds\n```\n\n### Running Specific Tests\n\n```bash\n# Run specific test pattern\nstack test --test-arguments=\"-m 'Connection'\"\n\n# Run with verbose output\nstack test --test-arguments=\"--verbose\"\n\n# Run integration tests with specific focus\nstack test ib:test:ib-integration-test --test-arguments=\"-m 'Market Data'\"\n```\n\n## Protocol Implementation\n\nThis client implements the IB API protocol as documented in `protocol.md`, including:\n\n- **Message Framing**: 4-byte big-endian length prefix\n- **Field Encoding**: Null-terminated ASCII strings\n- **Handshake Protocol**: Multi-step connection establishment\n- **Message Types**: All major request/response message types\n\n## Architecture\n\nThe codebase is organized into several modules:\n\n- `IB.Protocol.Types`: Data types for contracts, messages, and requests\n- `IB.Protocol.Constants`: Message ID constants\n- `IB.Codec.Encoder`: Message encoding for client requests\n- `IB.Codec.Decoder`: Message decoding for server responses\n- `IB.Network.Connection`: TCP connection handling\n- `IB.Network.Framing`: Message framing and unframing\n\n## Example Usage\n\nThe main application demonstrates several features:\n\n1. **Connection**: Establishes connection with proper handshake\n2. **Contract Details**: Requests EUR/USD forex contract details\n3. **Historical Data**: Retrieves 1-month daily bars\n4. **Market Data**: Subscribes to real-time price updates\n5. **Market Depth**: Requests order book data\n6. **Real-time Bars**: Subscribes to 5-second bars\n7. **Tick-by-Tick**: Requests high-resolution tick data\n8. **Account Data**: Requests account summaries and positions\n9. **Option Calculations**: Calculates implied volatility and option prices\n\n## Development\n\n### Adding New Features\n\nTo add new API features:\n\n1. **Extend Types**: Add new data types in `IB.Protocol.Types`\n2. **Add Constants**: Define message IDs in `IB.Protocol.Constants`\n3. **Implement Encoder**: Add encoding logic in `IB.Codec.Encoder`\n4. **Implement Decoder**: Add decoding logic in `IB.Codec.Decoder`\n5. **Update Main**: Add example usage in `src/Lib.hs`\n6. **Add Tests**: Include both unit and integration tests\n\n### Adding New Tests\n\n1. **Unit Tests**: Add to `test/Spec.hs` for isolated component testing\n2. **Integration Tests**: Add to `test/IntegrationTest.hs` for end-to-end testing\n\n### Test Structure\n\n```haskell\ndescribe \"Feature Name\" $ do\n  it \"should perform expected behavior\" $ do\n    -- Setup\n    -- Action\n    -- Assertion\n    result `shouldBe` expectedValue\n```\n\n### Testing Against\n\nThe client can be tested against:\n- IB Paper Trading Gateway\n- TWS Paper Trading\n- IB Gateway (paper or live)\n\n## License\n\nThis project is provided as-is for educational and development purposes. Please ensure compliance with Interactive Brokers' API usage terms.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedwinhere%2Fib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedwinhere%2Fib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedwinhere%2Fib/lists"}