-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
274 lines (228 loc) Β· 9.64 KB
/
Copy pathserver.py
File metadata and controls
274 lines (228 loc) Β· 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# server.py
# Flask backend server for AI Stock Advisor web application
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import pandas as pd
import joblib
import numpy as np
import os
import warnings
from datetime import datetime, timedelta
import traceback
warnings.filterwarnings('ignore')
app = Flask(__name__)
CORS(app) # Enable Cross-Origin Resource Sharing
# Global variables to store loaded data and models
model_data = None
models = {}
def load_data_and_models():
"""Load the feature-engineered data and trained models on startup."""
global model_data, models
try:
# Load the feature-engineered data
if os.path.exists('feature_engineered_data.csv'):
model_data = pd.read_csv('feature_engineered_data.csv', parse_dates=['Date'])
print(f"β Loaded feature-engineered data with {len(model_data)} rows")
else:
print("β Error: 'feature_engineered_data.csv' not found")
return False
# Load the trained models
model_files = {
'short': 'short_term_model.joblib',
'medium': 'medium_term_model.joblib',
'long': 'long_term_model.joblib'
}
for horizon, filename in model_files.items():
if os.path.exists(filename):
models[horizon] = joblib.load(filename)
print(f"β Loaded {horizon}-term model from {filename}")
else:
print(f"β Warning: {filename} not found")
if not models:
print("β Error: No model files found")
return False
return True
except Exception as e:
print(f"β Error loading data and models: {e}")
traceback.print_exc()
return False
def get_latest_data_for_prediction():
"""Get the latest data point for each stock for prediction."""
if model_data is None:
return None
# Get the most recent date for each stock
latest_data = model_data.loc[model_data.groupby('Symbol')['Date'].idxmax()]
return latest_data
def filter_recommendations_by_risk(recommended_stocks, risk_level):
"""Filter and sort recommendations based on risk tolerance."""
if recommended_stocks.empty:
return recommended_stocks
if risk_level == 'Low':
# Low risk: prefer stocks with lower volatility
return recommended_stocks.sort_values(by='Volatility_20', ascending=True)
elif risk_level == 'Medium':
# Medium risk: balanced approach, exclude extreme volatility
vol_q1 = recommended_stocks['Volatility_20'].quantile(0.25)
vol_q3 = recommended_stocks['Volatility_20'].quantile(0.75)
filtered = recommended_stocks[
(recommended_stocks['Volatility_20'] >= vol_q1) &
(recommended_stocks['Volatility_20'] <= vol_q3)
]
return filtered.sort_values(by='Volatility_20') if not filtered.empty else recommended_stocks
else: # High risk
# High risk: prefer stocks with higher volatility (potential for higher returns)
return recommended_stocks.sort_values(by='Volatility_20', ascending=False)
@app.route('/')
def serve_frontend():
"""Serve the main HTML file."""
return send_from_directory('.', 'index.html')
@app.route('/api/recommendations', methods=['POST'])
def get_recommendations():
"""Main endpoint to get stock recommendations."""
try:
# Get request data
data = request.get_json()
risk_level = data.get('risk_level', 'Medium')
time_horizon = data.get('time_horizon', 'medium')
num_stocks = data.get('num_stocks', 10)
print(f"π Processing request: Risk={risk_level}, Horizon={time_horizon}, Count={num_stocks}")
# Validate inputs
if time_horizon not in models:
return jsonify({
'success': False,
'error': f'Model for {time_horizon}-term horizon not available'
}), 400
# Get the model and latest data
model = models[time_horizon]
latest_data = get_latest_data_for_prediction()
if latest_data is None or latest_data.empty:
return jsonify({
'success': False,
'error': 'No data available for predictions'
}), 500
# Prepare features for prediction
features = ['SMA_20', 'SMA_50', 'EMA_20', 'RSI', 'MACD', 'Volatility_20', 'Lag_1_Close', 'Volume']
X_latest = latest_data[features]
# Handle any missing values
X_latest = X_latest.fillna(X_latest.mean())
# Make predictions
predictions = model.predict(X_latest)
latest_data = latest_data.copy()
latest_data['Prediction'] = predictions
# Filter for buy recommendations (prediction = 1)
recommended_stocks = latest_data[latest_data['Prediction'] == 1].copy()
print(f"π― Found {len(recommended_stocks)} initial recommendations")
# Apply risk-based filtering
final_recommendations = filter_recommendations_by_risk(recommended_stocks, risk_level)
# Limit to requested number
final_recommendations = final_recommendations.head(num_stocks)
if final_recommendations.empty:
return jsonify({
'success': True,
'recommendations': [],
'message': 'No strong buy signals found in current market conditions'
})
# Format the recommendations for the frontend
recommendations_list = []
for _, stock in final_recommendations.iterrows():
recommendations_list.append({
'symbol': stock['Symbol'],
'price': float(stock['Close']),
'volatility': float(stock['Volatility_20']),
'volume': int(stock['Volume']),
'rsi': float(stock['RSI']),
'macd': float(stock['MACD'])
})
print(f"β
Returning {len(recommendations_list)} final recommendations")
return jsonify({
'success': True,
'recommendations': recommendations_list,
'parameters': {
'risk_level': risk_level,
'time_horizon': time_horizon,
'num_stocks': num_stocks
}
})
except Exception as e:
print(f"β Error in get_recommendations: {e}")
traceback.print_exc()
return jsonify({
'success': False,
'error': f'Internal server error: {str(e)}'
}), 500
@app.route('/api/chart/<symbol>', methods=['GET'])
def get_chart_data(symbol):
"""Get historical price data for charting."""
try:
if model_data is None:
return jsonify({'error': 'No data available'}), 500
# Get data for the specific symbol
stock_data = model_data[model_data['Symbol'] == symbol].copy()
if stock_data.empty:
return jsonify({'error': f'No data found for symbol {symbol}'}), 404
# Sort by date and get last ~500 days (approximately 2 years of trading days)
stock_data = stock_data.sort_values('Date').tail(500)
# Prepare chart data
chart_data = {
'symbol': symbol,
'dates': stock_data['Date'].dt.strftime('%Y-%m-%d').tolist(),
'close': stock_data['Close'].tolist(),
'sma_20': stock_data['SMA_20'].tolist(),
'sma_50': stock_data['SMA_50'].tolist(),
'volume': stock_data['Volume'].tolist()
}
return jsonify(chart_data)
except Exception as e:
print(f"β Error in get_chart_data: {e}")
traceback.print_exc()
return jsonify({'error': f'Internal server error: {str(e)}'}), 500
@app.route('/api/status', methods=['GET'])
def get_status():
"""Check server status and loaded models."""
return jsonify({
'status': 'running',
'data_loaded': model_data is not None,
'data_rows': len(model_data) if model_data is not None else 0,
'models_loaded': list(models.keys()),
'unique_stocks': model_data['Symbol'].nunique() if model_data is not None else 0
})
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors."""
return jsonify({'error': 'Endpoint not found'}), 404
@app.errorhandler(500)
def internal_error(error):
"""Handle 500 errors."""
return jsonify({'error': 'Internal server error'}), 500
if __name__ == '__main__':
print("π Starting AI Stock Advisor Backend Server...")
print("=" * 50)
# Load data and models on startup
if load_data_and_models():
print("=" * 50)
print("β
Server initialization successful!")
if model_data is not None:
print(f"π Data: {len(model_data)} rows, {model_data['Symbol'].nunique()} unique stocks")
else:
print("π Data: 0 rows, 0 unique stocks")
print(f"π€ Models: {list(models.keys())}")
print("π Frontend: http://localhost:5000")
print("π API Status: http://localhost:5000/api/status")
print("=" * 50)
# Start the Flask development server
app.run(
host='0.0.0.0',
port=5000,
debug=True,
use_reloader=False # Prevent double loading of models
)
else:
print("=" * 50)
print("β Failed to initialize server!")
print("\nPlease ensure the following files exist:")
print("- feature_engineered_data.csv")
print("- short_term_model.joblib")
print("- medium_term_model.joblib")
print("- long_term_model.joblib")
print("\nRun the model training scripts first if these files are missing.")
print("=" * 50)