This repository contains a production-grade, low-latency algorithmic trading infrastructure optimized for the National Stock Exchange (NSE) of India. The system is architected as an event-driven distributed system, integrating institutional-grade risk controls, multi-asset quantitative strategies, and a high-fidelity research environment.
The infrastructure follows a decoupled, event-driven architecture designed for high throughput and fault tolerance.
- Event Orchestration: A central
EventBusfacilitates asynchronous communication between data feeds, strategy engines, and execution clients usingasynciofor non-blocking I/O. - Data Ingestion Layer: Multi-threaded WebSocket consumers for Zerodha (Kite) and Shoonya (Noren), featuring automated failover and tick-level quality validation.
- Persistence Layer:
- OLAP (ClickHouse): High-performance time-series storage for multi-year tick data, feature logs, and execution traces.
- State (Redis): Low-latency in-memory store for real-time positions, feature states, and risk utilization metrics.
- Message Broker (Kafka): Decouples high-volume raw ticks from downstream feature computation and monitoring services.
- Microstructure Features: Real-time calculation of Order Flow Imbalance (OFI), Volume-Synchronized Probability of Informed Trading (VPIN), and Microprice dynamics.
- Options Engine: High-performance Greeks calculation (Delta, Gamma, Vega, Theta) and IV surface calibration using Black-Scholes and Heston model abstractions.
- Regime Detection: Statistical regime identification using Gaussian Hidden Markov Models (HMM) to adapt strategy parameters to market volatility states.
The system implements a multi-layered safety framework to ensure capital preservation and regulatory compliance.
Every signal must pass through the PreTradeChecker which validates:
- Order Size Limits: Maximum INR value per clip.
- Liquidity Constraints: Order-to-ADV (Average Daily Volume) percentage limits.
- Buying Power: Real-time margin utilization checks against broker-reported capital.
- Portfolio Drawdown: Automated kill-switch activation if peak-to-trough drawdown exceeds defined thresholds.
- Exposure Management: Gross and net exposure limits tracked across all sub-strategies.
- Kill Switch: A global safety mechanism that cancels all pending orders and flattens active positions across all legs upon breach.
The execution layer abstracts broker-specific APIs into a unified interface, supporting sophisticated routing logic.
- Smart Executor: Implements order slicing (Time-Weighted or Volume-Weighted style) to minimize market impact.
- Passive-to-Active Upgrades: Automated limit order management that upgrades to market execution if fill-latency exceeds specified timeouts.
- Slippage Profiling: Detailed tracking of expected vs. realized fill prices to optimize execution parameters.
The infrastructure includes a high-fidelity backtesting engine designed to eliminate look-ahead bias and simulate realistic market conditions.
- Engine: Event-driven simulator that processes historical ClickHouse data as if it were a live feed.
- Fill Model: Incorporates market impact (using Square-root impact models) and variable slippage based on depth-at-best.
- Validation: Implementation of the Deflated Sharpe Ratio (DSR) to account for multiple testing bias and reduce the probability of false discoveries.
- Walk-Forward: Automated runner for out-of-sample validation and parameter stability testing.
- Linux/Windows (Docker-enabled)
- Python 3.11+
- Kite Connect / Shoonya API Credentials
- Minimum 8GB RAM for ClickHouse/Kafka stack
The system relies on three primary distributed services. These are fully containerized via Docker—you do not need to install Redis, Kafka, or ClickHouse directly on your host machine.
Run the bootstrap script to pull and initialize the stack:
./scripts/bootstrap.shThis command manages the lifecycle of:
- Redis: (Port 6379) - Used for real-time state and risk tracking.
- Kafka: (Port 9092) - High-throughput tick distribution.
- ClickHouse: (Port 8123/9000) - Analytical storage for historical research.
This section provides the exact commands required to operate the system.
To populate the analytical database with historical NSE data for backtesting:
# Downloads and loads Bhavcopy data into ClickHouse
python scripts/backfill.py --from 2024-01-01 --to 2024-12-31To execute a backtest for the Statistical Arbitrage strategy:
# Runs the event-driven simulator and generates a performance report
python scripts/run_backtest.py --months 6 --symbols RELIANCE,TCS,HDFCBANK,INFYOutput: reports/backtest.html
To launch the institutional-grade dashboard for PnL and Risk oversight:
# Requires Redis to be running via Step 5.2
streamlit run monitoring/dashboard/app.pytrading-system/
├── core/ # System kernel: EventBus, MarketClock, AppConfig
├── data/ # Ingestion: Feed clients, Quality Validators, Storage Drivers
├── execution/ # OMS: Broker abstractions, Order Managers, Smart Exec
├── features/ # Alpha: Pipeline, Microstructure, Greeks, Stats
├── infra/ # DevOps: Docker, SQL Init, Kafka Config
├── monitoring/ # Ops: Telegram Alerters, Streamlit Dashboard
├── research/ # Analytics: Backtester, Performance Metrics, DSR Validation
├── risk/ # Safety: Pre-Trade, Real-Time Monitoring, Kill Switch
├── strategies/ # Quant: StatArb, ML Directional, Options RV
└── scripts/ # Utils: Bootstrap, Data Backfill, Backtest Runners
Unit tests are mandatory for all core components. The suite covers data validation, mathematical greeks, and event-bus integrity.
pytest tests/Disclaimer: This software is for institutional research and trading purposes. Use at your own risk. The developers assume no liability for financial losses.