PROSTLE commited on
Commit
d82d475
·
unverified ·
1 Parent(s): 7a3dfcd

Update intraday_model.py

Browse files
Files changed (1) hide show
  1. intraday_model.py +174 -447
intraday_model.py CHANGED
@@ -1,465 +1,192 @@
1
- """
2
- Intraday XGBoost model for short-term price movement forecasting.
3
- Predicts next 3-5 bar (minute) returns using tabular features.
4
- Used exclusively by the Auto Trade tab.
5
-
6
- Includes:
7
- - In-memory model cache (10-min TTL) to avoid retraining every tick
8
- - Sector-strength feature
9
- - generate_intraday_signal() - full pipeline producing structured signals
10
- """
11
-
12
- import time
13
  import numpy as np
14
  import pandas as pd
15
- from xgboost import XGBRegressor
16
- from sklearn.model_selection import TimeSeriesSplit
17
- from sklearn.metrics import mean_squared_error
18
- from stock_data import (
19
- fetch_intraday_data,
20
- add_intraday_indicators,
21
- fetch_india_vix,
22
- fetch_nifty_trend,
23
- detect_ipo_stock,
24
- )
25
- from trader import (
26
- compute_dynamic_levels,
27
- detect_market_regime,
28
- )
29
-
30
-
31
- # Features the model uses (must match columns from add_intraday_indicators)
32
- FEATURE_COLS = [
33
- # Price & returns
34
- "returns_1bar", "returns_5bar", "returns_15bar",
35
- # Moving average distances
36
- "price_vs_ema9", "price_vs_ema21", "price_vs_sma50",
37
- # Momentum
38
- "RSI", "MACD", "MACD_Signal", "MACD_Hist",
39
- # Volatility
40
- "ATR", "BB_Width", "BB_Position",
41
- # Volume
42
- "volume_ratio", "price_vs_vwap",
43
- # Trend
44
- "Supertrend_Dir", "supertrend_dist",
45
- # Time
46
- "minute_of_day", "is_first_hour", "is_last_hour",
47
- ]
48
-
49
- # Market context features (appended at prediction time)
50
- MARKET_FEATURES = ["vix_level", "vix_change_5d", "nifty_trend_dir", "sector_strength"]
51
-
52
- FORWARD_BARS = 5 # Predict return over next 5 minutes
53
- MODEL_CACHE_TTL = 600 # 10 minutes in seconds
54
-
55
- # --- In-memory model cache ---
56
- _model_cache: dict = {} # ticker -> {model, feature_names, metrics, timestamp}
57
-
58
-
59
- def _cache_valid(ticker: str) -> bool:
60
- """Check if cached model for ticker is still within TTL."""
61
- if ticker not in _model_cache:
62
- return False
63
- return (time.time() - _model_cache[ticker]["timestamp"]) < MODEL_CACHE_TTL
64
-
65
-
66
- # --- Sector strength helper ---
67
- SECTOR_INDEX_MAP = {
68
- "Financial Services": "^NSEBANK",
69
- "Information Technology": "^CNXIT",
70
- "Pharmaceuticals": "^CNXPHARMA",
71
- "Energy": "^CNXENERGY",
72
- "Automobile": "^CNXAUTO",
73
- "FMCG": "^CNXFMCG",
74
- "Metals & Mining": "^CNXMETAL",
75
- "Realty": "^CNXREALTY",
76
- "Media": "^CNXMEDIA",
77
- }
78
-
79
-
80
- def _fetch_sector_strength(ticker: str) -> float:
81
- """
82
- Compute sector relative strength vs Nifty 50 over last 5 days.
83
- Returns a float: >0 means sector outperforming, <0 underperforming.
84
- """
85
- import yfinance as yf
86
- try:
87
- stock_info = yf.Ticker(ticker).info
88
- sector = stock_info.get("sector", "")
89
- idx_symbol = SECTOR_INDEX_MAP.get(sector)
90
- if not idx_symbol:
91
- return 0.0
92
-
93
- idx = yf.Ticker(idx_symbol)
94
- hist = idx.history(period="5d")
95
- if len(hist) < 2:
96
- return 0.0
97
-
98
- sector_ret = (float(hist["Close"].iloc[-1]) / float(hist["Close"].iloc[0]) - 1) * 100
99
-
100
- nifty = yf.Ticker("^NSEI")
101
- nh = nifty.history(period="5d")
102
- if len(nh) < 2:
103
- return 0.0
104
- nifty_ret = (float(nh["Close"].iloc[-1]) / float(nh["Close"].iloc[0]) - 1) * 100
105
-
106
- return round(sector_ret - nifty_ret, 4)
107
- except Exception:
108
- return 0.0
109
-
110
-
111
- # --- Core model functions ---
112
-
113
- def _build_target(df: pd.DataFrame, forward_bars: int = FORWARD_BARS) -> pd.Series:
114
- """Target = percentage return over the next N bars."""
115
- future_close = df["Close"].shift(-forward_bars)
116
- target = ((future_close - df["Close"]) / df["Close"]) * 100
117
- return target
118
-
119
-
120
- def _add_market_context(df: pd.DataFrame, vix: dict, nifty: dict,
121
- sector_strength: float = 0.0) -> pd.DataFrame:
122
- """Append market-level context features to every row."""
123
- df["vix_level"] = vix.get("vix_level", 15.0)
124
- df["vix_change_5d"] = vix.get("vix_change_5d", 0.0)
125
- trend_map = {"bullish": 1, "flat": 0, "bearish": -1}
126
- df["nifty_trend_dir"] = trend_map.get(nifty.get("trend", "flat"), 0)
127
- df["sector_strength"] = sector_strength
128
- return df
129
-
130
-
131
- def _prepare_features(df: pd.DataFrame) -> tuple:
132
- """Select feature columns and return X matrix + available column names."""
133
- all_cols = FEATURE_COLS + MARKET_FEATURES
134
- available = [c for c in all_cols if c in df.columns]
135
- X = df[available].values.astype(np.float32)
136
- return X, available
137
-
138
-
139
- def train_intraday_model(df: pd.DataFrame, vix: dict, nifty: dict,
140
- sector_strength: float = 0.0) -> dict:
141
- """
142
- Train an XGBoost regressor on intraday data.
143
- Returns dict with model, scaler info, feature names, and metrics.
144
- """
145
- df = _add_market_context(df, vix, nifty, sector_strength)
146
-
147
- # Build target
148
- df["target"] = _build_target(df, FORWARD_BARS)
149
-
150
- # Drop rows where target is NaN (last FORWARD_BARS rows)
151
- df_clean = df.dropna(subset=["target"]).copy()
152
-
153
- if len(df_clean) < 100:
154
- raise ValueError(f"Insufficient data for training: {len(df_clean)} rows (need >=100)")
155
-
156
- X, feature_names = _prepare_features(df_clean)
157
- y = df_clean["target"].values.astype(np.float32)
158
-
159
- # Replace any remaining NaN/inf in features
160
- X = np.nan_to_num(X, nan=0.0, posinf=0.0, neginf=0.0)
161
-
162
- # Time-series split: train on first 80%, test on last 20%
163
- split_idx = int(len(X) * 0.8)
164
- X_train, X_test = X[:split_idx], X[split_idx:]
165
- y_train, y_test = y[:split_idx], y[split_idx:]
166
-
167
- model = XGBRegressor(
168
- n_estimators=200,
169
- max_depth=5,
170
- learning_rate=0.05,
171
- subsample=0.8,
172
- colsample_bytree=0.8,
173
- min_child_weight=5,
174
- reg_alpha=0.1,
175
- reg_lambda=1.0,
176
- random_state=42,
177
- verbosity=0,
178
- )
179
 
