Self-hosted Telegram bot for Interactive Brokers. Manage your portfolio, execute trades, and monitor positions — all from Telegram.
- Multi-account trading — manage multiple IBKR accounts from a single bot, with per-account allocation and position limits
- Options-first — built-in option chain wizard for LEAPS, with deep ITM strike selection and automatic contract matching
- Safety-first execution — market hours checks, duplicate signal detection, position limit enforcement, and mandatory confirmation before every trade
- Real-time portfolio sync — periodic position snapshots, P&L tracking, and Flex Web Service integration for deposit/withdrawal history
- Margin compliance — configurable soft (alert) or hard (auto-sell) margin enforcement per account
| 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) |
- An Interactive Brokers account with API access enabled
- A Telegram bot token from @BotFather
- Docker and Docker Compose
-
Copy the example files:
cp .env.example .env cp config.example.yaml config.yaml cp docker-compose.example.yml docker-compose.yml
-
Edit
.envwith your Telegram bot token, admin chat ID, and IBKR credentials. -
Edit
config.yamlwith 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
/statusin Telegram to verify connectivity.
accounts:
- 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| 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) |
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 |
This project uses the gnzsnz/ib-gateway Docker image. Key settings:
- 2FA: Required once per week (Sunday). The container auto-restarts and relogins after timeout.
- Session persistence:
SAVE_TWS_SETTINGS=yespreserves settings across restarts. - API access:
READ_ONLY_API=nois 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)
- Bot (
bot.py): Telegram command handling, inline keyboards, confirmation flows - Executor (
executor.py): IBKR connection management, order sizing, option chain resolution - Safety (
safety.py): Market hours, position limits, duplicate detection - DB (
db.py): Trade log, signal history, deposit tracking - App (
app.py): Orchestration — wires bot, executor, DB, and periodic sync
Contributions are welcome. Please open an issue first to discuss what you'd like to change.
- Fork the repository
- Create a feature branch (
git checkout -b feature/my-feature) - Run tests (
pytest tests/ -v) - Run linting (
ruff check .) - Open a pull request