PROSTLE commited on
Commit
cc926a2
·
unverified ·
1 Parent(s): f1161d8

Delete backtest_lstm.py

Browse files
Files changed (1) hide show
  1. backtest_lstm.py +0 -93
backtest_lstm.py DELETED
@@ -1,93 +0,0 @@
1
-
2
- import os
3
- import sys
4
- import pandas as pd
5
- import numpy as np
6
- import random
7
- from datetime import datetime, timedelta
8
- # Add backend directory to sys.path for imports
9
- sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
- from stock_data import fetch_stock_data, add_technical_indicators
11
- from model import train_and_predict
12
-
13
- def evaluate_prediction_accuracy(ticker, start_date, end_date, lookahead_days=5):
14
- """
15
- For a given ticker and date range, run the LSTM model as if in the past, then compare its 5-day forecast to actual closes.
16
- Returns a DataFrame with prediction, actual, and error for each window.
17
- """
18
- df = fetch_stock_data(ticker)
19
- df = add_technical_indicators(df)
20
- df = df[(df['Date'] >= start_date) & (df['Date'] <= end_date)].reset_index(drop=True)
21
- results = []
22
- for i in range(len(df) - 60 - lookahead_days):
23
- window = df.iloc[i:i+60+lookahead_days]
24
- hist = window.iloc[:60]
25
- future = window.iloc[60:60+lookahead_days]
26
- # Skip if not enough future data or if hist is too short after dropna
27
- if len(future) < lookahead_days or len(hist.dropna()) < 60:
28
- continue
29
- try:
30
- pred = train_and_predict(hist)
31
- pred_prices = pred['predicted_prices']
32
- actual_prices = future['Close'].values.tolist()
33
- errors = [abs(p - a) for p, a in zip(pred_prices, actual_prices)]
34
- results.append({
35
- 'date': hist.iloc[-1]['Date'],
36
- 'predicted': pred_prices,
37
- 'actual': actual_prices,
38
- 'mae': np.mean(errors),
39
- 'mse': np.mean([(p-a)**2 for p,a in zip(pred_prices, actual_prices)]),
40
- })
41
- except Exception as e:
42
- print(f"Skipping window at {hist.iloc[-1]['Date']} due to error: {e}")
43
- continue
44
- return pd.DataFrame(results)
45
-
46
- if __name__ == "__main__":
47
- ticker = input("Enter IPO stock ticker (e.g. RELIANCE.NS): ").strip()
48
- # Fetch all available data for the ticker (max 2 years)
49
- df_all = fetch_stock_data(ticker, period="2y", interval="1d")
50
- df_all = add_technical_indicators(df_all)
51
- if len(df_all) < 70:
52
- print("Not enough data for backtest (need at least 70 days). Exiting.")
53
- sys.exit(1)
54
- # Find all valid window start indices (where 60-row history and 5-row future have no NaNs)
55
- valid_starts = []
56
- for i in range(len(df_all) - 65):
57
- hist = df_all.iloc[i:i+60]
58
- future = df_all.iloc[i+60:i+65]
59
- if len(hist.dropna()) == 60 and len(future.dropna()) == 5:
60
- valid_starts.append(i)
61
- if not valid_starts:
62
- print("No valid windows found in the last 2 years for this stock.")
63
- sys.exit(1)
64
- # Pick a random valid window
65
- start_idx = random.choice(valid_starts)
66
- # Pick a random end index at least 65 days ahead, but not more than 120 days or end of data
67
- max_end = min(start_idx + 120, len(df_all) - 1)
68
- end_idx = random.randint(start_idx + 65, max_end)
69
- start_date = str(df_all.iloc[start_idx]['Date'])[:10]
70
- end_date = str(df_all.iloc[end_idx]['Date'])[:10]
71
- print(f"Randomly selected date range: {start_date} to {end_date}")
72
- df = evaluate_prediction_accuracy(ticker, start_date, end_date)
73
- if df.empty:
74
- print("No valid prediction windows found in this range.")
75
- # Try to print the last available predicted and actual price if possible
76
- # Use the last 60 rows for prediction and next 5 for actual
77
- hist = df_all.iloc[start_idx:start_idx+60]
78
- future = df_all.iloc[start_idx+60:start_idx+65]
79
- if len(hist.dropna()) == 60 and len(future.dropna()) == 5:
80
- try:
81
- pred = train_and_predict(hist)
82
- pred_prices = pred['predicted_prices']
83
- actual_prices = future['Close'].values.tolist()
84
- print(f"Predicted 5-day prices: {pred_prices}")
85
- print(f"Actual 5-day prices: {actual_prices}")
86
- except Exception as e:
87
- print(f"Could not compute prediction for last window: {e}")
88
- else:
89
- print(df[['date', 'mae', 'mse']])
90
- print("Average MAE:", df['mae'].mean())
91
- print("Average MSE:", df['mse'].mean())
92
- df.to_csv(f"trained_models/{ticker}_backtest_results.csv", index=False)
93
- print(f"Results saved to trained_models/{ticker}_backtest_results.csv")