---
license: apache-2.0
language:
- en
metrics:
- accuracy
- mae
- mse
base_model:
- Qwen/Qwen3-4B-Thinking-2507
library_name: transformers
tags:
- finan
- market-prediction
- stock-market
- nifty50
- technical-analysis
- qwen
- fine-tuned
---
# Qwen Market Prediction Model

## Model Details

- **Developed by:** Afzalur Rahman
- **Funded by:** Self
- **Shared by:** Afzalur Rahman
- **Model type:** Causal Language Model (fine-tuned from Qwen3-4B-Thinking)
- **Language(s) (NLP):** English
- **License:** Apache 2.0
- **Finetuned from model:** Qwen3-4B-Thinking

### Model Sources

- **Repository:** https://huggingface.co/afzalur/qwen-market-model
- **Base Model:** https://huggingface.co/Qwen/Qwen3-4B-Thinking

## Uses

### Direct Use

This model is designed for financial analysis of NIFTY 50 market data to predict short-term price movements. It can:

- Analyze technical indicators (RSI, MACD, Bollinger Bands) to identify market trends
- Predict directional movement (Up/Down/Flat) for the next trading day
- Estimate percentage change in price
- Provide confidence levels for predictions
- Explain reasoning behind market forecasts

The model is particularly useful for traders, financial analysts, and investors focused on the Indian stock market who need AI assistance with technical analysis.

### Downstream Use

The model can be integrated into:
- Trading dashboards and financial analysis tools
- Market research platforms
- Algorithmic trading systems (with appropriate risk management)
- Educational platforms teaching technical analysis
- Financial research applications

### Out-of-Scope Use

This model is NOT suitable for:
- Making high-stakes investment decisions without human oversight
- Long-term market forecasting (beyond a few trading days)
- Analysis of markets other than NIFTY 50 (model was not trained on these)
- Financial advice that doesn't incorporate fundamental analysis and macroeconomic factors
- Fully automated trading without risk management systems

## Bias, Risks, and Limitations

- **Historical Bias:** The model is trained on historical data and may perpetuate existing biases in market analysis
- **Overfitting Risk:** May over-optimize for patterns in its training data that don't generalize to future market conditions
- **Limited Context:** Does not incorporate news events, global economic factors, or company-specific developments unless explicitly provided
- **Market Unpredictability:** Financial markets are inherently unpredictable, especially during black swan events
- **Technical Limitations:** Predictions are probabilistic in nature and should not be treated as certainties
- **Time Sensitivity:** Market patterns change over time, potentially reducing model accuracy as market dynamics evolve

### Recommendations

- Use predictions as one input among many for investment decisions
- Combine with fundamental analysis and broader market research
- Implement strict risk management practices when using for actual trading
- Regularly evaluate model performance against recent market conditions
- Retrain periodically with newer market data to maintain relevance
- Do not use for high-frequency trading without extensive validation
- Always have human oversight for investment decisions

## How to Get Started with the Model

```python
from transformers import AutoModelForCausalLM, AutoTokenizer

# Load model and tokenizer
model = AutoModelForCausalLM.from_pretrained("afzalur/qwen-market-model", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("afzalur/qwen-market-model")

# Format prompt
system_message = {
    "role": "system",
    "content": "You are an expert financial analyst specializing in NIFTY 50 market prediction. Analyze technical indicators, options data, and market sentiment to provide accurate predictions with clear reasoning and confidence levels."
}

user_message = {
    "role": "user",
    "content": """Current NIFTY: 24500.0
Recent 5-day closes: [24400, 24450, 24480, 24500, 24500]
Recent 3-day returns (%): [0.2, 0.12, 0.08]

Technical Indicators:
- RSI(14): 65.2
- MACD: 45.2, Signal: 42.1
- Bollinger Band Width: 1.8%

Task: Analyze the data and predict next trading day movement.
Provide: 1) Technical analysis, 2) Direction (Up/Down/Flat), 3) Expected % change, 4) Confidence level"""
}

messages = [system_message, user_message]

# Format using chat template
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)

# Generate prediction
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
    **inputs,
    max_new_tokens=300,
    temperature=0.7,
    top_p=0.9,
    do_sample=True
)

# Extract just the assistant's part
response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
print(response)
```

## Training Details

### Training Data

The model was trained on a dataset consisting of:
- Historical NIFTY 50 price data from TechN50Data.xlsx
- Technical indicators calculated from price data, including RSI, MACD, and Bollinger Bands
- Options data from Max Pain.xlsx
- VIX (volatility) data from IndVix_DES.xlsx
- Additional context from Nifty Index OSA.xlsx

The dataset includes sequences of market data with lookback periods to predict future price movements, specifically focusing on next-day returns and directional movements.

### Training Procedure

#### Preprocessing

1. Technical indicators were calculated from raw price data
2. Sequences were created with lookback windows of historical data
3. Data was formatted into chat-style examples with system, user, and assistant messages
4. Temporal features were extracted from date information
5. Outlier detection and handling were implemented for extreme market events

#### Training Hyperparameters

