danielthatu12 commited on
Commit
c5c01e8
Β·
verified Β·
1 Parent(s): 3996113

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +200 -282
README.md CHANGED
@@ -1,420 +1,338 @@
1
  ---
2
- title: stockpredictor
3
- sdk: gradio
4
- emoji: 😻
5
  colorFrom: blue
6
  colorTo: indigo
7
- sdk_version: 6.14.0
 
 
8
  pinned: false
 
9
  ---
10
- # πŸ“ˆ Stock Price Predictor
 
 
 
 
 
11
 
12
  AI-powered stock price predictions using LSTM neural networks, technical analysis, and market sentiment analysis.
13
 
14
- **Features:**
15
- - ⚑ Fast predictions (<2 seconds)
16
- - 🧠 Deep Learning LSTM model
17
- - πŸ“Š Technical indicators (RSI, MACD, Volatility, etc.)
18
- - πŸ’­ News sentiment analysis
19
- - πŸ’Ύ 24-hour prediction cache
20
- - 🎯 85-86% directional accuracy
21
- - πŸ“± Web interface (Gradio)
 
 
 
 
 
 
 
 
 
22
 
23
  ---
24
 
25
- ## πŸš€ Quick Start (Local)
26
 
27
- ### 1. Clone or Download Files
28
 
29
- ```bash
30
- # Download all files from this repository
31
- # You should have:
32
- # - app.py
33
- # - model.py
34
- # - requirements.txt
35
- # - .env.example
36
- # - .gitignore
37
- # - README.md
38
- ```
39
 
40
- ### 2. Create Virtual Environment (Recommended)
41
 
42
- ```bash
43
- # Windows
44
  python -m venv venv
45
  venv\Scripts\activate
46
 
47
- # macOS/Linux
 
48
  python3 -m venv venv
49
  source venv/bin/activate
50
- ```
51
 
52
- ### 3. Install Dependencies
53
 
54
- ```bash
 
 
 
55
  pip install -r requirements.txt
56
- ```
57
 
58
- ### 4. Setup Environment Variables
59
 
60
- ```bash
61
- # Copy example file to .env
62
- cp .env.example .env
 
 
63
 
64
- # Edit .env and add your API key:
65
- # FINNHUB_API_KEY=your_api_key_here
66
- ```
67
 
68
- **Get Free API Key:**
69
- - Go to https://finnhub.io/register
70
- - Sign up (free tier available)
71
- - Copy your API key to `.env`
 
 
 
72
 
73
- ### 5. Run the App
74
 
75
- ```bash
76
  python app.py
77
- ```
78
 
79
- The app will start at `http://localhost:7860`
 
 
 
80
 
81
  ---
82
 
83
- ## 🌐 Deploy on HuggingFace Spaces
84
 
85
- ### Step 1: Create HF Space
86
 
87
- 1. Go to https://huggingface.co/spaces
88
- 2. Click "Create new Space"
89
- 3. Fill in:
90
- - **Space name**: `stock-predictor` (or any name)
91
- - **License**: `mit` or `apache-2.0`
92
- - **SDK**: Select **Gradio**
93
- - **Visibility**: Public or Private
94
 
95
- 4. Click "Create Space"
96
 
97
- ### Step 2: Upload Files
98
 
99
- You'll see a command to clone your space. Then:
 
 
 
 
 
 
 
 
 
100
 
101
- ```bash
102
- cd your-space-repo
103
- git clone https://huggingface.co/spaces/YOUR_USERNAME/stock-predictor
104
- cd stock-predictor
105
- ```
106
 
107
- Copy these files into the directory:
108
- ```
 
 
 
 
 
 
 
 
 
 
 
 
109
  app.py
110
  model.py
111
  requirements.txt
 
112
  .env.example
113
  .gitignore
114
- README.md
115
- ```
116
 
117
- ### Step 3: Setup Environment Variables (HF Spaces)
118
 
119
- In HF Spaces, go to **Settings** β†’ **Repository secrets** β†’ **Add secret**
 
 
120
 
121
- Add:
122
- - **Name**: `FINNHUB_API_KEY`
123
- - **Value**: Your Finnhub API key
124
 
125
- **DO NOT** commit `.env` file to git. HF Spaces will automatically load secrets as environment variables.
126
 
127
- ### Step 4: Push to HF Spaces
128
 
129
- ```bash
130
- git add .
131
- git commit -m "Initial stock predictor setup"
132
- git push
133
- ```
134
 
135
- HF Spaces will automatically:
136
- 1. Install dependencies from `requirements.txt`
137
- 2. Run `app.py`
138
- 3. Deploy your app with a public URL
139
 
