Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

33 - Broker Folder Explanations

Overview

Each broker in OpenAlgo follows a standardized folder structure with consistent interfaces for authentication, order management, data retrieval, and symbol mapping.

Broker Directory Structure

broker/
├── zerodha/                          # Example broker
│   ├── api/
│   │   ├── __init__.py
│   │   ├── auth_api.py               # authenticate_broker()
│   │   ├── order_api.py              # place_order_api(), books, positions
│   │   ├── data.py                   # class BrokerData
│   │   ├── funds.py                  # get_margin_data()
│   │   ├── margin_api.py             # order margin preview (optional)
│   │   └── gtt_api.py                # GTT lifecycle (six broker implementations)
│   ├── mapping/
│   │   ├── transform_data.py         # OpenAlgo request to broker request
│   │   ├── order_data.py             # broker response to OpenAlgo shape
│   │   ├── margin_data.py
│   │   └── gtt_data.py
│   ├── database/
│   │   └── master_contract_db.py     # master_contract_download()
│   ├── streaming/
│   │   ├── __init__.py
│   │   ├── zerodha_adapter.py        # ZerodhaWebSocketAdapter
│   │   ├── zerodha_order_adapter.py  # order-update ingestion
│   │   ├── zerodha_mapping.py
│   │   └── zerodha_websocket.py
│   └── plugin.json                   # name, type, exchanges, leverage flag
├── dhan/
│   └── ... (same shape)
├── angel/
│   └── ... (same shape)
└── ... (38 broker plugins total)

The streaming files are named after the broker, so Angel uses broker/angel/streaming/angel_adapter.py and so on. Not every plugin ships every optional module: gtt_api.py exists for Angel One, Dhan, Fyers, Rupeezy, Upstox and Zerodha, and <broker>_order_adapter.py only where the broker pushes order updates. mapping/ and database/ have no __init__.py in most plugins.

File Explanations

1. api/auth_api.py

Handles broker authentication/OAuth flow.

2. api/order_api.py

Order management operations. All 37 plugins expose the same set.

3. api/data.py

Market data retrieval. Every plugin exposes a BrokerData class constructed with the auth token, rather than module-level functions.

4. api/funds.py

Account margin information. This module holds one function only.

5. mapping/transform_data.py

Convert OpenAlgo format to broker format.

Response mapping lives in mapping/order_data.py, which supplies map_order_data, transform_order_data, map_trade_data, transform_tradebook_data, map_position_data, transform_positions_data, map_portfolio_data, transform_holdings_data, calculate_order_statistics and calculate_portfolio_statistics.

6. database/master_contract_db.py

Symbol/token database management. Rows are written into the shared symtoken table defined by SymToken in database/symbol.py.

7. streaming/{broker}_adapter.py

Real-time data streaming adapter. The class name follows <Broker>WebSocketAdapter (capitalised broker key) and subclasses BaseBrokerWebSocketAdapter from websocket_proxy/base_adapter.py, which is how websocket_proxy/broker_factory.py resolves it dynamically.

Ticks are normalised by the adapter and published on the shared ZeroMQ bus with publish_market_data(topic, data) inherited from the base class.

8. plugin.json

Broker metadata plus a small amount of capability configuration.

Important: plugin.json carries identity plus the three capability keys supported_exchanges, broker_type and leverage_config. It does NOT contain API URLs, credentials or rate limits. Authentication methods, API endpoints and WebSocket URLs are handled directly in the broker's Python code. Of the 37 plugins, only deltaexchange declares "broker_type": "crypto" with "leverage_config": true; the other 37 declare "broker_type": "IN_stock" with "leverage_config": false.

Adding a New Broker

Step 1: Create Directory Structure

Step 2: Implement Required Files

  1. api/auth_api.py - Authentication (authenticate_broker)

  2. api/order_api.py - Orders and books

  3. api/data.py - Market data (BrokerData class)

  4. api/funds.py - Margin data (get_margin_data)

  5. mapping/transform_data.py and mapping/order_data.py - Request and response mapping

  6. database/master_contract_db.py - Master-contract download into the shared symtoken table

  7. streaming/newbroker_adapter.py - NewbrokerWebSocketAdapter (optional, needed for WebSocket feeds)

  8. plugin.json - Metadata plus supported_exchanges, broker_type, leverage_config

Step 3: Register Broker

VALID_BROKERS in .sample.env currently lists all 38 plugins.

Field Mapping Examples

Price Type Mapping

OpenAlgo
Zerodha
Dhan
Angel

MARKET

MARKET

MARKET

MARKET

LIMIT

LIMIT

LIMIT

LIMIT

SL

SL

STOP_LOSS

STOPLOSS_LIMIT

SL-M

SL-M

STOP_LOSS_MARKET

STOPLOSS_MARKET

Product Type Mapping

OpenAlgo
Zerodha
Dhan
Angel

CNC

CNC

CNC

DELIVERY

MIS

MIS

INTRADAY

INTRADAY

NRML

NRML

MARGIN

CARRYFORWARD

Exchange Mapping

OpenAlgo
Zerodha
Dhan
Angel

NSE

NSE

NSE_EQ

NSE

NFO

NFO

NSE_FNO

NFO

BSE

BSE

BSE_EQ

BSE

MCX

MCX

MCX_COMM

MCX

Reference Implementations

Best Examples

Broker
Strength

zerodha

Request-token exchange with SHA-256 checksum

dhan

Consent flow plus direct access token (get_direct_access_token)

angel

Full feature set

nubra

gRPC-based streaming, protos directory

deltaexchange

Crypto derivatives, broker_type: crypto, leverage config

dhan_sandbox

Paper trading against Dhan sandbox endpoints

Code Reference

Key Files Reference

Component
File Pattern

Auth

broker/*/api/auth_api.py

Orders

broker/*/api/order_api.py

Data

broker/*/api/data.py

Funds

broker/*/api/funds.py

Request mapping

broker/*/mapping/transform_data.py

Response mapping

broker/*/mapping/order_data.py

Symbols

broker/*/database/master_contract_db.py

WebSocket

broker/*/streaming/*_adapter.py

Metadata

broker/*/plugin.json

Adapter base class

websocket_proxy/base_adapter.py

Adapter resolution

websocket_proxy/broker_factory.py

Last updated