# kumbhani11/algo architecture

> Architecture diagram of the GitHub repository kumbhani11/algo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kumbhani11/algo
- Repository: https://github.com/kumbhani11/algo

## Overview

#### Purpose and Entry Points
This is a Python 3.10+ intraday options-trading system for Indian exchanges, using DhanHQ for authenticated REST/WebSocket market and trading access. The primary runtime starts from `main.py`; operational commands are routed through `algo_cli.py`. Supporting user-facing entry points include `tools/run_backtest.py`, `dashboard/server.py`, and `telegram/bot.py`.

#### Configuration, Instrument Resolution, and Dhan Boundary
`config.py` loads environment-driven credentials and trading settings, including the selected underlying. Symbol-specific security IDs, exchange segments, lot sizes, and market hours are resolved through `core/dynamic_resolver.py` and `core/exchange_mapper.py`, using downloaded local instrument datasets under `data/`. `dhan_client.py` is the application-facing DhanHQ client boundary; the vendored SDK lives in `DhanHQ-py/src/dhanhq/`, including REST aggregation in `dhanhq.py` and WebSocket feed implementations such as `marketfeed.py`.

#### Real-Time Trading Pipeline
The central low-latency path is coordinated by `realtime/trading_engine_realtime.py`:
1. `realtime/market_feed_manager.py` owns DhanHQ WebSocket connectivity and subscriptions.
2. Ticks enter `realtime/market_data_cache.py` for in-memory LTP access.
3. `realtime/candle_aggregator.py` derives candle data; `realtime/greeks_calculator.py` derives option metrics.
4. `realtime/signal_engine_realtime.py` evaluates trade opportunities.
5. Signals pass to execution/position handling, while `realtime/async_db_writer.py` persists without blocking the feed loop.

The intended tick-to-signal latency target is under 100 ms.

#### Strategy Layer
Strategies are isolated behind `strategies/base_strategy.py` and selected/orchestrated by `strategies/strategy_manager.py`. Current implementations include breakout logic in `strategies/breakout_strategy.py`; operational parameters are externalized in `strategies/configs/breakout.yaml` and `strategies/configs/straddle.yaml`. This boundary lets the signal pipeline consume a selected strategy rather than embed strategy rules in feed or execution code.

#### Execution, Positions, and Risk Controls
The trading domain layer separates decisions from side effects. `trading/signal_engine.py` produces or validates signals; `trading/risk_manager.py` enforces position and drawdown constraints; `trading/order_executor.py` performs paper or live placement through DhanHQ. `trading/position_manager.py` maintains open positions and P&L. Further safety/control boundaries include `trading/idempotency_guard.py`, `trading/drawdown_monitor.py`, `trading/execution_monitor.py`, and portfolio/option risk modules such as `trading/portfolio_greeks_manager.py`.

#### Persistence, Monitoring, and Operations
SQLite persistence is centralized in `db/persistence.py`, with tracked database artifacts including `db/positions.db`, `db/options_data.db`, and `db/trade_analytics.db`; RAM-disk placement is supported operationally via `DB_PATH`. Option-market monitoring is handled by `monitor/options_monitor_daemon.py`, backed by `monitor/option_data_fetcher.py` and `monitor/data_store.py`. Feed failures and request pressure are explicit operational concerns in `utils/feed_failure_handler.py` and `utils/rate_limiter.py`.

#### Offline Analysis and External Control Surfaces
Historical evaluation is separate from live execution: `backtesting/backtester.py` uses `backtesting/options_data_loader.py` and `backtesting/black_scholes_pricer.py`. Post-trade analysis is provided by `analytics/trade_analytics.py` and `analytics/performance_analytics.py`. Remote control and notifications flow through Telegram in `telegram/bot.py` and `telegram/trading_controller.py`; the optional local web UI is served by `dashboard/server.py`.

