Self-hosted Telegram bot for Interactive Brokers. Manage your portfolio, execute trades, and monitor positions — all from Telegram.
This software is provided "as is" under the GPL-3.0 license. Use at your own risk. The author(s) accept no liability for financial losses. This is not financial advice — test thoroughly with paper trading accounts before using real money.
config.yaml.asset_types field in config controls which instruments are active. A built-in option chain wizard provides LEAPS selection with deep ITM strike matching and automatic contract resolution.| Command | Description |
|---|---|
/v | Full portfolio snapshot with NLV, positions, and P&L |
/buy TICKER PCT PRICE|MKT | Add to an existing position |
/sell TICKER all|half|% PRICE|MKT | Reduce or close a position |
/new | Open a new position via option chain wizard |
/info | Position details: bid/ask, Greeks, P&L |
/price TICKER | Live stock + option quote |
/orders | View and cancel open orders |
/trades | Execution history (today/week) |
/kill | Cancel all open orders |
/deposits | Deposit/withdrawal history (via IBKR Flex) |
/signals | Recent signal history |
/status | System health and connectivity |
/pending | Pending trade confirmations |
/pause | Pause IB Gateway containers (for manual IBKR login) |
Copy the example files:
cp .env.example .env
cp config.example.yaml config.yaml
cp docker-compose.example.yml docker-compose.yml
Edit .env with your Telegram bot token, admin chat ID, and IBKR credentials.
Edit config.yaml with your account names, gateway hosts, and trading preferences.
Start the stack:
docker compose up -d
Complete IB Gateway 2FA — on first start, the gateway container will wait for your two-factor authentication. Check container logs for instructions.
Send /status in Telegram to verify connectivity.
config.yamlaccounts:
- name: main
gateway_host: ib-gateway # Docker service name
gateway_port: 4003 # IB Gateway API port
max_allocation_pct: 100 # Max total portfolio allocation
max_position_pct: 15 # Max single position size
margin_mode: soft # "soft", "hard", or "off"
# max_margin_usd: 5000 # Optional margin cap in USD
trading:
order_type: LMT # LMT or MKT
limit_offset_pct: 0.5 # Offset from mid for limit orders
confirm_before_execute: true # Require Telegram confirmation
asset_types:
- options
- stocks
| Variable | Description |
|---|---|
TELEGRAM_BOT_TOKEN | Bot token from @BotFather |
TELEGRAM_ADMIN_CHAT_ID | Your Telegram user ID (admin only) |
CONFIG_PATH | Path to config.yaml (default: config.yaml) |
WEBHOOK_SECRET | Secret for webhook Bearer auth (optional, enables webhook API) |
WEBHOOK_PORT | Webhook server port (default: 8080) |
MARGIN_MODE_<NAME> | Per-account margin mode override |
MAX_MARGIN_<NAME> | Per-account margin cap override (USD) |
| Mode | Behavior |
|---|---|
off | No margin awareness — sizes based on cash only |
soft | Uses margin for position sizing, sends alerts when approaching limits |
hard | Soft behavior + automatically sells positions when margin limits are breached |
The bot optionally exposes an HTTP endpoint for receiving trade signals from external sources such as TradingView alerts, custom parsers, or other trading systems.
Endpoint: POST /api/v1/signal
Authentication: Bearer token via the Authorization header. Set WEBHOOK_SECRET in your environment to enable the webhook server.
Signal flow: Incoming signals are saved to the database and presented to the admin in Telegram for confirmation before execution — the same confirmation gate as manual commands.
Health check: GET /health returns {"status": "ok"}.
Example request:
curl -X POST http://localhost:8080/api/v1/signal \
-H "Authorization: Bearer YOUR_WEBHOOK_SECRET" \
-H "Content-Type: application/json" \
-d '{"ticker": "AAPL", "action": "BUY", "target_weight_pct": 5}'
This project uses the gnzsnz/ib-gateway-docker Docker image. Key settings:
SAVE_TWS_SETTINGS=yes preserves settings across restarts.READ_ONLY_API=no is required for order execution.See docker-compose.example.yml for the full configuration.
Telegram <-> Bot (aiogram) <-> TradeExecutor <-> IB Gateway (ib-async) <-> IBKR
| |
SQLite DB Safety Checks
(trades.db) (hours, limits, dupes)
^
Webhook API <- External Sources (optional)
(POST /api/v1/signal)
bot.py): Telegram command handling, inline keyboards, confirmation flowsexecutor.py): IBKR connection management, order sizing, option chain resolutionsafety.py): Market hours, position limits, duplicate detectiondb.py): Trade log, signal history, deposit trackingwebhook.py): HTTP server for external trade signals with Bearer authapp.py): Orchestration — wires bot, executor, DB, webhook, and periodic syncContributions are welcome. Please open an issue first to discuss what you'd like to change.
git checkout -b feature/my-feature)pytest tests/ -v)ruff check .)Content type
Image
Digest
sha256:28bc91c87…
Size
75.8 MB
Last updated
about 2 months ago
docker pull drumsergio/ibkr-telegram