180
- model.fit(
181
- X_train, y_train,
182
- eval_set=[(X_test, y_test)],
183
- verbose=False,
184
- )
185
 
186
- # Evaluate
187
- y_pred = model.predict(X_test)
188
- mse = mean_squared_error(y_test, y_pred)
189
- rmse = float(np.sqrt(mse))
190
 
191
- # Directional accuracy (did we predict the right direction?)
192
- dir_correct = np.sum(np.sign(y_pred) == np.sign(y_test))
193
- dir_accuracy = float(dir_correct / len(y_test)) * 100 if len(y_test) > 0 else 0
194
 
195
- return {
196
- "model": model,
197
- "feature_names": feature_names,
198
- "rmse": round(rmse, 4),
199
- "dir_accuracy": round(dir_accuracy, 1),
200
- "train_samples": len(X_train),
201
- "test_samples": len(X_test),
202
- }
203
 
 
 
 
 
204
 
205
- def compute_confidence(expected_return: float, rmse: float,
206
- dir_accuracy: float, regime_str: str) -> float:
207
- """
208
- Compute a confidence score (0-100) for the prediction.
209
- Factors: magnitude of expected return vs model RMSE,
210
- directional accuracy, and current market regime.
211
- """
212
- # Signal-to-noise ratio: how big is the predicted move vs model error
213
- snr = abs(expected_return) / rmse if rmse > 0 else 0
214
- snr_score = min(40, snr * 20) # 0-40 points
215
-
216
- # Directional accuracy contribution
217
- dir_score = min(35, max(0, (dir_accuracy - 45) * 0.7)) # 0-35 points
218
-
219
- # Regime bonus/penalty
220
- regime_bonus = {
221
- "BULL_LOW_VOL": 15,
222
- "BULL_HIGH_VOL": 10,
223
- "SIDEWAYS": 0,
224
- "BEAR_HIGH_VOL": -5,
225
- }.get(regime_str, 5)
226
-
227
- # Return-direction bonus: predicted returns in the direction we'd trade
228
- dir_bonus = 10 if expected_return > 0.3 else (5 if expected_return > 0 else 0)
229
-
230
- confidence = snr_score + dir_score + regime_bonus + dir_bonus
231
- return round(max(0, min(100, confidence)), 1)
232
-
233
-
234
- def intraday_predict(ticker: str) -> dict:
235
- """
236
- Full pipeline: fetch data -> add indicators -> add market context ->
237
- train XGBoost -> predict next bars -> return raw prediction data.
238
- Uses cache when available.
239
- """
240
- # 1. Fetch intraday data
241
- df = fetch_intraday_data(ticker, period="5d", interval="1m")
242
- df = add_intraday_indicators(df)
243
-
244
- if len(df) < 120:
245
- return {
246
- "ticker": ticker,
247
- "status": "insufficient_data",
248
- "message": f"Only {len(df)} bars available (need >=120)",
249
- }
250
-
251
- # 2. Fetch market context
252
- vix = fetch_india_vix()
253
- nifty = fetch_nifty_trend()
254
- sector_str = _fetch_sector_strength(ticker)
255
-
256
- # 3. Train model or use cache
257
- if _cache_valid(ticker):
258
- cached = _model_cache[ticker]
259
- model = cached["model"]
260
- feature_names = cached["feature_names"]
261
- result_metrics = {
262
- "rmse": cached["rmse"],
263
- "dir_accuracy": cached["dir_accuracy"],
264
- "train_samples": cached["train_samples"],
265
- "test_samples": cached["test_samples"],
266
- }
267
- else:
268
- try:
269
- result = train_intraday_model(df.copy(), vix, nifty, sector_str)
270
- except ValueError as e:
271
- return {"ticker": ticker, "status": "train_error", "message": str(e)}
272
-
273
- model = result["model"]
274
- feature_names = result["feature_names"]
275
- result_metrics = {
276
- "rmse": result["rmse"],
277
- "dir_accuracy": result["dir_accuracy"],
278
- "train_samples": result["train_samples"],
279
- "test_samples": result["test_samples"],
280
- }
281
-
282
- # Cache the trained model
283
- _model_cache[ticker] = {
284
- "model": model,
285
- "feature_names": feature_names,
286
- "timestamp": time.time(),
287
- **result_metrics,
288
- }
289
-
290
- # 4. Predict on latest bar
291
- df_latest = _add_market_context(df, vix, nifty, sector_str)
292
- X_latest, _ = _prepare_features(df_latest)
293
- X_latest = np.nan_to_num(X_latest, nan=0.0, posinf=0.0, neginf=0.0)
294
-
295
- # Predict for the last bar
296
- last_features = X_latest[-1:].reshape(1, -1)
297
- expected_return = float(model.predict(last_features)[0])
298
-
299
- # 5. Get current indicators for the signal
300
- last_row = df.iloc[-1]
301
- current_price = float(last_row["Close"])
302
- current_atr = float(last_row["ATR"])
303
- current_rsi = float(last_row["RSI"])
304
- current_supertrend_dir = int(last_row["Supertrend_Dir"])
305
- current_vwap = float(last_row["VWAP"])
306
- current_volume_ratio = float(last_row["volume_ratio"])
307
-
308
- # 6. Feature importances (top 5)
309
- importances = model.feature_importances_
310
- top_indices = np.argsort(importances)[-5:][::-1]
311
- top_features = [
312
- {"name": feature_names[i], "importance": round(float(importances[i]), 3)}
313
- for i in top_indices if i < len(feature_names)
314
- ]
315
 
