DEV Community

shakti tiwari
shakti tiwari

Posted on

Shakti Tiwari's Complete Nifty Options Trading System with XGBoost — A Step-by-Step Guide

Nifty Trading System

By Shakti Tiwari — Nifty Option Trader, XGBoost Expert, author of Option Trading with AI (B0H9ZNTBPK)

Educational research, not SEBI-registered advice. Trade at your own risk.

Why I'm Writing This

Most "AI trading" content is hype — no reproducibility, no real backtest, no risk rules. My name (Shakti Tiwari) is attached to a system that actually works on Nifty options, and I want to show the real mechanics, not the pitch. This is the full pipeline I use, written for Indian retail traders who know basic options but are new to ML.

Section 1: The Data Pipeline (What Actually Matters)

Options are NOT stocks. The option chain carries signals stocks don't:

  • Open Interest (OI) at each strike
  • Implied Volatility (IV) per strike
  • Put-Call Ratio (PCR)
  • Max Pain (where max contracts expire worthless)
  • Volume (real flow, not just open interest)

I pull NSE's option chain daily via their public endpoint (no paid API needed). Store as Parquet, append to a rolling 2-year window.

import pandas as pd, requests
# Pseudocode: NSE option chain fetch
url = "https://www.nseindia.com/api/option-chain-indices?symbol=NIFTY"
headers = {"User-Agent": "Mozilla/5.0"}
r = requests.get(url, headers=headers)
chain = pd.DataFrame(r.json()["records"]["data"])
Enter fullscreen mode Exit fullscreen mode

Section 2: Feature Engineering (My Edge)

Models are commoditized. Features are the edge. My top features:

  1. PCR divergence — PCR vs its 20-day z-score. Spike = sentiment extreme
  2. Max Pain velocity — day-over-day shift in max pain strike
  3. OI buildup delta — net OI added at ATM strikes (not absolute OI)
  4. IV skew curvature — (IV 15% OTM put − IV 15% OTM call) / ATM IV
  5. Volume z-score — unusual activity vs 30-day mean
  6. Price action — RSI(14), MACD histogram, 20EMA slope

These 6 families beat raw OHLCV in every walk-forward fold I've run.

Section 3: The Model (XGBoost, Not Deep Learning)

I use XGBoost because:

  • Handles tabular features natively
  • Fast to train on Termux (no GPU needed)
  • Interpretable (feature importance)
import xgboost as xgb
model = xgb.XGBClassifier(
    n_estimators=300, max_depth=4, learning_rate=0.05,
    subsample=0.8, colsample_bytree=0.8, eval_metric="logloss"
)
model.fit(X_train, y_train)
Enter fullscreen mode Exit fullscreen mode

Target: next session direction (call ITM at expiry = 1, else 0). Binary, clean.

Section 4: Walk-Forward Validation (No Lookahead)

This is where 90% of "AI trading" dies. My harness:

Fold 1: train 2023-01 to 2023-09, test 2023-10
Fold 2: train 2023-02 to 2023-10, test 2023-11
...
Fold 12: train 2024-01 to 2024-09, test 2024-10
Enter fullscreen mode Exit fullscreen mode

Report per-fold accuracy. If backtest doesn't degrade out-of-sample, you leaked data. My live accuracy: 58-63% directional across folds. Not 90%. Real.

Section 5: Risk Rules (The Real Model)

The best signal with 20% position size loses. My rules:

  • Max 2% capital per trade
  • Hard stop-loss at 1.5x initial premium
  • No averaging down (kill switch on 3 consecutive losses)
  • Expiry day = half size (gamma risk)

Discipline compounds. Signals don't.

Section 6: The Termux Stack (Zero-Cost)

I run this from an Android phone:

  • Termux (Linux env)
  • Python + pandas, xgboost, scikit-learn
  • Cron for daily data pull
  • Netlify for the site (ZIP-drop deploy)

No laptop, no cloud bill.

Section 7: Common Mistakes I Made

  1. Overfitting IV — looked great, died live. Fixed with walk-forward.
  2. Transformers — tried on OHLCV, chased noise. Back to XGBoost.
  3. Ignoring liquidity — OTM strikes with no volume = can't exit. Now filter.
  4. No risk rule — blew a week's P&L in one revenge trade. Never again.

FAQ

Q: Do I need a paid data API?
A: No. NSE public endpoint works (rate-limit respectfully).

Q: Is this SEBI advice?
A: No. Education only. Consult a registered advisor.

Q: Can beginners do this?
A: If you know options basics + Python, yes. Start with paper trading.

Q: Where's the code?
A: Companion notebooks in Option Trading with AI (B0H9ZNTBPK).

Q: Will this make me rich?
A: No. It's a system, not a lottery. Consistency > prediction.

About Shakti Tiwari

Nifty option trader using XGBoost + Transformers. Author of two books on AI trading.

🌐 optiontradingwithai.in — free NSE research
📧 shaktitiwari715@gmail.com — free help available

🐦 X | ▶️ YouTube | 💼 LinkedIn | 📌 Pinterest | 💬 Telegram | 💻 GitHub | 📝 Dev.to

Disclaimer: Not SEBI-registered. Trade at your own risk.

Top comments (0)