Published 2026-07-21|12 min read

QuantFlow: Building an Institutional-Grade Algo Trading System for Indian Markets

Open Source
PythonAlgorithmic TradingNSEQuantitative FinanceMachine Learning

The Indian equity markets execute over 80,000 crores in daily turnover across the NSE and BSE. Sandwiched between institutional algorithms executing in microseconds and retail traders chasing momentum on mobile apps sits a fascinating engineering problem: can a systematically designed quant system, running on consumer hardware with publicly available data, consistently extract alpha from the most liquid stocks in the emerging world's largest derivatives market?

I spent three months finding out. The result is QuantFlow — an institutional-grade algorithmic trading framework purpose-built for the National Stock Exchange of India.

Source code: github.com/The-Peacemaker/Algo-Trade — Python, MIT license. Contributions, forks, and paper-trading audits welcome.


Architecture: The Pipeline

QuantFlow is not a single strategy. It is a modular signal processing pipeline through which market data flows, transforms through successive layers of technical and statistical analysis, and emerges as actionable trade decisions with calibrated risk parameters.

text
Market Data (yfinance/Groww/Angel One)

Technical Indicator Matrix (15+ indicators)

Multi-Timeframe Confluence Engine (1m, 5m, 15m, 1h)

Candlestick Pattern Recognition (12 patterns)

Signal Generation (Bullish/Neutral/Bearish + Confidence Score)

Kelly Criterion Position Sizing

Portfolio Risk Validation (Drawdown, Correlation, Exposure)

Order Execution (Broker API / Paper Trade)

Analytics & Logging

Every layer is independently testable, swappable, and parameterizable. The architecture is a directed acyclic graph of transformations — not a monolithic script.


The Technical Indicator Matrix

QuantFlow computes fifteen distinct technical indicators across four categories, each feeding into a weighted confluence scoring system.

Trend Layer

SMAn=1ni=0n1PtiSMA_{n} = \frac{1}{n} \sum_{i=0}^{n-1} P_{t-i}

EMAn(t)=αPt+(1α)EMAn(t1),α=2n+1EMA_{n}(t) = \alpha \cdot P_t + (1-\alpha) \cdot EMA_{n}(t-1), \quad \alpha = \frac{2}{n+1}

VWAP=(PtVt)VtVWAP = \frac{\sum (P_t \cdot V_t)}{\sum V_t}

The 21-period EMA serves as the primary trend filter. Price above EMA(21) establishes a bullish regime; below, bearish. VWAP provides institutional-grade fair-value reference — a stock trading significantly below VWAP with rising volume suggests accumulation.

Momentum Layer

RSI=1001001+Avg GainAvg LossRSI = 100 - \frac{100}{1 + \frac{\text{Avg Gain}}{\text{Avg Loss}}}

MACD=EMA12EMA26,Signal=EMA9(MACD)\text{MACD} = EMA_{12} - EMA_{26}, \quad \text{Signal} = EMA_{9}(\text{MACD})

RSI(14) is the workhorse of the momentum layer. Values below 30 with bullish divergence trigger buy signals; above 70 with bearish divergence trigger sells. The MACD histogram — the difference between MACD and its signal line — provides early momentum inflection detection.

Volatility Layer

ATR=1ni=0n1TRti,TR=max(HL,HCprev,LCprev)\text{ATR} = \frac{1}{n} \sum_{i=0}^{n-1} \text{TR}{t-i}, \quad \text{TR} = \max(H-L, |H-C{prev}|, |L-C_{prev}|)

Bollinger Upper=SMA20+2σ,Bollinger Lower=SMA202σ\text{Bollinger Upper} = SMA_{20} + 2\sigma, \quad \text{Bollinger Lower} = SMA_{20} - 2\sigma

ATR(14) is the universal risk calibrator. Position sizes are normalized by ATR — a stock with twice the volatility receives half the allocation. Bollinger Bands provide mean-reversion signals when price touches the outer bands with RSI confirmation.

Volume Layer

Volume Ratio=Vt120i=120Vti\text{Volume Ratio} = \frac{V_t}{\frac{1}{20}\sum_{i=1}^{20} V_{t-i}}

A Volume Ratio above 1.5 on a breakout confirms institutional participation. On-balance volume (OBV) cumulative trends provide divergence signals — price making new highs while OBV lags suggests weakening momentum.


The Candlestick Pattern Engine

Beyond continuous indicators, QuantFlow implements a discrete candlestick pattern recognition engine detecting twelve patterns:

Pattern Bullish/Bearish Logic
Hammer Bullish Small body, long lower wick, downtrend
Morning Star Bullish Three-candle reversal: bearish → doji → bullish
Bullish Engulfing Bullish Green body fully engulfs previous red
Three White Soldiers Bullish Three consecutive long green candles
Evening Star Bearish Three-candle top reversal
Bearish Engulfing Bearish Red body fully engulfs previous green
Doji Neutral Open ≈ Close, indecision

Patterns are scored by reliability weight and fed into the confluence matrix alongside indicator signals. A Hammer at support with RSI oversold and bullish MACD divergence is a high-conviction signal across three independent analytical modalities.


Multi-Timeframe Confluence

Single-timeframe analysis is noise. QuantFlow implements a hierarchical confirmation engine that evaluates signal alignment across four timeframes:

Timeframe Role
1-minute Entry precision, scalp detection
5-minute Short-term trend, pattern formation
15-minute Primary trading timeframe
1-hour Regime filter (trend direction override)

The 1-hour trend acts as a regime override: if the hourly trend is bearish, no long signals from lower timeframes execute regardless of their confidence score. This prevents catching falling knives in the name of mean reversion.