316
- return {
317
- "ticker": ticker,
318
- "status": "ok",
319
- "expected_return_pct": round(expected_return, 4),
320
- "forward_bars": FORWARD_BARS,
321
- "current_price": round(current_price, 2),
322
- "current_atr": round(current_atr, 4),
323
- "current_rsi": round(current_rsi, 2),
324
- "supertrend_dir": current_supertrend_dir,
325
- "vwap": round(current_vwap, 2),
326
- "volume_ratio": round(current_volume_ratio, 2),
327
- "model_rmse": result_metrics["rmse"],
328
- "model_dir_accuracy": result_metrics["dir_accuracy"],
329
- "train_samples": result_metrics["train_samples"],
330
- "vix": vix,
331
- "nifty_trend": nifty,
332
- "sector_strength": sector_str,
333
- "top_features": top_features,
334
- }
335
 
336
 
337
- # --- Full signal generator ---
338
-
339
- def generate_intraday_signal(ticker: str) -> dict:
340
- """
341
- Full pipeline: predict -> detect regime -> compute dynamic SL/TP ->
342
- apply entry threshold -> return structured trading signal.
343
-
344
- Output example:
345
- {
346
- "action": "BUY",
347
- "ticker": "RELIANCE.NS",
348
- "entry_price": 2987.40,
349
- "sl_price": 2952.20, "sl_pct": -1.18,
350
- "tp1_price": 3024.60, "tp1_pct": 1.24,
351
- "tp2_price": 3057.80, "tp2_pct": 2.35,
352
- "confidence": 67.3,
353
- "expected_return_pct": 1.12,
354
- "regime": "BULL_LOW_VOL",
355
- ...
356
- }
357
- """
358
- # 1. Get raw prediction
359
- pred = intraday_predict(ticker)
360
-
361
- if pred.get("status") != "ok":
362
- return {
363
- "action": "SKIP",
364
- "ticker": ticker,
365
- "reason": pred.get("message", "Prediction failed"),
366
- "status": pred.get("status", "error"),
367
- }
368
-
369
- expected_return = pred["expected_return_pct"]
370
- current_price = pred["current_price"]
371
- current_atr = pred["current_atr"]
372
-
373
- # 2. Build stock-level indicators for regime detection
374
- stock_indicators = {
375
- "RSI": pred["current_rsi"],
376
- "price": current_price,
377
- "Supertrend_Dir": pred["supertrend_dir"],
378
- "MACD": 0,
379
- "MACD_Signal": 0,
380
- }
381
 
