Broker Integration Engine
Relevant source files
The following files were used as context for generating this wiki page:
OpenAlgo serves as a production algorithmic trading platform that functions as a unified bridge for 34 broker plugins. The Broker Integration Engine is the core system responsible for normalizing diverse broker APIs into a standardized interface, enabling external platforms like TradingView, Amibroker, and Python scripts to execute trades seamlessly across different securities and crypto derivatives.
Sources: README.md:14-18, CLAUDE.md:8-10
The engine provides a multi-layer architecture that includes a Unified REST API, a ZeroMQ-based market data bus, and a WebSocket proxy. This allows for a "Single API, Multiple Brokers" approach where traders can switch brokers without modifying their strategy code.
Sources: README.md:148-150, CLAUDE.md:162-167
Architecture Overview
The Broker Integration Engine is structured into three distinct layers to decouple broker-specific proprietary feeds from client delivery.
Market Data Pipeline
- Broker Adapters: Located in
broker/*/streaming/, these modules connect to proprietary broker feeds and normalize incoming ticks into the OpenAlgo format. - ZeroMQ Bus: Operates on a fixed port (default 5555) as a fan-in system. It decouples the feed from delivery, ensuring the adapter does not block on slow clients.
- WebSocket Proxy: Manages client connections, subscriptions, and per-symbol throttling for real-time delivery to the frontend or external SDKs.
Sources: CLAUDE.md:162-172, README.md:65-72
The diagram shows the flow from raw broker APIs through normalization adapters into the ZeroMQ bus, ultimately reaching end-user surfaces via WebSocket and REST.
Sources: CLAUDE.md:162-185, README.md:164-167
Unified API Specification
OpenAlgo maintains a standardized contract for all 34 supported plugins. This covers order management, account information, and market data.
Standardized Functionality
The engine maps broker-specific functions to a common set of capabilities, categorized as follows:
| Category | Capabilities | File Reference |
|---|---|---|
| Order Management | Place, Smart Order, Modify, Cancel, Cancel All, Close Position | api/order_api.py |
| GTT Orders | Place GTT, Modify GTT, Cancel GTT, GTT Book | api/gtt_api.py |
| Account & Portfolio | Orderbook, Tradebook, Positionbook, Holdings, Funds, Margin | api/order_api.py, api/funds.py, api/margin_api.py |
| Market Data | Quotes, Depth, History, Intervals | api/data.py |
Sources: scripts/generate_broker_compatibility.py:46-81
Symbol Format Normalization
The engine converts broker-specific symbols into a standardized OpenAlgo format across all segments:
- Equity: Base symbol (e.g.,
INFY,SBIN). - Futures:
[Base][Expiry]FUT(e.g.,BANKNIFTY24APR24FUT). - Options:
[Base][Expiry][Strike][CE/PE](e.g.,NIFTY28MAR2420800CE).
Sources: CLAUDE.md:213-220, README.md:275-276
Internal Data Flow & Invariants
ZeroMQ Invariants
The ZMQ market-data bus uses a specific connection pattern:
- SUB Binds: The WebSocket proxy's SUB socket is the single binder.
- PUB Connects: Every publisher (broker adapters and cache-invalidation publishers) connects to the bound SUB.
- Port Stability:
ZMQ_PORTis fixed by configuration and never drifts to prevent race conditions during gunicorn/eventlet execution.
Sources: CLAUDE.md:113-125
Broker Session Management
OpenAlgo supports up to 5 concurrent sessions per user. All sessions share a single server-side broker feed keyed by {broker}_{user_id}.
- Feed Persistence: Login from a second device resumes the existing broker session without tearing down the shared feed.
- Teardown Gate: Feed teardown is only triggered if the broker token has materially changed, preventing "broker session expired" errors on brokers like Flattrade or Shoonya.
Sources: CLAUDE.md:131-145
Integration Implementation
The following snippet illustrates how a Python strategy interacts with the integration engine using environment variables injected by the platform.
# The /python runner injects these automatically
import os
from openalgo import api
API_KEY = os.getenv("OPENALGO_API_KEY")
# OPENALGO_STRATEGY_EXCHANGE ensures alignment with host holiday gating
EXCHANGE = os.getenv("OPENALGO_STRATEGY_EXCHANGE", "NSE")
client = api(api_key=API_KEY)
# Standardized call regardless of the underlying broker plugin
response = client.placeorder(
strategy="SampleStrategy",
symbol="RELIANCE",
exchange=EXCHANGE,
action="BUY",
quantity=1,
price_type="MARKET",
product="MIS"
)
Sources: frontend/src/pages/python-strategy/PythonStrategyGuide.tsx:43-160, CLAUDE.md:229-231
Integration Lifecycle
The sequence below details how the engine processes an external trade request:
The sequence demonstrates the normalization of an incoming request from an external platform into a broker-specific call.
Sources: CLAUDE.md:191-197, test/test_broker_integration.py:165-175
Summary
The Broker Integration Engine is the foundational component of OpenAlgo, providing an abstraction layer that hides the complexity of individual broker APIs. By enforcing standardized symbol formats, ZMQ-based transport invariants, and unified REST/WebSocket interfaces, it allows for a highly stable and portable algorithmic trading environment across 34 different brokerage systems.
Sources: README.md:14-22, CLAUDE.md:8-13