52 - Broker Factory Implementation

This document describes the broker factory design that enables OpenAlgo to work with any of the 29 supported brokers while maintaining a single common interface for the WebSocket proxy system. OpenAlgo allows one user to connect to one broker at a time, and the broker factory ensures consistent implementation across all supported brokers.

Architecture Overview

┌─────────────────────────────────────────────────────────────┐
│                    WebSocket Proxy Server                    │
│                                                              │
│  ┌────────────────────────────────────────────────────────┐ │
│  │                   Broker Factory                        │ │
│  │  create_broker_adapter(broker_name) → Adapter Instance  │ │
│  └──────────────────────────┬─────────────────────────────┘ │
│                             │                                │
│     ┌───────────────────────┼───────────────────────┐       │
│     ▼                       ▼                       ▼       │
│  ┌──────────┐        ┌──────────┐           ┌──────────┐   │
│  │ Zerodha  │        │  Angel   │           │   Dhan   │   │
│  │ Adapter  │        │ Adapter  │    ...    │ Adapter  │   │
│  └────┬─────┘        └────┬─────┘           └────┬─────┘   │
│       │                   │                      │          │
│       └───────────────────┼──────────────────────┘          │
│                           │                                  │
│                           ▼                                  │
│  ┌────────────────────────────────────────────────────────┐ │
│  │              Base Broker WebSocket Adapter              │ │
│  │  • initialize()  • connect()  • subscribe()            │ │
│  │  • disconnect()  • unsubscribe()  • on_data()          │ │
│  └────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘

Broker Factory

The factory creates appropriate WebSocket adapters based on broker name:

Base Adapter Interface

All broker adapters implement this common interface:

Broker-Specific Adapters

Zerodha Adapter

Angel Adapter

Note: Angel broker sends prices in paise (1/100th of a rupee). The adapter normalizes values by dividing by 100.

Dhan Adapter

Supported Brokers (29)

Broker
Max Symbols
Depth Levels
Notes

Zerodha

3000

5

KiteTicker

Angel

1000

5, 20

Prices in paise

Dhan

1000

5, 20

DhanHQ SDK

Fyers

2000

5

Fyers API v3

Upstox

1500

5, 20

Upstox API v2

5Paisa

1000

5

5Paisa SDK

Kotak

1000

5

Neo API

IIFL

1000

5

IIFL Markets

Motilal

1000

5

Motilal API

Alice Blue

1000

5

Ant API

Finvasia

1000

5

NorenAPI

Flattrade

1000

5

Flattrade API

Firstock

1000

5

Firstock API

ICICI

1000

5

ICICIdirect

Compositedge

1000

5

Composite API

Mastertrust

1000

5

MT API

Mandot

1000

5

Mandot API

Paytm

1000

5

Paytm Money

Pocketful

1000

5

Pocketful API

Shoonya

1000

5

Shoonya API

Tradejini

1000

5

Tradejini API

Wisdom

1000

5

Wisdom Capital

Zebu

1000

5

Zebu API

Mstock

1000

5

Mstock API

Nubra

1000

5

gRPC-based streaming

Data Normalization

All adapters normalize broker data to OpenAlgo format:

Connection Pooling

For brokers with low symbol limits, connection pooling is used:

Usage in Application

Key Files

File
Purpose

websocket_proxy/broker_factory.py

Adapter factory

websocket_proxy/base_adapter.py

Abstract base class

broker/*/streaming/*_adapter.py

Broker implementations

websocket_proxy/server.py

Main proxy server

Adding a New Broker

  1. Create adapter file: broker/newbroker/streaming/newbroker_adapter.py

  2. Implement BaseBrokerWebSocketAdapter interface

  3. Handle broker-specific data normalization

  4. Register in factory (or rely on dynamic import)

Last updated