140
  ---
141
 
142
- ## πŸ“– Usage Guide
143
 
144
- ### Via Web Interface
145
 
146
- 1. Enter stock symbol (e.g., `AAPL`, `GOOGL`, `MSFT`)
147
- 2. Select number of days to predict (1-30)
148
- 3. Click **πŸš€ Predict**
149
- 4. View results table and analysis
150
 
151
- ### Supported Symbols
152
 
153
- Any stock ticker from:
154
- - **US**: NASDAQ, NYSE (AAPL, GOOGL, MSFT, TSLA, etc.)
155
- - **Europe**: LSE, Euronext (various symbols)
156
- - **India**: NSE, BSE (SBIN.NS, INFY.NS, etc.)
157
 
158
- ### Example Stocks
159
 
160
- ```
161
- AAPL - Apple
162
- GOOGL - Google (Alphabet)
163
- MSFT - Microsoft
164
- AMZN - Amazon
165
- TSLA - Tesla
166
- NVDA - NVIDIA
167
- META - Meta (Facebook)
168
- NFLX - Netflix
169
- ```
170
 
171
  ---
172
 
173
- ## πŸ”§ How It Works
174
-
175
- ### 1. Data Collection (Step 1)
176
- - Fetches 100 days of historical price data from Yahoo Finance
177
- - Uses: Open, High, Low, Close, Volume prices
178
-
179
- ### 2. Feature Engineering (Step 2)
180
- Calculates 6 technical indicators:
181
- - **Close**: Stock closing price
182
- - **RSI**: Relative Strength Index (momentum, 14-day)
183
- - **MACD**: Moving Average Convergence Divergence
184
- - **Volatility**: Standard deviation of returns (10-day)
185
- - **SMA20**: 20-day Simple Moving Average (trend)
186
- - **ROC**: Rate of Change (5-day)
187
-
188
- ### 3. Preprocessing (Step 3)
189
- - Normalizes all features to [0, 1] range using MinMaxScaler
190
- - Creates sequences of 30 days for LSTM training
191
-
192
- ### 4. LSTM Model Training (Step 4)
193
- - **Architecture**: 1 LSTM layer (32 hidden units) + 1 Dense layer
194
- - **Training**: 50 epochs, batch size 32
195
- - **Optimizer**: Adam (lr=0.001)
196
- - **Loss**: Mean Squared Error
197
- - **Time**: ~2 minutes on CPU
198
-
199
- ### 5. Price Prediction (Step 5)
200
- - Uses trained LSTM to predict next N days
201
- - Autoregressive approach: uses previous predictions for future predictions
202
- - Inverse transforms predictions back to actual prices
203
-
204
- ### 6. Sentiment Analysis (Step 6)
205
- - Fetches latest 10 news articles from Finnhub
206
- - Uses VADER sentiment analyzer (rule-based, fast, accurate)
207
- - Classifies each article as Positive/Negative/Neutral
208
 
209
- ---
210
 
211
- ## ⚑ Performance
 
 
 
 
 
 
 
212
 
213
- | Metric | Value |
214
- |--------|-------|
215
- | **Single Prediction** | 1-2 seconds |
216
- | **Cached Prediction** | 50-100ms |
217
- | **Model Training** | 2 minutes |
218
- | **Memory Usage** | 2-3GB |
219
- | **Package Size** | 600MB |
220
- | **Accuracy (MAPE)** | 4.8% |
221
- | **Directional Accuracy** | 76-77% |
222
 
223
- ### Optimization Techniques
 
 
 
 
224
 
225
- - βœ… PyTorch instead of TensorFlow (6x smaller)
226
- - βœ… yfinance instead of Alpha Vantage (no API limits)
227
- - βœ… Direct LSTM (no XGBoost residuals)
228
- - βœ… VADER sentiment (no transformers)
229
- - βœ… 24-hour prediction cache
230
- - βœ… Gradio native support
231
 
232
  ---
233
 
234
- ## πŸ“Š Model Architecture
 
 
 
 
 
 
 
 
235
 
236
- ```
237
- Input Sequence (30 days Γ— 6 features)
238
- ↓
239
- LSTM Layer
240
- (32 hidden units)
241
- ↓
242
- Dense Layer
243
- (1 output)
244
- ↓
245
- Predicted Price
246
- ```
247
 
248
  ---
249
 
250
- ## 🎯 Accuracy Analysis
251
 
252
- ### Why 85-86%?
253
 