382
- # 3. Detect regime (uses VIX + Nifty + stock signals)
383
- regime_info = detect_market_regime(
384
- indicators=stock_indicators,
385
- vix=pred["vix"],
386
- nifty=pred["nifty_trend"],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  )
388
- regime = regime_info["regime"]
389
- entry_threshold = regime_info["entry_threshold"]
390
- min_confidence = regime_info["min_confidence"]
391
-
392
- # 4. Check IPO status
393
- ipo_info = detect_ipo_stock(ticker)
394
- is_ipo = ipo_info.get("is_ipo", False)
395
-
396
- # 5. Compute dynamic SL/TP
397
- levels = compute_dynamic_levels(current_price, current_atr, regime, is_ipo)
398
-
399
- # 6. Compute confidence
400
- confidence = compute_confidence(
401
- expected_return,
402
- pred["model_rmse"],
403
- pred["model_dir_accuracy"],
404
- regime,
405
  )
406
 
407
- # 7. Apply entry threshold
408
- if expected_return >= entry_threshold and confidence >= min_confidence:
409
- action = "BUY"
410
- elif expected_return > 0 and confidence >= min_confidence * 1.3:
411
- # Relaxed entry if confidence is very high
412
- action = "BUY"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413
  else:
414
- action = "WAIT"
415
-
416
- # Build reason string for WAIT
417
- reason = None
418
- if action == "WAIT":
419
- parts = []
420
- if expected_return < entry_threshold:
421
- parts.append(
422
- f"Expected return {expected_return:.2f}% < threshold {entry_threshold}%"
423
- )
424
- if confidence < min_confidence:
425
- parts.append(
426
- f"Confidence {confidence} < minimum {min_confidence}"
427
- )
428
- reason = "; ".join(parts) if parts else "Conditions not met"
429
-
430
- signal = {
431
- "action": action,
432
- "ticker": ticker,
433
- "entry_price": current_price,
434
- "sl_price": levels["sl_price"],
435
- "sl_pct": levels["sl_pct"],
436
- "tp1_price": levels["tp1_price"],
437
- "tp1_pct": levels["tp1_pct"],
438
- "tp2_price": levels["tp2_price"] if levels["include_tp2"] else None,
439
- "tp2_pct": levels["tp2_pct"] if levels["include_tp2"] else None,
440
- "confidence": confidence,
441
- "expected_return_pct": round(expected_return, 4),
442
- "regime": regime,
443
- "regime_details": regime_info,
444
- "is_ipo": is_ipo,
445
- "ipo_age_days": ipo_info.get("ipo_age_days"),
446
- "current_atr": pred["current_atr"],
447
- "current_rsi": pred["current_rsi"],
448
- "supertrend_dir": pred["supertrend_dir"],
449
- "vwap": pred["vwap"],
450
- "volume_ratio": pred["volume_ratio"],
451
- "sector_strength": pred.get("sector_strength", 0),
452
- "model_metrics": {
453
- "rmse": pred["model_rmse"],
454
- "dir_accuracy": pred["model_dir_accuracy"],
455
- "train_samples": pred["train_samples"],
456
- },
457
- "top_features": pred["top_features"],
458
- "vix": pred["vix"],
459
- "nifty_trend": pred["nifty_trend"],
460
- }
461
 
