PROSTLE commited on
Commit
ebb3ef6
·
unverified ·
1 Parent(s): 8c2f11a

Update model.py

Browse files
Files changed (1) hide show
  1. model.py +27 -46
model.py CHANGED
@@ -1,8 +1,3 @@
1
- """
2
- LSTM-based stock price predictor (v2.0).
3
- Multi-feature model using Close + technical indicators.
4
- Trains with mini-batches, LR scheduling, gradient clipping, and early stopping.
5
- """
6
 
7
  import numpy as np
8
  import pandas as pd
@@ -10,17 +5,17 @@ import torch
10
  import torch.nn as nn
11
  from torch.utils.data import DataLoader, TensorDataset
12
  from sklearn.preprocessing import MinMaxScaler
 
13
 
14
 
15
- # ── Feature columns used by the model ─────────────────────────
16
- FEATURE_COLS = ["Close", "SMA_20", "EMA_20", "RSI", "MACD", "Volume"]
17
- LOOK_BACK = 60 # 60 trading days as input window
18
  PREDICT_DAYS = 5
19
 
20
 
21
- # ── LSTM Architecture ─────────────────────────────────────────
22
  class StockLSTM(nn.Module):
23
- def __init__(self, input_size: int = 6, hidden_size: int = 128,
24
  num_layers: int = 3, output_size: int = 5, dropout: float = 0.3):
25
  super().__init__()
26
  self.hidden_size = hidden_size
@@ -31,7 +26,6 @@ class StockLSTM(nn.Module):
31
  batch_first=True, dropout=dropout if num_layers > 1 else 0.0,
32
  )
33
 
34
- # Deeper head: hidden → hidden/2 → output
35
  self.head = nn.Sequential(
36
  nn.Linear(hidden_size, hidden_size // 2),
37
  nn.ReLU(),
@@ -43,17 +37,11 @@ class StockLSTM(nn.Module):
43
  h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=x.device)
44
  c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=x.device)
45
  out, _ = self.lstm(x, (h0, c0))
46
- out = self.head(out[:, -1, :]) # last time-step
47
  return out
48
 
49
 
50
- # ── Data helpers ──────────────────────────────────────────────
51
  def _prepare_features(df: pd.DataFrame):
52
- """
53
- Extract feature matrix and target vector from a DataFrame
54
- that already contains technical indicators.
55
- """
56
- # Ensure required columns exist; fall back to Close-only if not
57
  available = [c for c in FEATURE_COLS if c in df.columns]
58
  if len(available) < 2:
59
  available = ["Close"]
@@ -64,37 +52,28 @@ def _prepare_features(df: pd.DataFrame):
64
 
65
 
66
  def _prepare_data(features: np.ndarray, close_col_idx: int = 0):
67
- """
68
- Scale features, build sliding-window X/y arrays.
69
- Returns X, y, scaler, and the split index so the scaler is fit
70
- ONLY on training data (no data leakage).
71
- """
72
  n_samples = len(features)
73
  split = int(n_samples * 0.85)
74
 
75
- # Fit scaler on training portion ONLY
76
  scaler = MinMaxScaler()
77
  scaler.fit(features[:split])
78
 
79
  scaled = scaler.transform(features)
80
 
81
  X, y = [], []
82
- # y targets are next-5-day Close prices (column 0 after scaling)
83
  for i in range(LOOK_BACK, len(scaled) - PREDICT_DAYS):
84
- X.append(scaled[i - LOOK_BACK:i]) # shape (LOOK_BACK, n_features)
85
- y.append(scaled[i:i + PREDICT_DAYS, close_col_idx]) # shape (PREDICT_DAYS,)
86
 
87
  X = np.array(X, dtype=np.float32)
88
  y = np.array(y, dtype=np.float32)
89
 
90
- # Adjust split index for the windowed arrays
91
  adj_split = split - LOOK_BACK
92
  adj_split = max(1, min(adj_split, len(X) - 1))
93
 
94
  return X, y, scaler, adj_split
95
 
96
 