254
- Stock predictions face inherent limitations:
255
- - **Market efficiency**: ~95% of info already priced in
256
- - **Black swan events**: Unpredictable shocks
257
- - **Noise**: High-frequency trading noise
258
 
259
- ### MAPE (Mean Absolute Percentage Error)
260
 
261
- - **Interpretation**: Average prediction error
262
- - **Value**: 4.8% (Β±5% of stock price)
263
- - **Trade-off**: Acceptable for directional trading
 
 
264
 
265
- ### Directional Accuracy
266
 
267
- - **Definition**: Predicting if price goes UP or DOWN
268
- - **Accuracy**: 76-77%
269
- - **Better than**: Random guess (50%)
270
 
271
  ---
272
 
273
- ## 🚨 Limitations & Disclaimers
 
 
 
 
 
 
 
 
 
274
 
275
- ⚠️ **This is for educational purposes only. NOT financial advice.**
276
 
277
- ### Limitations
278
 
279
- 1. **Historical data dependent**: Assumes past patterns repeat
280
- 2. **No fundamental analysis**: Ignores earnings, dividends, etc.
281
- 3. **No geopolitical events**: Can't predict wars, pandemics
282
- 4. **Short-term only**: Best for 1-7 day predictions
283
- 5. **Sentiment limited**: Only recent news, not long-term sentiment
284
 
285
- ### What This Can't Predict
286
 
287
- - Company earnings surprises
288
- - FDA drug approvals
289
- - Geopolitical events
290
- - Market crashes
291
- - Sector rotations
292
 
293
- ### Investment Disclaimer
 
 
294
 
295
- **Past performance β‰  Future results**
296
 
297
- Always:
298
- - βœ… Conduct your own research
299
- - βœ… Diversify your portfolio
300
- - βœ… Consult a financial advisor
301
- - βœ… Manage risk properly
302
- - βœ… Never invest what you can't afford to lose
303
 
304
  ---
305
 
306
- ## πŸ” Security
307
 
308
- ### API Keys
309
 
310
- - **Never commit** `.env` file
311
- - Use **HF Spaces Secrets** for deployment
312
- - API keys are not logged or stored
313
- - Data comes from Yahoo Finance (public)
314
 
315
- ### Data Privacy
316
 
317
- - βœ… No user data stored
318
- - βœ… Predictions cached locally (24 hours)
319
- - βœ… No ads or tracking
320
- - βœ… Open source code
321
 
322
- ---
323
 
324
- ## πŸ› Troubleshooting
325
 
326
- ### "ModuleNotFoundError: No module named 'torch'"
327
 
328
- ```bash
329
- pip install -r requirements.txt
330
- ```
331
 
332
- ### "No data for symbol XXXX"
333
 
334
- 1. Check symbol spelling (e.g., `AAPL` not `APPLE`)
335
- 2. Use international format (e.g., `SBIN.NS` for Indian stocks)
336
- 3. Check stock is listed on a major exchange
337
 
338
- ### "FINNHUB_API_KEY not set"
339
 
340
- Sentiment analysis will be skipped, but predictions will still work:
341
- 1. Get key: https://finnhub.io/register
342
- 2. Add to `.env` file
343
- 3. Restart app
344
 
345
- ### "CUDA out of memory"
346
 
347
- Will automatically fall back to CPU (slower but works)
348
 
349
- ### First prediction is slow
350
 
351
- This is expected (model training takes ~2 minutes). Subsequent predictions use cached model (<2 seconds).
352
 
353
- ### "Connection timeout" or "Too many requests"
354
 
355
- API rate limit reached. Solution:
356
- - Wait a few minutes
357
- - Cache prevents repeated requests (enabled by default)
358
 
359
  ---
360
 
361
- ## πŸ“š File Structure
 
 
362
 
363
- ```
364
- stock-predictor/
365
- β”œβ”€β”€ app.py # Gradio interface (main entry point)
366
- β”œβ”€β”€ model.py # LSTM model and prediction logic
367
- β”œβ”€β”€ requirements.txt # Python dependencies
368
- β”œβ”€β”€ .env.example # Environment variables template
369
- β”œβ”€β”€ .gitignore # Git ignore rules
370
- └── README.md # This file
371
-
372
- Optional (generated):
373
- β”œβ”€β”€ .env # Your API keys (NOT committed)
374
- β”œβ”€β”€ lstm_model.pt # Trained model cache
375
- β”œβ”€β”€ prediction_cache.pkl # Prediction cache
376
- └── __pycache__/ # Python cache
377
- ```
378
 
379
  ---
380
 
381
- ## 🀝 Contributing
382
 
