f4e5a9f8e0
- Fix Invalid Date display in Dashboard notifications - Fix timezone offset (8 hours) in Trading Records time display - Fix position closing failures due to commission discrepancies (fetch actual exchange position size for reduce_only orders) - Fix IBKR connection error 'no current event loop in thread' by ensuring asyncio event loop exists - Fix duplicate orders on same candle by extending signal deduplication to close signals - Add responsive design for Profile page (mobile-friendly) - Remove unused strategy_code module and database table - Fix LLM service to support multiple providers (OpenRouter, OpenAI, DeepSeek, Grok, Google) - Add auto-detection of configured LLM provider based on API key availability - Fix AI code generation to use unified LLMService with proper provider selection - Fix crypto symbol format handling (ETH/USDT no longer becomes ETH/USDT/USDT) - Fix Commission display showing '0E-8' in Trading Records - Fix P&L display for signal-only trades (show '--' for unrealized P&L) - Fix OAuth login not updating last_login_at for new users - Add migration script for notification_settings column - Update env.example with new LLM provider configurations - Remove ESLint rule that was not defined in config
Interactive Brokers Trading Module
Supports US stocks and Hong Kong stocks trading via TWS or IB Gateway.
Installation
pip install ib_insync
Or the dependency is already in requirements.txt.
Port Reference
| Client | Live Port | Paper Port |
|---|---|---|
| TWS | 7497 | 7496 |
| IB Gateway | 4001 | 4002 |
TWS / IB Gateway Configuration
- Open TWS or IB Gateway
- Go to Configure -> API -> Settings
- Enable the following options:
- ✅ Enable ActiveX and Socket Clients
- ✅ Allow connections from localhost only
- Set Socket port (refer to the table above)
- Click Apply / OK
API Endpoints
Connection Management
GET /api/ibkr/status # Get connection status
POST /api/ibkr/connect # Connect to TWS/Gateway
POST /api/ibkr/disconnect # Disconnect
Account Queries
GET /api/ibkr/account # Account information
GET /api/ibkr/positions # Current positions
GET /api/ibkr/orders # Open orders
Trading
POST /api/ibkr/order # Place order
DELETE /api/ibkr/order/<id> # Cancel order
Market Data
GET /api/ibkr/quote?symbol=AAPL&marketType=USStock
Usage Examples
Connect
curl -X POST http://localhost:5000/api/ibkr/connect \
-H "Content-Type: application/json" \
-d '{"host": "127.0.0.1", "port": 7497, "clientId": 1}'
Place Order
# Market order: buy 10 shares of AAPL
curl -X POST http://localhost:5000/api/ibkr/order \
-H "Content-Type: application/json" \
-d '{"symbol": "AAPL", "side": "buy", "quantity": 10, "marketType": "USStock"}'
# Limit order: sell 100 shares of Tencent
curl -X POST http://localhost:5000/api/ibkr/order \
-H "Content-Type: application/json" \
-d '{"symbol": "0700.HK", "side": "sell", "quantity": 100, "marketType": "HShare", "orderType": "limit", "price": 300}'
Get Positions
curl http://localhost:5000/api/ibkr/positions
Symbol Format
| Market | Format | Examples |
|---|---|---|
| US Stock | Ticker symbol | AAPL, TSLA, GOOGL |
| HK Stock | XXXX.HK or digits |
0700.HK, 00700, 700 |
Important Notes
- TWS/Gateway must be running: Ensure TWS or IB Gateway is started and logged in before using the API
- Market data subscription: Real-time quotes may require market data subscription
- Client ID: Use different clientId if multiple programs connect to the same TWS/Gateway
- Readonly mode: Set
readonly: trueto only query without trading - Multi-account: Specify
accountparameter if you have multiple sub-accounts
Troubleshooting
| Error | Cause | Solution |
|---|---|---|
| Connection failed | TWS/Gateway not running | Start and login to TWS/Gateway |
| Connection failed | Wrong port | Check API port setting in TWS/Gateway |
| Connection failed | API not enabled | Enable Socket API in TWS/Gateway settings |
| Client ID conflict | Same clientId already connected | Use a different clientId |
| Invalid contract | Wrong symbol format | Check symbol format |
Removing This Module
To remove this module, delete the following files/directories:
backend_api_python/app/services/ibkr_trading/ # Entire directory
backend_api_python/app/routes/ibkr.py # Route file
Then remove the related import and registration code in app/routes/__init__.py.
Docker Note
When running in Docker, IBKR trading requires TWS/IB Gateway to be accessible from the container. For local deployment, you can:
- Run TWS/Gateway on host machine
- Use host network mode or configure port mapping
- Set
hostto the host machine's IP address (e.g.,host.docker.internalon Docker Desktop)
Note
: IBKR connection parameters are configured per-strategy in the frontend, not via environment variables.