ndsideload/stk_predt
0
NIFTY Stock Prediction System
An automated 8-model ensemble that predicts next-day stock direction for the Indian liquid universe (~600 stocks). Deployed on Hugging Face Spaces with daily self-optimization.
Architecture
┌─────────────────────────────────────────────────────────────┐
│ Google Sheet (GAS Pipeline) │
│ Daily closes → 19 columns × 1750 stocks │
└──────────────────────┬──────────────────────────────────────┘
│ CSV fetch
▼
┌─────────────────────────────────────────────────────────────┐
│ Data Manager + Feature Engine │
│ Clean → filter liquid (MCap>5KCr) → engineer 30+ features │
└──────────────────────┬──────────────────────────────────────┘
│
┌────────────┼────────────────────┐
▼ ▼ ▼
┌─────────────┐ ┌──────────┐ ┌─────────────────┐
│ Batch Models │ │ Sequence │ │ Per-Stock Models │
│ XGBoost │ │ LSTM │ │ Prophet │
│ LightGBM │ │ Transf. │ │ Markov │
└──────┬──────┘ └────┬─────┘ │ Monte Carlo │
│ │ └────────┬──────────┘
└──────────────┼─────────────────┘
▼
┌──────────────────────┐
│ Dynamic Ensemble │
│ PSO-optimized wts │
│ Swarm Intelligence │
└──────────┬───────────┘
│
┌────────────┼────────────┐
▼ ▼ ▼
┌────────────┐ ┌─────────┐ ┌──────────┐
│ Predictions │ │Feedback │ │Telegram │
│ saved/day │ │Loop │ │Report │
└─────────────┘ └─────────┘ └──────────┘
│
▼
┌──────────────────────┐
│ Performance Sheet │
│ + Optuna Retrain │
└──────────────────────┘Models
Daily Pipeline (7:30 PM IST)
- Fetch latest data from published Google Sheet CSV.
- Feedback — grade yesterday's predictions vs today's actuals.
- Train all 7 prediction models on updated data.
- Predict direction + return for each liquid stock.
- PSO — Swarm Intelligence optimizes ensemble weights using feedback.
- Log performance to Google Sheet Performance tab.
- Telegram — send top BUY/SELL signals + accuracy report.
- Save all model state to disk.
Retrain Pipeline (8:00 PM IST)
- Optuna tunes XGBoost and LightGBM hyperparameters (30 trials each).
- LSTM and Transformer retrain on latest sequences.
- Per-stock models re-fit.
File Structure
stock_predictor/
├── app.py # HF Spaces entrypoint (Gradio + APScheduler)
├── config.py # All settings and env vars
├── Dockerfile # Docker build for HF Spaces
├── requirements.txt
├── core/
│ ├── data_manager.py # Fetch, clean, filter, panel builder
│ ├── feature_engine.py # 30+ engineered features
│ ├── backtester.py # Walk-forward validation
│ ├── feedback.py # Recursive learning from mistakes
│ ├── optimizer.py # Optuna hyperparameter tuning
│ └── performance_tracker.py # Logs to Google Sheet / local CSV
├── models/
│ ├── base.py # Abstract model interface
│ ├── xgb_model.py # XGBoost
│ ├── lgbm_model.py # LightGBM
│ ├── lstm_model.py # BiLSTM + Multi-Head Attention
│ ├── transformer_model.py # Transformer encoder
│ ├── prophet_model.py # Meta Prophet
│ ├── markov_model.py # Markov Chain regime detector
│ ├── monte_carlo.py # Monte Carlo simulation (GBM)
│ ├── swarm_model.py # PSO ensemble weight optimizer
│ └── ensemble.py # Dynamic ensemble orchestrator
├── scheduler/
│ └── daily_runner.py # Pipeline orchestration
└── utils/
└── telegram.py # Notification formattingSetup
Hugging Face Spaces (Docker)
- Create a new Space with "Docker" SDK.
- Upload all files.
- Set these Secrets in Space settings:
- The Space will auto-build and start scheduling.
Local Development
pip install -r requirements.txt
python app.pyKey Design Decisions
- PSO over static weights: The swarm continuously adapts which models get more influence based on recent accuracy — no manual weight tuning.
- Two-tier model strategy: Fast tree models (XGB, LGBM) run on all 600 stocks. Expensive models (LSTM, Transformer, Prophet) run on top 150 by market cap.
- Feedback before prediction: Yesterday's mistakes inform today's weights before any new predictions are generated.
- Predictions saved to disk: Every day's predictions are JSON-persisted so the feedback loop always has ground truth to compare against.