383
- Improvements welcome! Ideas:
384
- - Better sentiment analysis
385
- - Multiple models ensemble
386
- - Real-time streaming
387
- - Mobile app
388
- - Explainability (SHAP)
389
 
390
- ---
391
 
392
- ## πŸ“„ License
393
 
394
- MIT License - Free for personal and commercial use
395
 
396
  ---
397
 
398
- ## πŸ”— Resources
 
 
 
 
 
 
 
 
 
399
 
400
- - **Finnhub API**: https://finnhub.io/
401
- - **Gradio**: https://gradio.app/
402
- - **HF Spaces**: https://huggingface.co/spaces
403
- - **PyTorch**: https://pytorch.org/
404
- - **yfinance**: https://github.com/ranaroussi/yfinance
405
 
406
  ---
407
 
408
- ## πŸ“§ Support
 
 
409
 
410
- For issues:
411
- 1. Check troubleshooting section above
412
- 2. Verify API key is set
413
- 3. Check stock symbol is valid
414
- 4. Check internet connection
415
 
416
  ---
417
 
418
- **Made with ❀️ for stock enthusiasts and traders.**
 
 
 
 
 
 
 
 
419
 
420
- *Last updated: 2024*
 
1
  ---
2
+ title: StockPredictor
3
+ emoji: πŸ“ˆ
 
4
  colorFrom: blue
5
  colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: "5.29.0"
8
+ app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
+
13
+ ---
14
+
15
+ title: StockPredictor emoji: πŸ“ˆ colorFrom: blue colorTo: indigo sdk: gradio sdk_version: "5.29.0" app_file: app.py pinned: false license: mit
16
+
17
+ πŸ“ˆ Stock Price Predictor
18
 
19
  AI-powered stock price predictions using LSTM neural networks, technical analysis, and market sentiment analysis.
20
 
21
+ πŸš€ Features
22
+
23
+ ⚑ Fast predictions
24
+
25
+ 🧠 LSTM Deep Learning model
26
+
27
+ πŸ“Š Technical indicators (RSI, MACD, SMA, ROC)
28
+
29
+ πŸ’­ News sentiment analysis
30
+
31
+ πŸ’Ύ Prediction caching
32
+
33
+ πŸ“± Gradio web interface
34
+
35
+ 🌍 Supports US and Indian stocks
36
+
37
+
38
 
39
  ---
40
 
41
+ πŸ› οΈ Local Setup
42
 
43
+ 1. Clone Repository
44
 
45
+ git clone https://github.com/your-username/stock-predictor.git
46
+ cd stock-predictor
47
+
48
+ 2. Create Virtual Environment
 
 
 
 
 
 
49
 
50
+ Windows
51
 
 
 
52
  python -m venv venv
53
  venv\Scripts\activate
54
 
55
+ macOS/Linux
56
+
57
  python3 -m venv venv
58
  source venv/bin/activate
 
59
 
 
60
 
61
+ ---
62
+
63
+ 3. Install Dependencies
64
+
65
  pip install -r requirements.txt
 
66
 
 
67
 
68
+ ---
69
+
70
+ 4. Add Environment Variables
71
+
72
+ Create a .env file:
73
 
74
+ FINNHUB_API_KEY=your_api_key_here
 
 
75
 
76
+ Get a free API key from:
77
+
78
+ https://finnhub.io/register
79
+
80
+
81
+
82
+ ---
83
 
84
+ 5. Run Application
85
 
 
86
  python app.py
 
87
 
88
+ The application will start at:
89
+
90
+ http://localhost:7860
91
+
92
 
93
  ---
94
 
95
+ 🌐 Deploy on Hugging Face Spaces
96
 
97
+ Step 1: Create Space
98
 
99
+ 1. Open:
 
 
 
 
 
 
100
 
 
101
 
 
102
 
103
+ https://huggingface.co/spaces
104
+
105
+ 2. Click:
106
+
107
+
108
+
109
+ Create New Space
110
+
111
+ 3. Configure:
112
+
113
 
 
 
 
 
 
114
 
115
+ SDK: Gradio
116
+
117
+ License: MIT
118
+
119
+ Visibility: Public or Private
120
+
121
+
122
+
123
+ ---
124
+
125
+ Step 2: Upload Project Files
126
+
127
+ Upload the following files:
128
+
129
  app.py
130
  model.py
131
  requirements.txt
132
+ README.md
133
  .env.example
134
  .gitignore
 
 
135
 
 
136
 
137
+ ---
138
+
139
+ Step 3: Add Repository Secrets
140
 
141
+ Go to:
 
 
142
 
