PROSTLE commited on
Commit
497574a
Β·
unverified Β·
1 Parent(s): 35e09d9

Update and rename model.py to backend/model.py

Browse files
Files changed (1) hide show
  1. model.py β†’ backend/model.py +285 -24
model.py β†’ backend/model.py RENAMED
@@ -5,6 +5,8 @@ import torch.nn as nn
5
  from torch.utils.data import DataLoader, TensorDataset
6
  from sklearn.preprocessing import MinMaxScaler
7
  from datetime import datetime, timedelta
 
 
8
 
9
  from enterprise_models import (
10
  get_enterprise_predictions,
@@ -13,16 +15,80 @@ from enterprise_models import (
13
 
14
  FEATURE_COLS = ["Close", "SMA_20", "EMA_20", "RSI", "MACD", "Volume",
15
  "BB_Upper", "BB_Lower", "BB_Width", "ATR"]
16
- LOOK_BACK = 60
17
- PREDICT_DAYS = 5
18
-
19
- # Blend: 60% legacy (LSTM), 40% enterprise (H2O + DataRobot + Alteryx)
20
- LEGACY_WEIGHT = 0.45
21
- ENTERPRISE_WEIGHT = 0.55
 
 
 
 
 
 
 
 
 
22
 
23
  # Drawdown dampening: when recent 2-day drop > 3%, reduce rubber-band bounce
24
- DRAWDOWN_THRESHOLD_PCT = 2.8
25
- DAMPER_STRENGTH = 0.46 # pull 46% toward flat when triggered
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
 
28
  def _apply_drawdown_dampening(
@@ -32,7 +98,8 @@ def _apply_drawdown_dampening(
32
  ) -> list[float]:
33
  """
34
  When stock just suffered a sharp sell-off (e.g. earnings miss), dampen
35
- immediate V-shaped bounce predictions. Pull path toward continuation/flat.
 
36
  """
37
  if len(df) < 3 or len(predicted_prices) != PREDICT_DAYS:
38
  return predicted_prices
@@ -50,18 +117,72 @@ def _apply_drawdown_dampening(
50
  if day1_pred <= current_price:
51
  return predicted_prices
52
 
53
- # Dampen: pull toward flat path (current_price flat for day1, gradual to day5)
54
- flat_day1 = current_price
55
- flat_day5 = current_price + 0.2 * (predicted_prices[-1] - current_price) # very mild recovery
56
  damped = []
57
  for i in range(PREDICT_DAYS):
58
  t = (i + 1) / PREDICT_DAYS
59
- flat_val = flat_day1 + t * (flat_day5 - flat_day1)
60
- blended = (1 - DAMPER_STRENGTH) * predicted_prices[i] + DAMPER_STRENGTH * flat_val
 
 
 
61
  damped.append(round(blended, 2))
62
  return damped
63
 
64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  class StockLSTM(nn.Module):
66
  def __init__(self, input_size: int = 10, hidden_size: int = 128,
67
  num_layers: int = 3, output_size: int = 5, dropout: float = 0.3):
@@ -122,13 +243,71 @@ def _prepare_data(features: np.ndarray, close_col_idx: int = 0):
122
  return X, y, scaler, adj_split
123
 
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  def train_and_predict(
126
  df: pd.DataFrame,
127
  epochs: int = 100,
128
  lr: float = 0.001,
129
  batch_size: int = 32,
130
  patience: int = 10,
 
 
 
 
 
 
 
 
131
  ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  features, close, n_features = _prepare_features(df)
133
  X, y, scaler, split = _prepare_data(features)
134
 
@@ -141,7 +320,10 @@ def train_and_predict(
141
  y_test_t = torch.tensor(y_test)
142
 
143
  train_ds = TensorDataset(X_train_t, y_train_t)
144
- train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
 
 
 
145
 
146
  model = StockLSTM(
147
  input_size=n_features,
@@ -212,22 +394,95 @@ def train_and_predict(
212
  lstm_prices = scaler.inverse_transform(dummy)[:, 0].tolist()
213
  current_price = float(close[-1])
214
 
215
- # Enterprise ensemble: H2O + DataRobot + Alteryx (40% weight)
216
  h2o_p, dr_p, alt_p = get_enterprise_predictions(df, current_price)
217
  enterprise_blend = blend_enterprise_predictions(h2o_p, dr_p, alt_p)
218
 
 
 
 
 
 
219
  if enterprise_blend is not None:
220
- predicted_prices = [
221
- round(LEGACY_WEIGHT * lstm_prices[i] + ENTERPRISE_WEIGHT * enterprise_blend[i], 2)
222
- for i in range(PREDICT_DAYS)
223
- ]
 
 
 
 
 
 
 
224
  models_used = ["LSTM", "H2O", "DataRobot", "Alteryx"]
225
  else:
226
- predicted_prices = [round(p, 2) for p in lstm_prices]
227
  models_used = ["LSTM"]
228
 
229
- # Drawdown dampening: reduce rubber-band bounce after sharp sell-offs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  predicted_prices = _apply_drawdown_dampening(predicted_prices, current_price, df)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  historical_prices = close[-30:].tolist()
233
 
@@ -247,7 +502,7 @@ def train_and_predict(
247
  pred_dates.append(d.strftime("%Y-%m-%d"))
248
  bdays += 1
249
 
250
- return {
251
  "predicted_prices": predicted_prices,
252
  "confidence": confidence,
253
  "risk": risk,
@@ -255,6 +510,12 @@ def train_and_predict(
255
  "historical_last_30": [round(float(p), 2) for p in historical_prices],
256
  "historical_dates": hist_dates,
257
  "prediction_dates": pred_dates,
258
- "blend": f"{int(LEGACY_WEIGHT*100)}% legacy + {int(ENTERPRISE_WEIGHT*100)}% enterprise",
259
  "models_used": models_used,
 
260
  }
 
 
 
 
 
 
5
  from torch.utils.data import DataLoader, TensorDataset
6
  from sklearn.preprocessing import MinMaxScaler
7
  from datetime import datetime, timedelta
8
+ import time
9
+ import hashlib
10
 
11
  from enterprise_models import (
12
  get_enterprise_predictions,
 
15
 
16
  FEATURE_COLS = ["Close", "SMA_20", "EMA_20", "RSI", "MACD", "Volume",
17
  "BB_Upper", "BB_Lower", "BB_Width", "ATR"]
18
+ LOOK_BACK = 60 # Number of historical days used as LSTM input window
19
+ PREDICT_DAYS = 5 # Number of days to forecast
20
+ # ── 5-Factor Prediction Weights ──────────────────────
21
+ # Each factor contributes to the final predicted price.
22
+ # Directional factors (sentiment, technical, LLM) push the base prediction
23
+ # up or down based on their score.
24
+ W_LSTM = 0.15 # Day-by-day price shape
25
+ W_ENTERPRISE = 0.25 # H2O + DataRobot + Alteryx magnitude
26
+ W_TECHNICAL = 0.13 # RSI / MACD / SMA trend scoring
27
+ W_SENTIMENT = 0.15 # FinBERT + VADER news sentiment
28
+ W_LLM = 0.32 # Google Gemini market analysis
29
+
30
+ # Max price adjustment from directional factors (% of price)
31
+ # Prevents extreme swings from sentiment/LLM alone
32
+ MAX_DIRECTIONAL_IMPACT_PCT = 3.0
33
 
34
  # Drawdown dampening: when recent 2-day drop > 3%, reduce rubber-band bounce
35
+ DRAWDOWN_THRESHOLD_PCT = 2.9
36
+ DAMPER_STRENGTH = 0.45 # moderate damping; too high creates flatline
37
+
38
+ # Minimum daily price movement as % of price (prevents dead-pulse flat lines)
39
+ MIN_DAILY_MOVE_PCT = 0.15
40
+
41
+ # Deterministic seed for reproducibility
42
+ RANDOM_SEED = 42
43
+
44
+ # ── Prediction cache ─────────────────────────────────────
45
+ # Cache predictions keyed by (ticker_hash, last_close_price_rounded).
46
+ # TTL = 15 minutes. Only re-trains when market data actually changes.
47
+ CACHE_TTL_SECONDS = 15 * 60 # 15 minutes
48
+ _prediction_cache = {} # {cache_key: {"result": ..., "timestamp": ...}}
49
+
50
+
51
+ def _make_cache_key(df: pd.DataFrame) -> str:
52
+ """Build a stable cache key. Uses last close rounded to integer + date + data length.
53
+ This prevents re-training on every intraday tick while still invalidating
54
+ when a new trading day's data arrives."""
55
+ close = df["Close"].values
56
+ # Round last close to nearest integer β€” small intraday changes don't bust cache
57
+ last_close_rounded = round(float(close[-1]))
58
+ # Use the date of the last data point if available
59
+ if "Date" in df.columns:
60
+ last_date = str(df["Date"].iloc[-1])[:10]
61
+ else:
62
+ last_date = datetime.now().strftime("%Y-%m-%d")
63
+ fingerprint = f"{len(df)}_{last_close_rounded}_{last_date}"
64
+ return hashlib.md5(fingerprint.encode()).hexdigest()
65
+
66
+
67
+ def _get_cached(key: str) -> dict | None:
68
+ """Return cached prediction if still valid, else None."""
69
+ entry = _prediction_cache.get(key)
70
+ if entry and (time.time() - entry["timestamp"]) < CACHE_TTL_SECONDS:
71
+ return entry["result"]
72
+ return None
73
+
74
+
75
+ def _set_cache(key: str, result: dict):
76
+ """Store prediction in cache."""
77
+ _prediction_cache[key] = {"result": result, "timestamp": time.time()}
78
+ # Evict old entries (keep max 20)
79
+ if len(_prediction_cache) > 20:
80
+ oldest = min(_prediction_cache, key=lambda k: _prediction_cache[k]["timestamp"])
81
+ del _prediction_cache[oldest]
82
+
83
+
84
+ def _set_deterministic_seeds():
85
+ """Set all random seeds for reproducible LSTM training."""
86
+ torch.manual_seed(RANDOM_SEED)
87
+ np.random.seed(RANDOM_SEED)
88
+ if torch.cuda.is_available():
89
+ torch.cuda.manual_seed_all(RANDOM_SEED)
90
+ torch.backends.cudnn.deterministic = True
91
+ torch.backends.cudnn.benchmark = False
92
 
93
 
94
  def _apply_drawdown_dampening(
 
98
  ) -> list[float]:
99
  """
100
  When stock just suffered a sharp sell-off (e.g. earnings miss), dampen
101
+ immediate V-shaped bounce prediction. Instead of pulling toward FLAT
102
+ (which creates dead-pulse), pull toward a gentle continuation slope.
103
  """
104
  if len(df) < 3 or len(predicted_prices) != PREDICT_DAYS:
105
  return predicted_prices
 
117
  if day1_pred <= current_price:
118
  return predicted_prices
119
 
120
+ # Instead of flat line (bathtub), use a gentle downward slope
121
+ # that respects the recent momentum direction, then gradually recovers
122
+ recent_slope = worst_return / 100 # e.g., -0.03 for -3% drop
123
  damped = []
124
  for i in range(PREDICT_DAYS):
125
  t = (i + 1) / PREDICT_DAYS
126
+ # Day 1-2: continue slight downtrend; Day 3-5: gradual recovery
127
+ momentum_factor = recent_slope * 0.3 * (1 - t) # fades over time
128
+ recovery_factor = t * (predicted_prices[-1] - current_price) / current_price * 0.5
129
+ slope_val = current_price * (1 + momentum_factor + recovery_factor)
130
+ blended = (1 - DAMPER_STRENGTH) * predicted_prices[i] + DAMPER_STRENGTH * slope_val
131
  damped.append(round(blended, 2))
132
  return damped
133
 
134
 
135
+ def _add_minimum_volatility(
136
+ predicted_prices: list[float],
137
+ current_price: float,
138
+ df: pd.DataFrame,
139
+ ) -> list[float]:
140
+ """
141
+ Prevent 'dead pulse' flat lines by adding natural wave-like fluctuation.
142
+ Uses recent historical daily return PATTERN (up/down sequence) as a
143
+ template for realistic micro-movements, instead of pushing all days
144
+ in the same direction (which creates a straight line).
145
+ """
146
+ if len(predicted_prices) != PREDICT_DAYS:
147
+ return predicted_prices
148
+
149
+ close = df["Close"].values
150
+ if len(close) >= 6:
151
+ daily_rets = np.diff(close[-6:]) / close[-6:-1]
152
+ avg_daily_vol = max(float(np.std(daily_rets)), MIN_DAILY_MOVE_PCT / 100)
153
+ # Use the SIGN pattern of recent returns for natural up/down alternation
154
+ ret_signs = np.sign(daily_rets).tolist()
155
+ else:
156
+ avg_daily_vol = MIN_DAILY_MOVE_PCT / 100
157
+ ret_signs = [1, -1, 1, -1, 1] # default alternating
158
+
159
+ min_move = current_price * max(MIN_DAILY_MOVE_PCT / 100, avg_daily_vol * 0.3)
160
+
161
+ result = list(predicted_prices)
162
+ prev = current_price
163
+ for i in range(len(result)):
164
+ diff = abs(result[i] - prev)
165
+ if diff < min_move:
166
+ # Use historical sign pattern for natural alternation
167
+ sign_idx = i % len(ret_signs)
168
+ direction = ret_signs[sign_idx] if ret_signs[sign_idx] != 0 else 1
169
+ # Scale the jitter β€” smaller near endpoints, larger in middle
170
+ middle_factor = 1.0 + 0.5 * (1 - abs(2 * i / (PREDICT_DAYS - 1) - 1))
171
+ result[i] = round(prev + direction * min_move * middle_factor, 2)
172
+ prev = result[i]
173
+
174
+ # Preserve the original endpoint β€” scale back so day-5 matches
175
+ if abs(result[-1] - predicted_prices[-1]) > 0.01:
176
+ shift = predicted_prices[-1] - result[-1]
177
+ # Gradually apply the shift more toward the end
178
+ for i in range(len(result)):
179
+ t = (i + 1) / len(result)
180
+ result[i] = round(result[i] + shift * t, 2)
181
+
182
+ return result
183
+
184
+
185
+
186
  class StockLSTM(nn.Module):
187
  def __init__(self, input_size: int = 10, hidden_size: int = 128,
188
  num_layers: int = 3, output_size: int = 5, dropout: float = 0.3):
 
243
  return X, y, scaler, adj_split
244
 
245
 
246
+
247
+ def detect_crisis_mode(sentiment_score: float, atr_pct: float, vix_level: float) -> tuple[bool, str]:
248
+ """
249
+ Detect if the market is in 'Crisis Mode' based on sentiment and volatility.
250
+ Triggers:
251
+ 1. Severe negative sentiment (<-0.5)
252
+ 2. High ATR (>3.0%) indicating panic
253
+ 3. High VIX (>24) indicating fear
254
+ """
255
+ reasons = []
256
+ if sentiment_score <= -0.5:
257
+ reasons.append("Severe Negative Sentiment")
258
+ if atr_pct > 3.0:
259
+ reasons.append("High Volatility (ATR > 3%)")
260
+ if vix_level > 24.0:
261
+ reasons.append("High Fear (VIX > 24)")
262
+
263
+ if reasons:
264
+ return True, ", ".join(reasons)
265
+ return False, ""
266
+
267
+
268
  def train_and_predict(
269
  df: pd.DataFrame,
270
  epochs: int = 100,
271
  lr: float = 0.001,
272
  batch_size: int = 32,
273
  patience: int = 10,
274
+ sentiment_score: float = 0.0,
275
+ technical_score: float = 0.0,
276
+ llm_score: float = 0.0,
277
+ sentiment_label: str = "neutral",
278
+ technical_direction: str = "neutral",
279
+ llm_direction: str = "neutral",
280
+ atr_pct: float = 0.0,
281
+ vix_level: float = 0.0,
282
  ):
283
+ # ── Cache check: return existing prediction if data hasn't changed ──
284
+ # Note: If crisis condition changes within TTL, cache might be stale,
285
+ # but 15 min TTL is acceptable trade-off vs retraining.
286
+ cache_key = _make_cache_key(df)
287
+ cached = _get_cached(cache_key)
288
+ if cached is not None:
289
+ return cached
290
+
291
+ # ── Set deterministic seeds for reproducibility ──
292
+ _set_deterministic_seeds()
293
+
294
+ # ── Crisis Mode Detection ──
295
+ is_crisis, crisis_reason = detect_crisis_mode(sentiment_score, atr_pct, vix_level)
296
+
297
+ # Dynamic weighting
298
+ if is_crisis:
299
+ w_lstm = 0.10
300
+ w_enterprise = 0.15
301
+ w_technical = 0.15
302
+ w_sentiment = 0.20 # Updated per user request
303
+ w_llm = 0.40 # Updated per user request
304
+ else:
305
+ w_lstm = W_LSTM
306
+ w_enterprise = W_ENTERPRISE
307
+ w_technical = W_TECHNICAL
308
+ w_sentiment = W_SENTIMENT
309
+ w_llm = W_LLM
310
+
311
  features, close, n_features = _prepare_features(df)
312
  X, y, scaler, split = _prepare_data(features)
313
 
 
320
  y_test_t = torch.tensor(y_test)
321
 
322
  train_ds = TensorDataset(X_train_t, y_train_t)
323
+ # Use seeded generator for deterministic shuffling
324
+ g = torch.Generator()
325
+ g.manual_seed(RANDOM_SEED)
326
+ train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True, generator=g)
327
 
328
  model = StockLSTM(
329
  input_size=n_features,
 
394
  lstm_prices = scaler.inverse_transform(dummy)[:, 0].tolist()
395
  current_price = float(close[-1])
396
 
397
+ # ── Enterprise ensemble ──
398
  h2o_p, dr_p, alt_p = get_enterprise_predictions(df, current_price)
399
  enterprise_blend = blend_enterprise_predictions(h2o_p, dr_p, alt_p)
400
 
401
+ # ── Build base prediction from LSTM shape + enterprise magnitude ──
402
+ lstm_enterprise_weight = w_lstm + w_enterprise
403
+ lstm_frac = w_lstm / lstm_enterprise_weight
404
+ ent_frac = w_enterprise / lstm_enterprise_weight
405
+
406
  if enterprise_blend is not None:
407
+ # Shift LSTM to match enterprise endpoint, preserving curvature
408
+ ent_day5 = enterprise_blend[-1]
409
+ lstm_day5 = lstm_prices[-1]
410
+ endpoint_shift = ent_day5 - lstm_day5
411
+
412
+ base_prices = []
413
+ for i in range(PREDICT_DAYS):
414
+ t = (i + 1) / PREDICT_DAYS
415
+ shifted_lstm = lstm_prices[i] + endpoint_shift * t
416
+ blended = lstm_frac * shifted_lstm + ent_frac * enterprise_blend[i]
417
+ base_prices.append(blended)
418
  models_used = ["LSTM", "H2O", "DataRobot", "Alteryx"]
419
  else:
420
+ base_prices = list(lstm_prices)
421
  models_used = ["LSTM"]
422
 
423
+ # ── Apply directional factors (sentiment + technical + LLM) ──
424
+ # Each factor's score (-1 to +1) is converted to a price adjustment %
425
+ # The adjustment builds over the 5 days (stronger at day 5)
426
+ directional_weight = w_sentiment + w_technical + w_llm
427
+ if directional_weight > 0:
428
+ # Weighted directional score
429
+ weighted_dir_score = (
430
+ w_sentiment * sentiment_score +
431
+ w_technical * technical_score +
432
+ w_llm * llm_score
433
+ ) / directional_weight
434
+
435
+ # Cap maximum impact
436
+ max_impact = current_price * MAX_DIRECTIONAL_IMPACT_PCT / 100
437
+ dir_adjustment = weighted_dir_score * max_impact
438
+
439
+ predicted_prices = []
440
+ for i in range(PREDICT_DAYS):
441
+ t = (i + 1) / PREDICT_DAYS # builds over time: 0.2 to 1.0
442
+ # Directional push increases toward day 5
443
+ adjusted = base_prices[i] + dir_adjustment * t
444
+ predicted_prices.append(round(adjusted, 2))
445
+ else:
446
+ predicted_prices = [round(p, 2) for p in base_prices]
447
+
448
+ # ── Post-processing ──
449
  predicted_prices = _apply_drawdown_dampening(predicted_prices, current_price, df)
450
+ predicted_prices = _add_minimum_volatility(predicted_prices, current_price, df)
451
+
452
+ # ── Factor breakdown for the API response ──
453
+ factor_breakdown = {
454
+ "lstm": {
455
+ "weight": int(w_lstm * 100),
456
+ "contribution": "shape",
457
+ "description": "Day-by-day price dynamics",
458
+ },
459
+ "enterprise": {
460
+ "weight": int(w_enterprise * 100),
461
+ "contribution": "magnitude",
462
+ "models": [m for m in models_used if m != "LSTM"],
463
+ "description": "H2O + DataRobot + Alteryx ensemble",
464
+ },
465
+ "technical": {
466
+ "weight": int(w_technical * 100),
467
+ "score": round(technical_score, 3),
468
+ "direction": technical_direction,
469
+ "description": "RSI / MACD / SMA trend analysis",
470
+ },
471
+ "sentiment": {
472
+ "weight": int(w_sentiment * 100),
473
+ "score": round(sentiment_score, 3),
474
+ "direction": sentiment_label,
475
+ "description": "FinBERT + VADER news analysis",
476
+ },
477
+ "llm": {
478
+ "weight": int(w_llm * 100),
479
+ "score": round(llm_score, 3),
480
+ "direction": llm_direction,
481
+ "description": "Gemini AI market reasoning",
482
+ },
483
+ "mode": "Crisis" if is_crisis else "Normal",
484
+ "crisis_reason": crisis_reason,
485
+ }
486
 
487
  historical_prices = close[-30:].tolist()
488
 
 
502
  pred_dates.append(d.strftime("%Y-%m-%d"))
503
  bdays += 1
504
 
505
+ result = {
506
  "predicted_prices": predicted_prices,
507
  "confidence": confidence,
508
  "risk": risk,
 
510
  "historical_last_30": [round(float(p), 2) for p in historical_prices],
511
  "historical_dates": hist_dates,
512
  "prediction_dates": pred_dates,
513
+ "blend": f"LSTM {int(W_LSTM*100)}% + Enterprise {int(W_ENTERPRISE*100)}% + Technical {int(W_TECHNICAL*100)}% + News {int(W_SENTIMENT*100)}% + LLM {int(W_LLM*100)}%",
514
  "models_used": models_used,
515
+ "factor_breakdown": factor_breakdown,
516
  }
517
+
518
+ # ── Store in cache ──
519
+ _set_cache(cache_key, result)
520
+
521
+ return result