- **Training regime:** BF16 mixed precision (where available) or FP16
- **Learning rate:** 2e-4
- **Optimizer:** AdamW with cosine learning rate schedule
- **LoRA Configuration:**
  - Rank: 16
  - Alpha: 32
  - Target modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
  - LoRA dropout: 0.05
- **Training epochs:** 3
- **Batch size:** Optimized based on available GPU memory
- **Gradient accumulation steps:** Dynamically set based on hardware
- **Sequence length:** Variable, optimized based on available memory

#### Speeds, Sizes, Times

- **Model size:** Base model 4B parameters, with LoRA adapter containing approximately 41M trainable parameters (0.58% of total)
- **Training time:** Varies based on hardware, typically 1-3 hours on consumer GPUs
- **Adapter size:** ~168MB
- **Full model size:** ~5GB
- **Inference speed:** 1-2 seconds per prediction on modern GPUs

## Evaluation

### Testing Data, Factors & Metrics

#### Testing Data

The model was evaluated on a held-out test set of NIFTY 50 data not seen during training, comprising approximately 10% of the overall dataset. The test set includes various market conditions (bullish, bearish, and sideways markets).

#### Factors

Evaluation considered:
- Market volatility conditions (high vs. low VIX periods)
- Trend strength (strong vs. weak trends)
- Market direction (uptrend vs. downtrend vs. sideways markets)
- Time periods (recent vs. older test data)
- Technical indicator regime (overbought, oversold, neutral conditions)

#### Metrics

- **Directional accuracy:** Percentage of correct Up/Down/Flat predictions
- **Mean absolute error (MAE):** For percentage change predictions
- **Confidence calibration:** How well confidence levels correlate with actual accuracy
- **Loss values:** Training and evaluation loss metrics

### Results

Due to the experimental nature of this model, specific benchmark results are still being compiled. The model demonstrates promising directional accuracy in standard market conditions but may have reduced performance during highly volatile periods or major market events.

#### Summary

The model provides valuable insights for technical analysis but should be used as one tool among many for financial decision-making. Its strengths lie in pattern recognition from technical indicators, while its limitations include sensitivity to unusual market conditions not well-represented in the training data.

## Model Examination

The model shows particular sensitivity to RSI values in the overbought (>70) and oversold (<30) regions, often giving these indicators significant weight in predictions. MACD crossovers and Bollinger Band contractions/expansions are also influential in the model's decision-making process. 

When confidence levels are high (>80%), directional accuracy tends to be significantly better than random chance, suggesting the model has learned meaningful patterns in technical indicator behavior.

## Environmental Impact

- **Hardware Type:** Consumer GPU (RTX series)
- **Hours used:** Approximately 3 hours for training
- **Cloud Provider:** N/A (local training)
- **Compute Region:** N/A
- **Carbon Emitted:** Minimal due to efficient LoRA fine-tuning approach (estimated <1kg CO2eq)

## Technical Specifications

### Model Architecture and Objective

The model is based on Qwen3-4B-Thinking, a 4 billion parameter transformer model, fine-tuned using LoRA adaptation for the specific task of market prediction. The objective function was to minimize loss on predicting market movements based on historical technical indicators and price data.

### Compute Infrastructure

#### Hardware

- GPU: Consumer-grade NVIDIA GPU with at least 8GB VRAM
- CPU: Modern multi-core processor
- RAM: Minimum 16GB system memory
- Storage: SSD with at least 20GB free space for model and datasets

#### Software

- PyTorch 2.0+
- Transformers library
- PEFT library for LoRA implementation
- Python 3.8+
- Pandas for data preprocessing
- NumPy for numerical operations
- Optional: Weights & Biases for experiment tracking

## Citation

**BibTeX:**
```
@misc{qwen-market-model,
  author = {Rahman, Afzalur},
  title = {Qwen Market Prediction Model},
  year = {2025},
  publisher = {Hugging Face},
  howpublished = {\url{https://huggingface.co/afzalur/Qwen-Market-Prediction-Model}}
}
```

**APA:**
Rahman, A. (2025). Qwen Market Prediction Model [Model]. Hugging Face. https://huggingface.co/afzalur/Qwen-Market-Prediction-Model

## Glossary

- **RSI (Relative Strength Index):** A momentum oscillator that measures the speed and change of price movements, typically on a scale of 0-100
- **MACD (Moving Average Convergence Divergence):** A trend-following momentum indicator showing the relationship between two moving averages of a security's price
- **Bollinger Bands:** A technical analysis tool defined by a set of lines plotted two standard deviations away from a simple moving average
- **VIX:** Volatility Index, a measure of market volatility
- **LoRA (Low-Rank Adaptation):** An efficient fine-tuning technique that significantly reduces the number of trainable parameters
- **Directional Accuracy:** The percentage of times the model correctly predicts the market direction (Up/Down/Flat)

## Model Card Authors

Afzalur Rahman

upwork: https://www.upwork.com/freelancers/~0198c0e561dde1229f

## Model Card Contact

For questions or feedback about this model, please contact through the Hugging Face model repository page: https://huggingface.co/afzalur/Qwen-Market-Prediction-Model