97
- # ── Train + Predict pipeline ─────────────────────────────────
98
  def train_and_predict(
99
  df: pd.DataFrame,
100
  epochs: int = 100,
@@ -102,23 +81,17 @@ def train_and_predict(
102
  batch_size: int = 32,
103
  patience: int = 10,
104
  ):
105
- """
106
- Train an LSTM on multi-feature data and return the next 5 predicted
107
- prices, a confidence score, and risk level.
108
- """
109
  features, close, n_features = _prepare_features(df)
110
  X, y, scaler, split = _prepare_data(features)
111
 
112
  X_train, y_train = X[:split], y[:split]
113
  X_test, y_test = X[split:], y[split:]
114
 
115
- # Torch tensors
116
  X_train_t = torch.tensor(X_train)
117
  y_train_t = torch.tensor(y_train)
118
  X_test_t = torch.tensor(X_test)
119
  y_test_t = torch.tensor(y_test)
120
 
121
- # Mini-batch DataLoader
122
  train_ds = TensorDataset(X_train_t, y_train_t)
123
  train_loader = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
124
 
@@ -134,7 +107,6 @@ def train_and_predict(
134
  optimizer, mode="min", factor=0.5, patience=5, min_lr=1e-6,
135
  )
136
 
137
- # ── Training loop with early stopping ──
138
  best_val_loss = float("inf")
139
  epochs_no_improve = 0
140
 
@@ -148,7 +120,6 @@ def train_and_predict(
148
  torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
149
  optimizer.step()
150
 
151
- # Validation
152
  model.eval()
153
  with torch.no_grad():
154
  val_preds = model(X_test_t)
@@ -156,7 +127,6 @@ def train_and_predict(
156
 
157
  scheduler.step(val_loss)
158
 
159
- # Early stopping
160
  if val_loss < best_val_loss:
161
  best_val_loss = val_loss
162
  epochs_no_improve = 0
@@ -166,17 +136,14 @@ def train_and_predict(
166
  if epochs_no_improve >= patience:
167
  break
168
 
169
- # Restore best model
170
  if best_val_loss < float("inf"):
171
  model.load_state_dict(best_state)
172
 
173
- # ── Final test evaluation ──
174
  model.eval()
175
  with torch.no_grad():
176
  test_preds = model(X_test_t).numpy()
177
  test_loss = float(criterion(torch.tensor(test_preds), y_test_t))
178
 
179
- # Confidence & risk (based on test MSE, lower is better)
180
  confidence = max(0, min(100, round((1 - test_loss * 10) * 100, 1)))
181
  if confidence >= 70:
182
  risk = "Low"
@@ -185,7 +152,6 @@ def train_and_predict(
185
  else:
186
  risk = "High"
187
 
188
- # ── Predict next 5 days ──
189
  last_window = features[-LOOK_BACK:]
190
  last_scaled = scaler.transform(last_window).reshape(1, LOOK_BACK, n_features)
191
  last_seq = torch.tensor(last_scaled, dtype=torch.float32)
@@ -193,19 +159,34 @@ def train_and_predict(
193
  with torch.no_grad():
194
  raw_pred = model(last_seq).numpy().flatten()
195
 
196
- # Inverse-transform: raw_pred is in scaled Close space (column 0)
197
- # Build a dummy array with all features set to 0, then set column 0
198
  dummy = np.zeros((len(raw_pred), n_features), dtype=np.float32)
199
  dummy[:, 0] = raw_pred
200
  predicted_prices = scaler.inverse_transform(dummy)[:, 0].tolist()
201
 
202
- # Historical last 30 days for chart overlay
203
  historical_prices = close[-30:].tolist()
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  return {
206
  "predicted_prices": [round(p, 2) for p in predicted_prices],
207
  "confidence": confidence,
208
  "risk": risk,
209
  "test_mse": round(test_loss, 6),
210
  "historical_last_30": [round(float(p), 2) for p in historical_prices],
211
- }
 
 
 
 
 
 
 
 
1
 
2
  import numpy as np
3
  import pandas as pd
 
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
 
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(),
 
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"]
 
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,
 
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
 
 
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
 
 
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)
 
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
 
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"
 
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)
 
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
+ }