CoolFace
Apppublic

ndsideload/stk_predt

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

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

ModelTypeInputStrengths
XGBoostTree ensembleFlat featuresFeature importance, fast
LightGBMTree ensembleFlat featuresHandles large datasets, fast
LSTM+AttentionDeep learning5-day sequenceTemporal patterns
TransformerDeep learning5-day sequenceSelf-attention over time
ProphetDecompositionPrice seriesTrend + seasonality
Markov ChainProbabilisticState sequenceRegime detection (bull/bear/flat)
Monte CarloSimulationμ, σ statsConfidence intervals, tail risk
Swarm (PSO)Meta-optimizerModel outputsDynamic weight tuning

Daily Pipeline (7:30 PM IST)

  1. 1.Fetch latest data from published Google Sheet CSV.
  2. 2.Feedback — grade yesterday's predictions vs today's actuals.
  3. 3.Train all 7 prediction models on updated data.
  4. 4.Predict direction + return for each liquid stock.
  5. 5.PSO — Swarm Intelligence optimizes ensemble weights using feedback.
  6. 6.Log performance to Google Sheet Performance tab.
  7. 7.Telegram — send top BUY/SELL signals + accuracy report.
  8. 8.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 formatting

Setup

Hugging Face Spaces (Docker)

  1. 1.Create a new Space with "Docker" SDK.
  2. 2.Upload all files.
  3. 3.Set these Secrets in Space settings:
SecretDescription
SOURCE_CSV_URLPublished CSV URL of your Google Sheet
TELEGRAM_TOKENYour Telegram bot token
TELEGRAM_CHAT_IDYour Telegram chat ID
PERF_SHEET_IDGoogle Sheet ID for performance logging
GSHEET_CREDENTIALS_JSONService account JSON (for writing to perf sheet)
  1. 1.The Space will auto-build and start scheduling.

Local Development

bash
pip install -r requirements.txt
python app.py

Key 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.