462
- if reason:
463
- signal["reason"] = reason
 
464
 
465
- return signal
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
 
 
 
 
 
 
 
 
 
 
 
2
  import numpy as np
3
  import pandas as pd
4
+ import torch
5
+ import torch.nn as nn
6
+ from torch.utils.data import DataLoader, TensorDataset
7
+ from sklearn.preprocessing import MinMaxScaler
8
+ from datetime import datetime, timedelta
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
 
 
 
 
10
 
11
+ FEATURE_COLS = ["Close", "SMA_20", "EMA_20", "RSI", "MACD", "Volume",
12
+ "BB_Upper", "BB_Lower", "BB_Width", "ATR"]
13
+ LOOK_BACK = 60
14
+ PREDICT_DAYS = 5
15
 
 
 
 
16
 
17
+ class StockLSTM(nn.Module):
18
+ def __init__(self, input_size: int = 10, hidden_size: int = 128,
19
+ num_layers: int = 3, output_size: int = 5, dropout: float = 0.3):
20
+ super().__init__()
21
+ self.hidden_size = hidden_size
22
+ self.num_layers = num_layers
 
 
23
 
24
+ self.lstm = nn.LSTM(
25
+ input_size, hidden_size, num_layers,
26
+ batch_first=True, dropout=dropout if num_layers > 1 else 0.0,
27
+ )
28
 
29
+ self.head = nn.Sequential(
30
+ nn.Linear(hidden_size, hidden_size // 2),
31
+ nn.ReLU(),
32
+ nn.Dropout(dropout),
33
+ nn.Linear(hidden_size // 2, output_size),
34
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ def forward(self, x):
37
+ h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=x.device)
38
+ c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=x.device)
39
+ out, _ = self.lstm(x, (h0, c0))
40
+ out = self.head(out[:, -1, :])
41
+ return out
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
 
44
+ def _prepare_features(df: pd.DataFrame):
45
+ available = [c for c in FEATURE_COLS if c in df.columns]
46
+ if len(available) < 2:
47
+ available = ["Close"]
48
+
49
+ features = df[available].values.astype(np.float32)
50
+ close = df["Close"].values.astype(np.float32)
51
+ return features, close, len(available)
52
+
53
+
54
+ def _prepare_data(features: np.ndarray, close_col_idx: int = 0):
55
+ n_samples = len(features)
56
+ split = int(n_samples * 0.85)
57
+
58
+ scaler = MinMaxScaler()
59
+ scaler.fit(features[:split])
60
+
61
+ scaled = scaler.transform(features)
62
+
63
+ X, y = [], []
64
+ for i in range(LOOK_BACK, len(scaled) - PREDICT_DAYS):
65
+ X.append(scaled[i - LOOK_BACK:i])
66
+ y.append(scaled[i:i + PREDICT_DAYS, close_col_idx])
67
+
68
+ X = np.array(X, dtype=np.float32)
69
+ y = np.array(y, dtype=np.float32)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ adj_split = split - LOOK_BACK
72
+ adj_split = max(1, min(adj_split, len(X) - 1))
73
+
74
+ return X, y, scaler, adj_split
75
+
76
+
77
+ def train_and_predict(
78
+ df: pd.DataFrame,
79
+ epochs: int = 100,
80
+ lr: float = 0.001,
81
+ batch_size: int = 32,
82
+ patience: int = 10,
83
+ ):
84
+ features, close, n_features = _prepare_features(df)
85
+ X, y, scaler, split = _prepare_data(features)
86
+
87
+ X_train, y_train = X[:split], y[:split]
88
+ X_test, y_test = X[split:], y[split:]
89
+
90
+ X_train_t = torch.tensor(X_train)
91
+ y_train_t = torch.tensor(y_train)
92
+ X_test_t = torch.tensor(X_test)
93
+ y_test_t = torch.tensor(y_test)
94
+
95
+ train_ds = TensorDataset(X_train_t, y_train_t)
96
+ train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
97
+
98
+ model = StockLSTM(
99
+ input_size=n_features,
100
+ hidden_size=128,
101
+ num_layers=3,
102
+ output_size=PREDICT_DAYS,
103
  )
104
+ criterion = nn.MSELoss()
105
+ optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=1e-5)
106
+ scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
107
+ optimizer, mode="min", factor=0.5, patience=5, min_lr=1e-6,
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  )
109
 
