Broker

The Broker component manages the execution side of trading in TheoryCraft. It handles order placement, position tracking, trade history, and account state.

⚠️ The Broker component is currently in development.

What is the Broker?

In TheoryCraft, the Broker acts as the interface between your strategy logic and order execution. Whether you are backtesting against historical data or trading live, the Broker provides a consistent API for:

  • Placing and managing orders
  • Tracking open positions
  • Maintaining trade history
  • Reporting account balance and equity

The Broker abstracts away the differences between simulated and real execution, allowing the same strategy code to run across all environments.

Broker Responsibilities

Order Management

The Broker receives order requests from Engines and handles their lifecycle:

  • Order submission - Validate and submit market, limit, and stop orders
  • Order tracking - Monitor pending, filled, and cancelled orders
  • Order modification - Update or cancel existing orders

Position Tracking

The Broker maintains the current state of all positions:

  • Open positions with entry price and size
  • Unrealized profit and loss
  • Position history and closed trades

Account State

The Broker tracks account-level information:

  • Available balance and margin
  • Total equity
  • Realized profit and loss

Execution Modes

The Broker supports three execution modes with a unified API:

Simulated (Backtesting)

For backtesting, the Broker simulates order execution against historical market data. It models realistic fills based on available prices, accounting for factors such as slippage and spread.

Paper Trading

Paper trading uses live market data with simulated execution. This allows strategy validation in real market conditions without risking capital.

Live Trading

For live trading, the Broker connects to external brokers and exchanges to execute real orders. The same strategy logic used in backtesting runs unchanged in production.

BrokerEvent

The Broker emits BrokerEvent structures to notify Engines of execution updates:

  • Order status changes (submitted, filled, cancelled, rejected)
  • Position updates (opened, modified, closed)
  • Account balance changes

Engines consume these events alongside MarketEvents to make trading decisions.

Planned Integrations

The following broker integrations are planned:

BrokerAsset ClassesStatus
Simulated BrokerAll (backtesting)In development
Interactive BrokersStocks, Options, Futures, ForexPlanned
BinanceCryptocurrenciesPlanned

Next Steps