Skip to content

Add IBKR broker support via ib_insync#60

Merged
IamJasonBian merged 1 commit into
mainfrom
IamJasonBian/ibkr-broker-support
May 9, 2026
Merged

Add IBKR broker support via ib_insync#60
IamJasonBian merged 1 commit into
mainfrom
IamJasonBian/ibkr-broker-support

Conversation

@IamJasonBian

Copy link
Copy Markdown
Owner

Summary

Scaffolds Interactive Brokers as a second broker for single-leg equity option orders, alongside the existing Robinhood path. Mirrors the surface and logging style of SafeCashBot.place_option_buy_limit_order / place_option_sell_limit_order so call sites can swap brokers without changes.

  • utils/ibkr_broker.py: IBKRBroker wrapping ib_insync, lazy-imported so dry-run paths work fully offline.
  • scripts/place_option_order_ibkr.py: CLI mirroring scripts/place_option_order.py.
  • tests/test_ibkr_broker.py: mocks ib_insync end-to-end; asserts dry_run=True does not connect or placeOrder(), and dry_run=False connects exactly once and calls placeOrder() once with a LimitOrder of the right action / qty / lmtPrice.
  • requirements.txt: ib_insync>=0.9.86.
  • .env.example: IBKR_HOST / IBKR_PORT / IBKR_CLIENT_ID / IBKR_PAPER.
  • TRADING_SYSTEM_GUIDE.md: appended a 10-line "IBKR setup" section.

Notes

  • Paper by default (port 7497). Live (port 7496) is opt-in via IBKR_PORT and IBKR_PAPER=false.
  • The IBKR account is not yet funded, and IBKR has no public REST endpoint for order placement: live execution requires TWS or IB Gateway running locally with API access enabled. This PR is the plumbing; first live paper smoke happens once TWS is up.
  • Independent of the Alpaca broker PR; no changes to safe_cash_bot.py or any RH code.

Test plan

  • pytest tests/test_ibkr_broker.py -v (2 passed)
  • python scripts/place_option_order_ibkr.py XLY 2026-09-18 120 call 1 4.50 buy (dry run, no TWS, prints banner + dry-run gate)
  • python scripts/place_option_order_ibkr.py XLY 2026-09-18 120 call 1 4.50 buy --live without ib_insync installed -> clear [ERR] import message
  • Once TWS / IB Gateway is up on 127.0.0.1:7497 with API socket enabled: end-to-end --live paper smoke

Adds Interactive Brokers as an alternate broker for single-leg equity option
orders. Mirrors the surface and logging style of SafeCashBot's option methods
so call sites can swap brokers without changes.

- utils/ibkr_broker.py: IBKRBroker class wrapping ib_insync. Lazy-imports
  ib_insync so dry-run paths work fully offline (no TWS connection attempted).
  Banner / [OK] / [ERR] / [WARN] / '='*70 dividers match safe_cash_bot.py.
- scripts/place_option_order_ibkr.py: CLI wrapper mirroring the RH script.
  Defaults to dry-run; --live requires TWS / IB Gateway on 127.0.0.1:7497.
- tests/test_ibkr_broker.py: smoke tests with mocked ib_insync. Asserts
  dry_run=True does not connect or placeOrder; dry_run=False connects exactly
  once and calls placeOrder once with a LimitOrder of the right action /
  quantity / lmtPrice.
- requirements.txt: add ib_insync>=0.9.86 (only needed for live).
- .env.example: add IBKR_HOST / IBKR_PORT / IBKR_CLIENT_ID / IBKR_PAPER.
- TRADING_SYSTEM_GUIDE.md: append IBKR setup section covering TWS / Gateway
  install, paper login, API socket port + read-only flag, trusted IP.

Defaults to paper (port 7497). Live (7496) is opt-in via IBKR_PORT and
IBKR_PAPER=false. Account is not yet funded; this PR scaffolds the plumbing.
@IamJasonBian IamJasonBian merged commit 7cc543e into main May 9, 2026
3 of 6 checks passed
@IamJasonBian IamJasonBian deleted the IamJasonBian/ibkr-broker-support branch May 9, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant