Skip to content

Implement ALICE/DECISION PARLIAMENT institutional-grade IBKR broker adapter - #5

Draft
Netrade1 with Copilot wants to merge 4 commits into
mainfrom
copilot/upgrade-ibkr-broker-adapter
Draft

Implement ALICE/DECISION PARLIAMENT institutional-grade IBKR broker adapter#5
Netrade1 with Copilot wants to merge 4 commits into
mainfrom
copilot/upgrade-ibkr-broker-adapter

Conversation

Copilot AI commented Apr 6, 2026

Copy link
Copy Markdown

Adds alice_broker.py — a single-file, broker-agnostic Python trading adapter for Interactive Brokers, covering the full order lifecycle from intent validation through execution, persistence, and event emission.

Core schemas & persistence

  • Enums/dataclasses: Instrument, OrderIntent, RiskEnvelope, DecisionContext, ExecutionReport, PositionSnapshot, AccountSnapshot, OpenOrder, BracketState, etc.
  • ExecutionJournal: SQLite WAL-mode persistence for intents, risk checks, and execution reports across three tables

Risk engine

Nine-stage RiskEngine.check() with quantity throttling at each gate:

  1. Kill-switch (ALICE_KILL_SWITCH env var)
  2. Min confidence threshold
  3. Short-selling permission
  4. Notional cap (throttles qty to fit)
  5. Cash availability
  6. Symbol exposure cap
  7. Max position quantity
  8. Stop-distance × qty order risk
  9. Daily realized loss limit

PaperBroker

  • Instant fills for market orders; limit/stop simulation via background price-update thread
  • _PaperPosition tracks avg cost, realized PnL, and handles long/short transitions correctly
  • Bracket orders (submit_bracket) activate protective stop-loss + take-profit child orders on entry fill
  • Open-order registry with cancel_order support
  • EventHook bus emitting on_fill, on_reject, on_cancel, on_status_change

IBKRBroker (scaffold)

  • ib_insync-backed adapter; gracefully degrades when ib_insync is not installed
  • Native bracket transmission using IBKR parentId + transmit=False pattern
  • _on_order_status / _on_error event handlers wired to ib_insync callbacks
  • Contract builders for STOCK, FOREX, FUTURE, CRYPTO

BrokerFactory

# Paper simulation (no TWS required)
broker = BrokerFactory.create(Environment.PAPER, journal=journal, risk_engine=risk)

# Live/paper IBKR (TWS or IB Gateway must be running)
broker = BrokerFactory.create(Environment.LIVE, host="127.0.0.1", port=7497, client_id=1)

broker.connect()
report = broker.submit_order(intent)        # single order
result = broker.submit_bracket(intent)      # entry + stop + take-profit
broker.cancel_order(intent.client_order_id)
broker.disconnect()

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI changed the title [WIP] Enhance institutional-grade IBKR broker adapter Implement ALICE/DECISION PARLIAMENT institutional-grade IBKR broker adapter Apr 6, 2026
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.

3 participants