I have developed a swing trading Algo.

Please provide me your opinions

""""

# Institutional Quantitative Screener V5: Comprehensive Architecture


This document provides a highly detailed mathematical and technical specification of the 
**V5 Automated Trading & Screening System**
. The V5 architecture is designed to capture market anomalies in the NIFTY 500 universe using advanced quantitative methods.


---


## 1. End-to-End System Diagram


```mermaid
flowchart TD
    subgraph Data Layer
        A[5paisa API Connection] -->|Hourly Poll / Fetch| B[(Local Raw OHLCV CSVs)]
        B -->|Check Last Date| C[Incremental Updater]
    end


    subgraph Mathematical Transforms
        B -->|Fractional Differencing| D[FFD Price Series]
        B -->|Volatility Engine| E[20-Day Rolling ATR]
        E -->|Triple Barrier Method| F[TP/SL/Time-Stop Barriers]
    end


    subgraph Feature Store
        D & B -->|Feature Engineering| G[Technical Indicators]
        G -->|Cross-Sectional Grouping| H[Market-Neutral Z-Scores]
        B -->|Macro Data Merge| I[NIFTY & India VIX Features]
        H & I --> J[Feature Selection: SHAP Importance]
    end


    subgraph Regime Modeling
        I -->|Gaussian Mixture Model| K[3-State Market Regime Classifier]
        K -->|State: Bull / Bear / Sideways| L[Model Context Gate]
    end


    subgraph Machine Learning Pipeline
        J & L -->|Train Model 1| M[XGBoost Classifier]
        M -->|Isotonic Regression| N[Calibrated Classifier]
        J & L -->|Train Model 2| O[XGBoost Ranker]
        J & L -->|Train Model 3| P[XGBoost Regressor]
    end


    subgraph Portfolio construction
        N -->|Calibrated Probabilities| Q[Composite Scoring & Gating]
        O -->|Rank Scores| Q
        P -->|Expected Return| Q
        Q -->|Correlation Matrix Clustering| R[Hierarchical Risk Parity]
        R -->|Bisection Weights| S[Half-Kelly Sizing Engine]
    end


    subgraph Execution & Diagnostics
        S --> T[SHAP Driver Extraction]
        T --> U[Final Trade Blueprint Dashboard]
    end
```


---


## 2. Mathematical Specifications


### A. Fractional Differencing (FFD)
Traditional integer differencing ($d=1$) removes non-stationarity but completely destroys the price series' "memory" (long-term cycles, support/resistance levels). FFD preserves memory while satisfying stationarity requirements.


We expand the difference operator $(1 - B)^d$ using a binomial series:


$$(1 - B)^d = \sum_{k=0}^{\infty} (-1)^k \binom{d}{k} B^k = 1 - dB + \frac{d(d-1)}{2!} B^2 - \frac{d(d-1)(d-2)}{3!} B^3 + \dots$$


Where:
*   $B$ is the backshift operator ($B^k X_t = X_{t-k}$).
*   The weights $w_k$ for lag $k$ are computed recursively:
    $$w_0 = 1$$
    $$w_k = -w_{k-1} \frac{d - k + 1}{k}, \quad k \ge 1$$


To implement this on finite historical data, we apply a weight threshold $\tau = 10^{-4}$. Lags with $|w_k| < \tau$ are dropped. The fractionally differenced series $X'_t$ is:


$$X'_t = \sum_{k=0}^{K} w_k X_{t-k}$$


In V5, we use 
**$d = 0.35$**
, which successfully stationarizes close price series while maintaining a correlation $>0.8$ with the original price, preserving crucial structural patterns for XGBoost.


---


### B. Dynamic Triple Barrier Labeling
Instead of using fixed profit targets (e.g., $+1\% / -1\%$) which fail when market volatility shifts, V5 uses Marcos López de Prado's 
**Triple Barrier Method**
 with volatility-adaptive barriers.


For each bar $t$, the barriers are constructed relative to the entry price $P_t$:


1.  
**Upper Barrier (Take-Profit)**
:
    $$\text{TP}_t = P_t \times (1 + \text{Multiplier}_{\text{TP}} \times \text{ATR}_{\%}) + \text{Cost}$$
2.  
**Lower Barrier (Stop-Loss)**
:
    $$\text{SL}_t = P_t \times (1 - \text{Multiplier}_{\text{SL}} \times \text{ATR}_{\%}) - \text{Cost}$$
3.  
**Vertical Barrier (Time-Stop)**
:
    $$\text{Vertical}_t = t + \text{Max\_Holding\_Period}$$


#### Parameters:
*   `Multiplier_TP` = 
**$2.5$**
 (Take-profit is 2.5x ATR).
*   `Multiplier_SL` = 
**$1.0$**
 (Stop-loss is 1x ATR).
*   `Max_Holding_Period` = 
**$30$ hourly bars**
 (exactly 5 trading days).
*   `Transaction_Cost_Pct` = 
**$0.0025$**
 (Subtracts 0.25% round-trip friction to ensure predicted returns are net of brokerage, taxes, and slippage).
*   `Vol_Floor` = $0.5\%$ (Prevents tiny targets in low-volatility sideways markets).
*   `Vol_Ceiling` = $8.0\%$ (Prevents unreachable targets during wild spikes).


#### Labeling Logic ($Y_t$):
$$Y_t = \begin{cases} 
1 & \text{if price hits } \text{TP}_t \text{ before } \text{SL}_t \text{ or } \text{Vertical}_t \\ 
0 & \text{if price hits } \text{SL}_t \text{ or } \text{Vertical}_t \text{ first} 
\end{cases}$$


---


### C. Gaussian Mixture Model (GMM) Market Regime
To prevent models from buying during bearish market panics, we model the market state using an unsupervised 
**Gaussian Mixture Model (GMM)**
 with $K=3$ clusters.


Let $x_t$ be the vector of market features at time $t$ (containing 20-day NIFTY 500 returns, India VIX close, and rolling 20-day NIFTY realized volatility). The probability density of $x_t$ is modeled as a mixture of Gaussians:


$$p(x_t) = \sum_{k=1}^{3} \pi_k \mathcal{N}(x_t | \mu_k, \Sigma_k)$$


Where:
*   $\pi_k$ is the mixing weight for cluster $k$ ($\sum \pi_k = 1$).
*   $\mathcal{N}(x_t | \mu_k, \Sigma_k)$ is the multivariate normal distribution with mean vector $\mu_k$ and covariance matrix $\Sigma_k$.
*   Parameters are trained using the 
**Expectation-Maximization (EM)**
 algorithm.


The current market state is mapped to three regimes:
1.  
**Regime 0 (Risk-Off / Bearish / High Volatility)**
: Characterized by negative NIFTY returns, high VIX, and elevated realized volatility.
2.  
**Regime 1 (Neutral / Sideways)**
: Moderate returns, normal volatility.
3.  
**Regime 2 (Risk-On / Bullish / Low Volatility)**
: Positive NIFTY returns, low VIX, and compressed realized volatility.


---


### D. Feature Store & Cross-Sectional Normalization
We engineer over 49 technical, cyclical, and volume features. 


#### 1. Technical Indicators:
*   
**Bollinger Bands (%B)**
: Identifies overextended setups relative to volatility bands:
    $$\text{BB \%B} = \frac{\text{Close} - \text{Lower BB}}{\text{Upper BB} - \text{Lower BB}}$$
*   
**Amihud Illiquidity Ratio**
: Captures how much price moves per unit of traded volume:
    $$\text{Amihud} = \frac{|\text{Return}_t|}{\text{Volume}_t \times \text{Close}_t}$$
*   
**Volume Price Trend (VPT)**
: Tracks the flow of money based on volume and cumulative percentage returns:
    $$\text{VPT}_t = \text{VPT}_{t-1} + \text{Volume}_t \times \left( \frac{\text{Close}_t - \text{Close}_{t-1}}{\text{Close}_{t-1}} \right)$$


#### 2. Cross-Sectional Normalization (Z-Scores):
To remove sector and broader market bias, key features (like RSI, Distance to VWAP, 1-hour returns, VPT, and Amihud) are cross-sectionally normalized hourly across the entire NIFTY 500 universe:


$$Z_{i, t} = \frac{x_{i, t} - \mu_{\text{Universe}, t}}{\sigma_{\text{Universe}, t}}$$


Where:
*   $x_{i, t}$ is the raw indicator value of stock $i$ at hour $t$.
*   $\mu_{\text{Universe}, t}$ is the mean of that indicator across all NIFTY 500 stocks at hour $t$.
*   $\sigma_{\text{Universe}, t}$ is the standard deviation of that indicator across the universe at hour $t$.


This makes the features market-neutral and robust to broad market swings.


---


## 3. Modeling & Optimization Pipeline


### A. The 3-Model Stack
V5 uses a three-pronged machine learning strategy to isolate and rank high-probability trades:


1.  
**XGBoost Classifier (Calibrated Probability)**
:
    *   Trains on binary labels ($Y_t$).
    *   Calibrated out-of-sample using 
**Isotonic Regression**
 (via `CalibratedClassifierCV`) to map raw classifier scores into true mathematical probabilities:
        $$P(\text{Win}) = f(S_{\text{raw}})$$
        Where $f(x)$ is a piecewise constant non-decreasing function fitted to minimize calibration error.
2.  
**XGBoost Ranker (Learning to Rank)**
:
    *   Trained using the pairwise ranking objective (`objective='rank:pairwise'`).
    *   Stocks are grouped by date. The model learns to rank which stock will outperform other stocks on the same day, optimizing relative return ordering.
3.  
**XGBoost Regressor (Expected Return)**
:
    *   Trained on the numerical return achieved at barrier touch (`barrier_return`).
    *   Used to predict the raw percentage magnitude of the expected return.


### B. Composite Scoring Gating
For the scanner, the three model outputs are blended into a unified 
**Composite Score**
:


$$\text{Composite Score} = 0.40 \times \text{Rank\_Score}_{\text{norm}} + 0.35 \times \text{Win\_Probability}_{\text{norm}} + 0.25 \times \text{Expected\_Return}_{\text{norm}}$$


Only setups with a 
**Calibrated Win Probability $\ge 50\%$**
 are allowed to pass the gate.


---


### C. Hierarchical Risk Parity (HRP) Optimization
Traditional Markowitz portfolio optimization (mean-variance) is highly unstable, often resulting in concentrated portfolios that are sensitive to small errors in expected return estimates. HRP solves this by applying hierarchical clustering to the covariance matrix.


#### 1. Tree Clustering (Single Linkage):
We define the distance between asset $i$ and $j$ based on their 120-day return correlation $\rho_{i, j}$:


$$d_{i, j} = \sqrt{\frac{1 - \rho_{i, j}}{2}}$$


HRP groups correlated stocks using single-linkage clustering, building a hierarchical binary tree (dendrogram).


#### 2. Quasi-Diagonalization:
Re-orders the covariance matrix so that similar assets are grouped adjacent to each other.


#### 3. Recursive Bisection:
The ordered assets are recursively bisected. At each split, the variance of the left cluster $V_L$ and right cluster $V_R$ is computed, and weights are distributed inversely to their cluster variances:


$$\alpha = 1 - \frac{V_L}{V_L + V_R}$$


$$W_L = W \times \alpha$$
$$W_R = W \times (1 - \alpha)$$


This process repeats until individual asset weights are determined. This ensures risk is distributed evenly across uncorrelated clusters rather than concentrating in a single sector.


---


### D. Half-Kelly Position Sizing
To maximize compounding while preventing capital drawdowns, HRP weights are scaled down using the 
**Half-Kelly**
 criterion.


Let $W$ be the calibrated win probability, and $R$ be the payoff ratio (Take Profit / Stop Loss = 2.5). The Kelly fraction $f^*$ is:


$$f^* = W - \frac{1 - W}{R} = W - \frac{1 - W}{2.5}$$


To be conservative, we use the 
**Half-Kelly**
 size:


$$\text{Half-Kelly} = \max\left(0, \frac{f^*}{2}\right)$$


The final capital allocation for stock $i$ (out of capital $C = ₹50,000$) is scaled:


$$\text{Allocation}_i = C \times \text{Weight}_{\text{HRP}, i} \times \text{Half-Kelly}_i \times 0.15$$


*(The 0.15 scaling multiplier acts as a conservative risk buffer for the portfolio).*


---


## 4. SHAP Explainability Engine


To prevent the system from being a "black box", the scanner runs 
**SHAP (SHapley Additive exPlanations)**
 on the base XGBoost model. 


For a given prediction, the Shapley value $\phi_j$ for feature $j$ represents its contribution to the model's output deviation from the base value $E[f(X)]$:


$$\phi_j = \sum_{S \subseteq F \setminus \{j\}} \frac{|S|!(|F| - |S| - 1)!}{|F|!} \Big[ f(S \cup \{j\}) - f(S) \Big]$$


This translates complex machine learning predictions into human-readable trade drivers printed directly on the scanner output, such as:
*   `✅ positive deviation`: `RSI_14_Cross` is oversold relative to Nifty 500.
*   `✅ positive deviation`: `Dist_VWAP` is showing strong support.

Posted Jul 6, 2026, 1:24 PM UTC

11
Loading comments…