110
+ best_val_loss = float("inf")
111
+ epochs_no_improve = 0
112
+
113
+ for epoch in range(epochs):
114
+ model.train()
115
+ for xb, yb in train_loader:
116
+ optimizer.zero_grad()
117
+ preds = model(xb)
118
+ loss = criterion(preds, yb)
119
+ loss.backward()
120
+ torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
121
+ optimizer.step()
122
+
123
+ model.eval()
124
+ with torch.no_grad():
125
+ val_preds = model(X_test_t)
126
+ val_loss = float(criterion(val_preds, y_test_t))
127
+
128
+ scheduler.step(val_loss)
129
+
130
+ if val_loss < best_val_loss:
131
+ best_val_loss = val_loss
132
+ epochs_no_improve = 0
133
+ best_state = {k: v.clone() for k, v in model.state_dict().items()}
134
+ else:
135
+ epochs_no_improve += 1
136
+ if epochs_no_improve >= patience:
137
+ break
138
+
139
+ if best_val_loss < float("inf"):
140
+ model.load_state_dict(best_state)
141
+
142
+ model.eval()
143
+ with torch.no_grad():
144
+ test_preds = model(X_test_t).numpy()
145
+ test_loss = float(criterion(torch.tensor(test_preds), y_test_t))
146
+
147
+ confidence = max(0, min(100, round((1 - test_loss * 10) * 100, 1)))
148
+ if confidence >= 70:
149
+ risk = "Low"
150
+ elif confidence >= 45:
151
+ risk = "Medium"
152
  else:
153
+ risk = "High"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
 
155
+ last_window = features[-LOOK_BACK:]
156
+ last_scaled = scaler.transform(last_window).reshape(1, LOOK_BACK, n_features)
157
+ last_seq = torch.tensor(last_scaled, dtype=torch.float32)
158
 
159
+ with torch.no_grad():
160
+ raw_pred = model(last_seq).numpy().flatten()
161
+
162
+ dummy = np.zeros((len(raw_pred), n_features), dtype=np.float32)
163
+ dummy[:, 0] = raw_pred
164
+ predicted_prices = scaler.inverse_transform(dummy)[:, 0].tolist()
165
+
166
+ historical_prices = close[-30:].tolist()
167
+
168
+ if "Date" in df.columns:
169
+ last_dates = df["Date"].iloc[-30:]
170
+ hist_dates = [str(d.date()) if hasattr(d, 'date') else str(d)[:10] for d in last_dates]
171
+ else:
172
+ hist_dates = [(datetime.now() - timedelta(days=30-i)).strftime("%Y-%m-%d") for i in range(30)]
173
+
174
+ last_date = datetime.now()
175
+ pred_dates = []
176
+ bdays = 0
177
+ d = last_date
178
+ while bdays < PREDICT_DAYS:
179
+ d += timedelta(days=1)
180
+ if d.weekday() < 5:
181
+ pred_dates.append(d.strftime("%Y-%m-%d"))
182
+ bdays += 1
183
+
184
+ return {
185
+ "predicted_prices": [round(p, 2) for p in predicted_prices],
186
+ "confidence": confidence,
187
+ "risk": risk,
188
+ "test_mse": round(test_loss, 6),
189
+ "historical_last_30": [round(float(p), 2) for p in historical_prices],
190
+ "historical_dates": hist_dates,
191
+ "prediction_dates": pred_dates,
192
+ }