143
+ Settings β†’ Repository secrets
144
 
145
+ Add the following secret:
146
 
147
+ Name: FINNHUB_API_KEY
148
+ Value: your_api_key
 
 
 
149
 
 
 
 
 
150
 
151
  ---
152
 
153
+ Step 4: Automatic Deployment
154
 
155
+ Hugging Face Spaces will automatically:
156
 
157
+ Install dependencies
 
 
 
158
 
159
+ Run app.py
160
 
161
+ Create a public deployment URL
 
 
 
162
 
 
163
 
 
 
 
 
 
 
 
 
 
 
164
 
165
  ---
166
 
167
+ πŸ“Š Supported Stock Symbols
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
168
 
169
+ US Stocks
170
 
171
+ AAPL
172
+ MSFT
173
+ GOOGL
174
+ AMZN
175
+ TSLA
176
+ NVDA
177
+ META
178
+ NFLX
179
 
180
+ Indian Stocks
 
 
 
 
 
 
 
 
181
 
182
+ SBIN.NS
183
+ INFY.NS
184
+ RELIANCE.NS
185
+ TCS.NS
186
+ HDFCBANK.NS
187
 
 
 
 
 
 
 
188
 
189
  ---
190
 
191
+ 🧠 Model Architecture
192
+
193
+ Input Features
194
+ ↓
195
+ LSTM Layer (32 Units)
196
+ ↓
197
+ Dense Layer
198
+ ↓
199
+ Predicted Price
200
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  ---
203
 
204
+ πŸ“ˆ Technical Indicators Used
205
 
206
+ RSI
207
 
208
+ MACD
 
 
 
209
 
210
+ SMA20
211
 
212
+ Volatility
213
+
214
+ ROC
215
+
216
+ Closing Price
217
 
 
218
 
 
 
 
219
 
220
  ---
221
 
222
+ ⚑ Performance
223
+
224
+ Metric Value
225
+
226
+ Prediction Speed 1-2 seconds
227
+ Cached Predictions 50-100ms
228
+ Training Time ~2 minutes
229
+ Memory Usage 2-3 GB
230
+ Accuracy (MAPE) ~4.8%
231
+ Directional Accuracy 76-77%
232
 
 
233
 
 
234
 
235
+ ---
236
+
237
+ ⚠️ Disclaimer
238
+
239
+ This project is for educational purposes only.
240
 
241
+ Not financial advice
242
 
243
+ Markets are unpredictable
 
 
 
 
244
 
245
+ Always conduct your own research
246
+
247
+ Invest responsibly
248
 
 
249
 
 
 
 
 
 
 
250
 
251
  ---
252
 
253
+ πŸ” Security Notes
254
 
255
+ Never upload .env
256
 
257
+ Use HF Spaces Secrets for API keys
 
 
 
258
 
259
+ No user data stored
260
 
261
+ Uses public Yahoo Finance data
 
 
 
262
 
 
263
 
 
264
 
265
+ ---
266
 
267
+ πŸ› Common Issues
268
+
269
+ Configuration Error on HF Spaces
270
 
271
+ Use this exact YAML block at the top of README.md:
272
 
 
 
 
273
 
 
274
 
275
+ Important:
 
 
 
276
 
277
+ YAML must start at line 1
278
 
279
+ No spaces before ---
280
 
281
+ app_file is required
282
 
283
+ Use supported Gradio versions only
284
 
 
285
 
 
 
 
286
 
287
  ---
288
 
289
+ ModuleNotFoundError
290
+
291
+ pip install -r requirements.txt
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
 
294
  ---
295
 
296
+ No Data Found
297
 
298
+ Verify stock symbol format.
 
 
 
 
 
299
 
300
+ Example:
301
 
302
+ SBIN.NS
303
 
 
304
 
305
  ---
306
 
307
+ πŸ“ Project Structure
308
+
309
+ stock-predictor/
310
+ β”‚
311
+ β”œβ”€β”€ app.py
312
+ β”œβ”€β”€ model.py
313
+ β”œβ”€β”€ requirements.txt
314
+ β”œβ”€β”€ README.md
315
+ β”œβ”€β”€ .env.example
316
+ └── .gitignore
317
 
 
 
 
 
 
318
 
319
  ---
320
 
321
+ πŸ“„ License
322
+
323
+ MIT License
324
 
 
 
 
 
 
325
 
326
  ---
327
 
328
+ ❀️ Built With
329
+
330
+ Gradio
331
+
332
+ PyTorch
333
+
334
+ yfinance
335
+
336
+ Finnhub API
337
 
338
+ Python