Risk Management: The Kelly Criterion

Position sizing is the only free lunch in trading. QuantFlow implements fractional Kelly Criterion:

f=W(R+1)1Rf^* = \frac{W \cdot (R+1) - 1}{R}

Where WW is the historical win rate and RR is the win/loss ratio (average winner / average loser). A fractional Kelly multiplier of 0.50.5 provides a balance between growth and drawdown protection.

Risk Profiles by Capital

Budget Profile Max Risk/Trade Max Positions
< ₹500 UltraSafe 1% 1
₹500–₹5,000 Conservative 1.5% 2
₹5,000–₹50,000 Moderate 2% 3
> ₹50,000 Aggressive 2% 5

Hard Limits

MAX_DAILY_LOSS = 0.06        # 6% of capital
MAX_CONSECUTIVE_LOSSES = 3   # auto-disable trading
MAX_TOTAL_EXPOSURE = 0.20    # 20% of capital
STOP_LOSS = 1.5 * ATR        # dynamic, volatility-adjusted
TAKE_PROFIT = 2.5 * ATR      # minimum 2R reward

The affordability check for small accounts is particularly important in the Indian context. A ₹200 account cannot buy a ₹1,200 stock — the system checks price against budget and returns a clear error rather than attempting a fractional order:

max_affordable = budget * leverage
if stock_price > max_affordable:
    return {"quantity": 0, "error": "Stock exceeds max affordable"}

Backtest Performance

Backtesting was conducted on real NSE data over a 15-day window using institutional-grade metrics.

Per-Symbol Results

Symbol Trades Win Rate P&L
RELIANCE 28 64% +₹91.84
TCS 12 50% +₹10.78
KOTAKBANK 28 43% +₹22.36
INFY 12 50% +₹2.60
HDFCBANK 21 14% –₹28.14
SBIN 6 0% –₹24.26

The RELIANCE result — 28 trades, 64% win rate — demonstrates what a trend-aligned system can achieve in India's most liquid stock. The SBIN result — 6 trades, 0% win rate — demonstrates what happens when the regime filter is misaligned (SBIN was in a downtrend during the test window).

Aggregated Metrics

Metric Value
Sharpe Ratio > 3.5 (trend-aligned)
Max Drawdown < 6% daily
Avg Win Rate 40–50%
Best Performer RELIANCE (64%)
Benchmark Nifty 50

The ₹200 Test

The most revealing test: a ₹200 account executing 10 trades with a 50% win rate, final capital ₹200.25. The absolute return is minuscule — 25 paise — but the system did not blow up. The risk management layer scaled positions so aggressively for the tiny capital that fees alone consumed any edge. This is an honest result: algorithmic trading below ₹5,000 is economically marginal in Indian markets due to fixed transaction costs.

bash
python3 institutional_trading.py

Broker Integration

QuantFlow ships with two broker connectors:

Angel One SmartAPI — Production-ready WebSocket integration for real-time execution. The connector handles JWT authentication, order placement, position tracking, and WebSocket reconnection with exponential backoff.

Groww Connector — REST-based data feed for NSE equity quotes. Limited to data fetching (Groww does not expose a programmatic order placement API for third parties).

Both connectors implement a unified interface, allowing the strategy engine to operate broker-agnostic:

class BrokerInterface:
    def place_order(self, symbol, qty, side, order_type): ...
    def get_positions(self): ...
    def get_balances(self): ...
    def stream_ticks(self, symbols): ...

The System Test Suite

Before any trade executes, QuantFlow validates its own integrity through a comprehensive test suite:

text
✓ Position Sizing Engine
✓ Portfolio Risk Manager
✓ Multi-Timeframe Analysis
✓ Advanced Orders
✓ Strategy Engine
✓ Backtest Engine
✓ Groww Connector
✓ Data Fetcher
✓ Trading System
✓ Stock Configuration
✓ Alert System

RESULTS: 11 passed, 0 failed

Every module has a corresponding test. The test suite is the entry point for any new contributor — and the gatekeeper for any change to the risk management layer.


What I Learned

Building QuantFlow taught me three things about algorithmic trading that no book prepares you for:

1. Transaction costs dominate strategy selection below ₹50,000. The difference between a 2% edge and a 3% edge is irrelevant when brokerage + STT + Sebi charges consume 1.5% per round trip. The real optimization is not signal generation but cost minimization — fewer trades, wider stops, longer holding periods.

2. Indicator overfitting is the default state. Combining fifteen indicators inevitably produces a curve-fit to the training window. QuantFlow addresses this through the regime filter: the 1-hour trend override prevents the system from trading against the dominant market direction, which is the primary source of overfit-induced losses.

3. The Kelly criterion is unforgiving with small sample sizes. With fewer than 30 trades, the win rate estimate has a standard error of approximately 9%. A 50% measured win rate could be 41% or 59% in reality. Fractional Kelly (k=0.5k = 0.5) is not optional — it is existential.

The complete source is on GitHub for anyone to audit, fork, or destroy in paper trading: github.com/The-Peacemaker/Algo-Trade


References

[1] Kelly, J. L. (1956). A new interpretation of information rate. Bell System Technical Journal, 35(4), 917–926.

[2] Thorp, E. O. (1962). Beat the Dealer: A Winning Strategy for the Game of Twenty-One. Random House.

[3] Murphy, J. J. (1999). Technical Analysis of the Financial Markets. New York Institute of Finance.

[4] Taleb, N. N. (2007). The Black Swan: The Impact of the Highly Improbable. Random House.