Main entry points: [`realtime/trading_engine_realtime.py`](https://github.com/kumbhani11/algo/blob/main/realtime/trading_engine_realtime.py) (Real-time trading engine), [`realtime/market_feed_manager.py`](https://github.com/kumbhani11/algo/blob/main/realtime/market_feed_manager.py) (Market feed manager), [`realtime/signal_engine_realtime.py`](https://github.com/kumbhani11/algo/blob/main/realtime/signal_engine_realtime.py) (Real-time signal engine), [`dhan_client.py`](https://github.com/kumbhani11/algo/blob/main/dhan_client.py) (Dhan client boundary)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; Control"]
  node_main["Main runtime<br/>Python entry point<br/>[main.py]"]
  node_cli["Operational CLI<br/>command router<br/>[algo_cli.py]"]
  node_telegram["Telegram control<br/>remote controller<br/>[bot.py]"]
  node_dashboard["Local dashboard<br/>web UI<br/>[server.py]"]
end

subgraph group_realtime["Real-Time Pipeline"]
  node_engine["Real-time trading engine<br/>pipeline coordinator"]
  node_feed["Market feed manager<br/>WebSocket subscriptions"]
  node_cache[("Market data cache<br/>in-memory LTP cache")]
  node_derivation["Candles &amp; Greeks<br/>market derivation"]
  node_rt_signal["Real-time signal engine<br/>opportunity evaluator"]
  node_async_writer["Async DB writer<br/>non-blocking persistence<br/>[async_db_writer.py]"]
end

subgraph group_trading["Trading Domain"]
  node_strategy_manager["Strategy manager<br/>strategy selection"]
  node_breakout["Breakout strategy<br/>strategy implementation"]
  node_risk["Risk controls<br/>risk and idempotency<br/>[risk_manager.py]"]
  node_executor["Order executor<br/>paper/live execution<br/>[order_executor.py]"]
  node_positions["Position manager<br/>positions and P&amp;L"]
end

subgraph group_data["Data &amp; Dhan"]
  node_config["Configuration<br/>environment settings<br/>[config.py]"]
  node_resolver["Instrument resolver<br/>symbol metadata"]
  node_dhan_client{{"Dhan client boundary<br/>broker adapter<br/>[dhan_client.py]"}}
  node_dhan_sdk{{"Vendored DhanHQ SDK<br/>REST and WebSocket SDK<br/>[dhanhq.py]"}}
end

subgraph group_offline["Offline &amp; Operations"]
  node_persistence[("SQLite persistence<br/>trade data store<br/>[persistence.py]")]
  node_options_monitor["Options monitor daemon<br/>market monitor"]
  node_backtester["Backtester<br/>historical evaluation<br/>[backtester.py]"]
  node_analytics["Trade analytics<br/>post-trade analysis<br/>[trade_analytics.py]"]
end

node_main -->|"starts"| node_engine
node_cli -->|"operates"| node_engine
node_telegram -->|"remote commands"| node_engine
node_dashboard -->|"views"| node_positions
node_config -->|"selected underlying"| node_resolver
node_config -->|"credentials"| node_dhan_client
node_resolver -->|"subscription instruments"| node_feed
node_engine -->|"coordinates"| node_feed
node_feed -->|"market WebSocket"| node_dhan_client
node_dhan_client -->|"uses"| node_dhan_sdk
node_feed -->|"ticks"| node_cache
node_cache -->|"LTP stream"| node_derivation
node_derivation -->|"candles and Greeks"| node_rt_signal
node_engine -->|"coordinates"| node_rt_signal
node_rt_signal -->|"evaluates with"| node_strategy_manager
node_strategy_manager -->|"selects"| node_breakout
node_rt_signal -->|"candidate signal"| node_risk
node_risk -->|"approved order"| node_executor
node_executor -->|"places live orders"| node_dhan_client
node_executor -->|"fills"| node_positions
node_positions -->|"state changes"| node_async_writer
node_async_writer -->|"writes asynchronously"| node_persistence
node_options_monitor -->|"stores option data"| node_persistence
node_backtester -->|"loads historical data"| node_persistence
node_persistence -->|"trade history"| node_analytics
node_analytics -->|"performance views"| node_dashboard

click node_main "https://github.com/kumbhani11/algo/blob/main/main.py"
click node_cli "https://github.com/kumbhani11/algo/blob/main/algo_cli.py"
click node_telegram "https://github.com/kumbhani11/algo/blob/main/telegram/bot.py"
click node_dashboard "https://github.com/kumbhani11/algo/blob/main/dashboard/server.py"
click node_config "https://github.com/kumbhani11/algo/blob/main/config.py"
click node_resolver "https://github.com/kumbhani11/algo/blob/main/core/dynamic_resolver.py"
click node_dhan_client "https://github.com/kumbhani11/algo/blob/main/dhan_client.py"
click node_dhan_sdk "https://github.com/kumbhani11/algo/blob/main/DhanHQ-py/src/dhanhq/dhanhq.py"
click node_engine "https://github.com/kumbhani11/algo/blob/main/realtime/trading_engine_realtime.py"
click node_feed "https://github.com/kumbhani11/algo/blob/main/realtime/market_feed_manager.py"
click node_cache "https://github.com/kumbhani11/algo/blob/main/realtime/market_data_cache.py"
click node_derivation "https://github.com/kumbhani11/algo/blob/main/realtime/candle_aggregator.py"
click node_rt_signal "https://github.com/kumbhani11/algo/blob/main/realtime/signal_engine_realtime.py"
click node_strategy_manager "https://github.com/kumbhani11/algo/blob/main/strategies/strategy_manager.py"
click node_breakout "https://github.com/kumbhani11/algo/blob/main/strategies/breakout_strategy.py"
click node_risk "https://github.com/kumbhani11/algo/blob/main/trading/risk_manager.py"
click node_executor "https://github.com/kumbhani11/algo/blob/main/trading/order_executor.py"
click node_positions "https://github.com/kumbhani11/algo/blob/main/trading/position_manager.py"
click node_persistence "https://github.com/kumbhani11/algo/blob/main/db/persistence.py"
click node_async_writer "https://github.com/kumbhani11/algo/blob/main/realtime/async_db_writer.py"
click node_options_monitor "https://github.com/kumbhani11/algo/blob/main/monitor/options_monitor_daemon.py"
click node_backtester "https://github.com/kumbhani11/algo/blob/main/backtesting/backtester.py"
click node_analytics "https://github.com/kumbhani11/algo/blob/main/analytics/trade_analytics.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_cli,node_telegram,node_dashboard toneBlue
class node_engine,node_feed,node_cache,node_derivation,node_rt_signal,node_async_writer toneAmber
class node_strategy_manager,node_breakout,node_risk,node_executor,node_positions toneMint
class node_config,node_resolver,node_dhan_client,node_dhan_sdk toneRose
class node_persistence,node_options_monitor,node_backtester,node_analytics toneIndigo
```

## Components

### Entry & Control

- [Main runtime](https://github.com/kumbhani11/algo/blob/main/main.py): Python entry point · `main.py`
- [Operational CLI](https://github.com/kumbhani11/algo/blob/main/algo_cli.py): command router · `algo_cli.py`
- [Telegram control](https://github.com/kumbhani11/algo/blob/main/telegram/bot.py): remote controller · `telegram/bot.py`
- [Local dashboard](https://github.com/kumbhani11/algo/blob/main/dashboard/server.py): web UI · `dashboard/server.py`

### Real-Time Pipeline

Low-latency tick-to-signal path.

- [Real-time trading engine](https://github.com/kumbhani11/algo/blob/main/realtime/trading_engine_realtime.py): pipeline coordinator · Coordinates the sub-100 ms tick-to-signal flow. · `realtime/trading_engine_realtime.py`
- [Market feed manager](https://github.com/kumbhani11/algo/blob/main/realtime/market_feed_manager.py): WebSocket subscriptions · `realtime/market_feed_manager.py`
- [Market data cache](https://github.com/kumbhani11/algo/blob/main/realtime/market_data_cache.py): in-memory LTP cache · `realtime/market_data_cache.py`
- [Candles & Greeks](https://github.com/kumbhani11/algo/blob/main/realtime/candle_aggregator.py): market derivation · `realtime/candle_aggregator.py`
- [Real-time signal engine](https://github.com/kumbhani11/algo/blob/main/realtime/signal_engine_realtime.py): opportunity evaluator · `realtime/signal_engine_realtime.py`
- [Async DB writer](https://github.com/kumbhani11/algo/blob/main/realtime/async_db_writer.py): non-blocking persistence · `realtime/async_db_writer.py`

### Trading Domain

- [Strategy manager](https://github.com/kumbhani11/algo/blob/main/strategies/strategy_manager.py): strategy selection · `strategies/strategy_manager.py`
- [Breakout strategy](https://github.com/kumbhani11/algo/blob/main/strategies/breakout_strategy.py): strategy implementation · Implements the base-strategy contract with externalized parameters. · `strategies/breakout_strategy.py`
- [Risk controls](https://github.com/kumbhani11/algo/blob/main/trading/risk_manager.py): risk and idempotency · Enforces position, drawdown, and duplicate-execution constraints. · `trading/risk_manager.py`
- [Order executor](https://github.com/kumbhani11/algo/blob/main/trading/order_executor.py): paper/live execution · `trading/order_executor.py`
- [Position manager](https://github.com/kumbhani11/algo/blob/main/trading/position_manager.py): positions and P&L · `trading/position_manager.py`

### Data & Dhan

- [Configuration](https://github.com/kumbhani11/algo/blob/main/config.py): environment settings · `config.py`
- [Instrument resolver](https://github.com/kumbhani11/algo/blob/main/core/dynamic_resolver.py): symbol metadata · Resolves security IDs, segments, lots, and market hours from local datasets. · `core/dynamic_resolver.py`
- [Dhan client boundary](https://github.com/kumbhani11/algo/blob/main/dhan_client.py): broker adapter · `dhan_client.py`
- [Vendored DhanHQ SDK](https://github.com/kumbhani11/algo/blob/main/DhanHQ-py/src/dhanhq/dhanhq.py): REST and WebSocket SDK · `DhanHQ-py/src/dhanhq/dhanhq.py`

### Offline & Operations

- [SQLite persistence](https://github.com/kumbhani11/algo/blob/main/db/persistence.py): trade data store · Central persistence for positions, options data, and trade analytics. · `db/persistence.py`
- [Options monitor daemon](https://github.com/kumbhani11/algo/blob/main/monitor/options_monitor_daemon.py): market monitor · `monitor/options_monitor_daemon.py`
- [Backtester](https://github.com/kumbhani11/algo/blob/main/backtesting/backtester.py): historical evaluation · `backtesting/backtester.py`
- [Trade analytics](https://github.com/kumbhani11/algo/blob/main/analytics/trade_analytics.py): post-trade analysis · `analytics/trade_analytics.py`

## Connections

- Main runtime → Real-time trading engine: starts
- Operational CLI → Real-time trading engine: operates
- Telegram control → Real-time trading engine: remote commands
- Local dashboard → Position manager: views
- Configuration → Instrument resolver: selected underlying
- Configuration → Dhan client boundary: credentials
- Instrument resolver → Market feed manager: subscription instruments
- Real-time trading engine → Market feed manager: coordinates
- Market feed manager → Dhan client boundary: market WebSocket
- Dhan client boundary → Vendored DhanHQ SDK: uses
- Market feed manager → Market data cache: ticks
- Market data cache → Candles & Greeks: LTP stream
- Candles & Greeks → Real-time signal engine: candles and Greeks
- Real-time trading engine → Real-time signal engine: coordinates
- Real-time signal engine → Strategy manager: evaluates with
- Strategy manager → Breakout strategy: selects
- Real-time signal engine → Risk controls: candidate signal
- Risk controls → Order executor: approved order
- Order executor → Dhan client boundary: places live orders
- Order executor → Position manager: fills
- Position manager → Async DB writer: state changes
- Async DB writer → SQLite persistence: writes asynchronously
- Options monitor daemon → SQLite persistence: stores option data
- Backtester → SQLite persistence: loads historical data
- SQLite persistence → Trade analytics: trade history
- Trade analytics → Local dashboard: performance views
