-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
978 lines (775 loc) · 40.2 KB
/
server.py
File metadata and controls
978 lines (775 loc) · 40.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from typing import List
import yfinance as yf
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import load_model
from datetime import datetime, timedelta
import uvicorn
import subprocess
import sys
import matplotlib.pyplot as plt
from docx import Document
from docx.shared import Inches
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader
import io
import requests
import os
import csv
import time
import re
from dotenv import load_dotenv
from grounding_search import get_grounding_response
# Load environment variables from .env.local file
load_dotenv('.env.local')
app = FastAPI(title="Stock AI-Forecaster API", version="1.0.0")
# Enable CORS for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://localhost:3000"], # Vite dev server
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Check and run model update if needed (only if update.py exists and is executable)
try:
if os.path.exists('update.py'):
print("Checking for model updates...")
result = subprocess.run([sys.executable, 'update.py'], capture_output=True, text=True, timeout=30)
print("Update check result:")
print(result.stdout)
if result.stderr and "Model was updated recently" not in result.stderr:
print("Update errors:", result.stderr)
else:
print("update.py not found, skipping model update check")
except subprocess.TimeoutExpired:
print("Update check timed out, continuing with server startup")
except Exception as e:
print(f"Error running update check: {e}")
# Load the trained model
try:
model = load_model('lstm_stock_predictor.keras')
print("LSTM model loaded successfully")
except Exception as e:
print(f"Error loading model: {e}")
model = None
# Supported tickers (same as in the notebook)
SUPPORTED_TICKERS = ['AAPL', 'GOOGL', 'MSFT', 'AMZN', 'TSLA']
LOOK_BACK = 60 # Same as notebook
# Prediction cache file
PREDICTIONS_CSV = 'predictions_cache.csv'
def save_predictions_to_cache(ticker, forecast_data):
"""Save predictions to CSV cache"""
try:
# Prepare data for CSV
cache_data = []
for forecast in forecast_data:
if forecast.isForecast: # Only save forecast data, not historical
cache_data.append({
'ticker': ticker,
'date': forecast.date,
'price': forecast.price,
'cached_at': datetime.now().isoformat()
})
# Write to CSV
file_exists = os.path.exists(PREDICTIONS_CSV)
with open(PREDICTIONS_CSV, 'a', newline='') as csvfile:
fieldnames = ['ticker', 'date', 'price', 'cached_at']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if not file_exists:
writer.writeheader()
for row in cache_data:
writer.writerow(row)
print(f"Saved {len(cache_data)} predictions to cache for {ticker}")
except Exception as e:
print(f"Error saving predictions to cache: {e}")
def load_predictions_from_cache(ticker, current_date):
"""Load predictions from CSV cache if they exist and are recent"""
try:
if not os.path.exists(PREDICTIONS_CSV):
return None
cached_predictions = []
with open(PREDICTIONS_CSV, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['ticker'] == ticker:
cached_date = datetime.fromisoformat(row['cached_at'])
# Check if cache is from today
if cached_date.date() == current_date.date():
cached_predictions.append({
'date': row['date'],
'price': float(row['price'])
})
if len(cached_predictions) >= 7: # We expect 7 days of predictions
print(f"Loaded {len(cached_predictions)} cached predictions for {ticker}")
return cached_predictions
else:
return None
except Exception as e:
print(f"Error loading predictions from cache: {e}")
return None
class MarketDataPoint(BaseModel):
date: str
open: float
high: float
low: float
close: float
volume: int
class ForecastDataPoint(BaseModel):
date: str
price: float
isForecast: bool
class PredictionResponse(BaseModel):
history: List[MarketDataPoint]
forecast: List[ForecastDataPoint]
def fetch_historical_data(ticker: str, days: int = 180) -> pd.DataFrame:
"""Fetch historical stock data using yfinance"""
end_date = datetime.now()
start_date = end_date - timedelta(days=days)
try:
data = yf.download(ticker, start=start_date, end=end_date, auto_adjust=False)
if data.empty:
raise ValueError(f"No data available for {ticker}")
return data
except Exception as e:
raise HTTPException(status_code=404, detail=f"Could not fetch data for {ticker}: {str(e)}")
def fetch_recent_data(ticker: str, start_date: datetime, end_date: datetime) -> pd.DataFrame:
"""Fetch recent stock data that might include today or yesterday"""
try:
# Try to get data from start_date to end_date (which includes today)
data = yf.download(ticker, start=start_date, end=end_date + timedelta(days=1), auto_adjust=False)
return data
except Exception as e:
print(f"Warning: Could not fetch recent data for {ticker}: {str(e)}")
return pd.DataFrame()
def preprocess_data(close_prices: np.ndarray, scaler: MinMaxScaler) -> np.ndarray:
"""Scale data and create sequences for LSTM input"""
scaled_data = scaler.transform(close_prices.reshape(-1, 1))
X = []
for i in range(len(scaled_data) - LOOK_BACK):
X.append(scaled_data[i:(i + LOOK_BACK), 0])
X = np.array(X)
X = np.reshape(X, (X.shape[0], X.shape[1], 1))
return X
def generate_forecast(scaled_sequence: np.ndarray, scaler: MinMaxScaler, days: int = 7) -> List[float]:
"""Generate iterative forecasts using the LSTM model"""
forecast_prices = []
current_sequence = scaled_sequence.copy()
for _ in range(days):
# Predict next price
prediction = model.predict(current_sequence.reshape(1, LOOK_BACK, 1), verbose=0)
predicted_price = scaler.inverse_transform(prediction)[0][0]
forecast_prices.append(predicted_price)
# Update sequence with prediction for next iteration
# Remove first element and append prediction
current_sequence = np.roll(current_sequence, -1)
current_sequence[-1] = prediction[0][0]
return forecast_prices
def calculate_rsi(prices, period=14):
"""Calculate RSI using Wilder's smoothing method"""
if len(prices) <= period:
return None
deltas = np.diff(prices)
gains = np.where(deltas > 0, deltas, 0)
losses = np.where(deltas < 0, -deltas, 0)
# Ensure we have enough data points
if len(gains) < period:
return None
avg_gain = np.mean(gains[:period])
avg_loss = np.mean(losses[:period])
# Handle case where avg_loss is zero or NaN
if avg_loss == 0 or np.isnan(avg_loss):
return 100
for i in range(period, len(gains)):
avg_gain = (avg_gain * (period - 1) + gains[i]) / period
avg_loss = (avg_loss * (period - 1) + losses[i]) / period
if avg_loss == 0:
return 100
rs = avg_gain / avg_loss
return 100 - (100 / (1 + rs))
def calculate_bollinger_bands(prices, period=20, std_dev=2):
"""Calculate Bollinger Bands"""
if len(prices) < period:
return None, None, None
prices_array = np.array(prices[-period:])
if len(prices_array) == 0:
return None, None, None
sma = np.mean(prices_array)
std = np.std(prices_array)
# Handle NaN values
if np.isnan(sma) or np.isnan(std):
return None, None, None
upper = sma + (std * std_dev)
lower = sma - (std * std_dev)
return sma, upper, lower
def get_real_time_market_sentiment(ticker):
"""Get real-time market sentiment for a stock using Google Search grounding."""
query = f"""
As a professional business analyst, provide a structured market sentiment analysis for {ticker} stock.
Format your response as follows:
### Current Market Overview
[Brief summary of recent price action and market position]
### Key News & Developments
[3-5 most important recent news items with impact assessment]
### Analyst Sentiment
[Current analyst ratings, price targets, and consensus outlook]
### Market Reactions & Technical Factors
[Price movements, volume patterns, and technical indicators]
### Key Risks & Opportunities
[Main concerns and potential catalysts]
### Investment Outlook
[Short-term and long-term sentiment summary]
Use professional business analysis language. Be concise but comprehensive. Base analysis on recent Google Search results.
"""
sentiment = get_grounding_response(query)
return sentiment if sentiment else "Market sentiment analysis currently unavailable."
def generate_fallback_market_sentiment(ticker, historical_data, forecast_data):
"""Generate fallback market sentiment when Gemini API is unavailable"""
# Calculate required technical indicators
recent_prices = historical_data.tail(30)
current_price = float(recent_prices['Close'].iloc[-1])
first_price = float(recent_prices['Close'].iloc[0])
price_change_30d = ((current_price - first_price) / first_price) * 100
closes = recent_prices['Close'].values
if len(closes) >= 30:
closes = closes[-30:]
rsi = calculate_rsi(closes)
forecast_prices = [f.price for f in forecast_data if f.isForecast]
avg_forecast = sum(forecast_prices) / len(forecast_prices) if forecast_prices else current_price
forecast_change = ((avg_forecast - current_price) / current_price) * 100
# Determine market sentiment based on technical indicators
rsi_condition = "neutral"
if rsi and rsi > 70:
rsi_condition = "overbought"
elif rsi and rsi < 30:
rsi_condition = "oversold"
technical_sentiment = "neutral"
if rsi and rsi > 70:
technical_sentiment = "bearish"
elif rsi and rsi < 30:
technical_sentiment = "bullish"
forecast_sentiment = "sideways"
if forecast_change > 5:
forecast_sentiment = "bullish"
elif forecast_change < -5:
forecast_sentiment = "bearish"
# Pre-calculate string values for f-string compatibility
rsi_display = f"{rsi:.1f}" if rsi else "N/A"
price_momentum = "Bullish momentum detected" if forecast_change > 0 else "Bearish momentum detected" if forecast_change < 0 else "Neutral price action"
volume_patterns = "increasing participation" if forecast_change > 0 else "distribution pressure" if forecast_change < 0 else "balanced market participation"
risk_assessment = "Moderate risk environment" if abs(forecast_change) < 10 else "Elevated volatility expected"
competitive_positioning = "momentum continuation" if forecast_change > 0 else "cautionary approach"
market_signals = "monitoring for potential reversals" if rsi and rsi > 70 else "watching for buying opportunities" if rsi and rsi < 30 else "maintaining current positioning until clearer signals emerge"
# Generate comprehensive fallback analysis
return f"""
### Technical Market Intelligence (AI Analysis)
**Note:** Real-time Gemini analysis temporarily unavailable. Using AI-powered technical analysis.
#### Immediate Technical Sentiment
- **RSI Analysis:** Current RSI of {rsi_display} indicates {rsi_condition} market conditions
- **Price Momentum:** {price_momentum}
- **Forecast Direction:** {forecast_sentiment.capitalize()} bias with {abs(forecast_change):.2f}% expected movement
#### Market Structure Analysis
- **Trend Assessment:** {technical_sentiment.capitalize()} technical setup based on momentum indicators
- **Support/Resistance:** Key levels should be monitored for breakout signals
- **Volume Patterns:** Recent activity suggests {volume_patterns}
#### Institutional Perspective
- **Analyst Consensus:** Technical signals suggest {forecast_sentiment} near-term outlook
- **Risk Assessment:** {risk_assessment}
- **Catalysts:** Monitor RSI divergence and Bollinger Band interactions for trade signals
#### Sector Context
- **Relative Performance:** {ticker} showing {technical_sentiment} divergence from broader market trends
- **Competitive Positioning:** Technical setup favors {competitive_positioning}
### Integrated Market Sentiment Summary
Technical analysis reveals a {technical_sentiment} market environment with {forecast_sentiment} price projections. While real-time news analysis is temporarily unavailable, the technical indicators suggest {market_signals}. Market conditions warrant close attention to key technical levels and momentum shifts.
"""
def generate_gemini_report(ticker, historical_data, forecast_data):
"""Generate comprehensive analyst report using Gemini"""
# Get recent news (if available from yfinance)
try:
stock = yf.Ticker(ticker)
news = stock.news[:5] if stock.news else [] # Get latest 5 news items
news_text = "\n".join([f"- {item.get('title', 'No title')} ({item.get('publisher', {}).get('name', 'Unknown')})" for item in news])
except:
news_text = "News data not available"
# Get real-time market sentiment using Gemini + Google Search
real_time_sentiment = get_real_time_market_sentiment(ticker)
# Prepare data summary
recent_prices = historical_data.tail(30)
current_price = float(recent_prices['Close'].iloc[-1])
first_price = float(recent_prices['Close'].iloc[0])
price_change_30d = ((current_price - first_price) / first_price) * 100
# Calculate indicators
closes = recent_prices['Close'].values
if len(closes) >= 30: # Ensure we have enough data
closes = closes[-30:] # Take last 30 values
rsi = calculate_rsi(closes)
sma, upper_bb, lower_bb = calculate_bollinger_bands(closes)
# Forecast summary
forecast_prices = [f.price for f in forecast_data if f.isForecast]
avg_forecast = sum(forecast_prices) / len(forecast_prices) if forecast_prices else current_price
forecast_change = ((avg_forecast - current_price) / current_price) * 100
rsi_str = f"{rsi:.1f}" if rsi is not None else "N/A"
sma_str = f"{sma:.2f}" if sma is not None else "N/A"
upper_bb_str = f"{upper_bb:.2f}" if upper_bb is not None else "N/A"
lower_bb_str = f"{lower_bb:.2f}" if lower_bb is not None else "N/A"
prompt = f"""
You are a senior financial analyst at a prestigious investment firm. Generate a comprehensive, professional stock analysis report for {ticker}.
STOCK DATA SUMMARY:
- Current Price: ${current_price:.2f}
- 30-Day Price Change: {price_change_30d:.2f}%
- RSI (14): {rsi_str}
- Bollinger Bands: SMA={sma_str}, Upper={upper_bb_str}, Lower={lower_bb_str}
- Average 7-Day Forecast Price: ${avg_forecast:.2f}
- Forecast Price Change: {forecast_change:.2f}%
RECENT NEWS:
{news_text}
LSTM MODEL FORECAST:
Next 7 days predicted prices: {', '.join([f'${p:.2f}' for p in forecast_prices])}
REQUIREMENTS FOR REPORT:
Write a detailed business analyst report including:
1. EXECUTIVE SUMMARY (2-3 paragraphs)
- Current market position and recent performance
- Key technical indicators analysis
- Investment thesis summary
2. COMPANY OVERVIEW (1-2 paragraphs)
- Business description and market position
- Recent developments from news
- Competitive landscape
3. TECHNICAL ANALYSIS (2-3 paragraphs)
- Price trend analysis with support/resistance levels
- RSI and Bollinger Band interpretation
- Volume analysis if significant patterns exist
4. LSTM MODEL ANALYSIS (2 paragraphs)
- Explanation of the forecasting methodology
- Interpretation of the 7-day forecast
- Model confidence and limitations
5. MARKET SENTIMENT & NEWS ANALYSIS (1-2 paragraphs)
- Impact of recent news on stock price
- Market sentiment indicators
- External factors affecting the stock
- Give me the most important points only based on the recent news and market sentiment analysis and search it on google (compulsory)
- How does the market sentiment align with technical indicators and LSTM forecast?
- What are the key risks and opportunities highlighted by recent developments?
- Summarize institutional investor behavior and analyst opinions
6. INVESTMENT RECOMMENDATION (1 paragraph)
- Clear buy/hold/sell recommendation with timeframe
- Risk assessment (High/Medium/Low)
- Key catalysts and concerns
7. PRICE TARGETS & TIMELINE
- Short-term (1-3 months) price targets
- Long-term (6-12 months) outlook
STYLE GUIDELINES:
- Professional, objective tone like a Wall Street analyst
- Cite specific data points and news items
- Use industry terminology appropriately
- Be comprehensive but concise
- Include realistic market context and potential scenarios
- Structure with clear headings and subheadings
The report should be suitable for institutional investors and financial advisors.
"""
# Generate comprehensive analyst report
rsi_display = f"{rsi:.1f}" if rsi is not None else "N/A"
rsi_condition = "overbought" if rsi and rsi > 70 else "oversold" if rsi and rsi < 30 else "neutral"
rsi_signal = "bearish" if rsi and rsi > 70 else "bullish" if rsi and rsi < 30 else "neutral"
bb_position = "above upper band (resistance)" if upper_bb and current_price > upper_bb else "below lower band (support)" if lower_bb and current_price < lower_bb else "within bands (consolidation)"
forecast_direction = "upward" if forecast_change > 0 else "downward"
forecast_strength = "strong" if abs(forecast_change) > 15 else "moderate" if abs(forecast_change) > 5 else "weak"
recommendation = "BUY" if forecast_change > 5 else "HOLD" if forecast_change > -5 else "SELL"
risk_level = "Low" if abs(forecast_change) < 10 else "Medium" if abs(forecast_change) < 20 else "High"
# Company info based on ticker
company_info = {
'AAPL': {'name': 'Apple Inc.', 'sector': 'Technology', 'industry': 'Consumer Electronics'},
'GOOGL': {'name': 'Alphabet Inc.', 'sector': 'Technology', 'industry': 'Internet Services'},
'MSFT': {'name': 'Microsoft Corporation', 'sector': 'Technology', 'industry': 'Software'},
'AMZN': {'name': 'Amazon.com Inc.', 'sector': 'Consumer Discretionary', 'industry': 'E-commerce'},
'TSLA': {'name': 'Tesla Inc.', 'sector': 'Consumer Discretionary', 'industry': 'Electric Vehicles'}
}
company = company_info.get(ticker, {'name': ticker, 'sector': 'Unknown', 'industry': 'Unknown'})
return f"""
# {company['name']} ({ticker}) - Comprehensive Stock Analysis Report
**Report Generated:** {datetime.now().strftime('%B %d, %Y at %H:%M UTC')}
**Analysis Period:** Last 180 trading days
**Reporting Analyst:** AI-Powered Financial Analysis System
## Executive Summary
{company['name']} ({ticker}) closed at ${current_price:.2f} per share, representing a {price_change_30d:+.2f}% change over the past 30 trading days. The stock is currently positioned within the {company['sector']} sector and {company['industry']} industry.
Our LSTM-based forecasting model projects an average price of ${avg_forecast:.2f} over the next 7 trading days, indicating a {forecast_strength} {forecast_direction} momentum with a {forecast_change:+.2f}% expected price movement. Technical indicators show {rsi_condition} momentum (RSI: {rsi_display}), with the stock currently trading {bb_position}.
**Investment Thesis:** {'We maintain a positive outlook for near-term price appreciation driven by AI model confidence.' if forecast_change > 5 else 'We recommend a cautious approach with close monitoring of technical levels.' if forecast_change > -5 else 'We advise reducing exposure given the bearish technical setup and model projections.'}
## Company Overview
{company['name']} is a leading player in the {company['industry']} space within the broader {company['sector']} sector. The company has demonstrated consistent growth through technological innovation and market expansion.
Recent market performance shows {price_change_30d:+.2f}% growth over the past month, {'outperforming' if price_change_30d > 2 else 'underperforming' if price_change_30d < -2 else 'tracking'} broader market indices. The company's market capitalization and trading volume reflect its significant position in the investment landscape.
## Technical Analysis
### Price Action & Trend Analysis
The stock has shown {'bullish' if price_change_30d > 0 else 'bearish'} momentum over the recent 30-day period, with price action {'breaking above key resistance levels' if current_price > (first_price * 1.05) else 'finding support near previous lows' if current_price < (first_price * 0.95) else 'consolidating within a defined range'}. Current trading range: ${float(min(closes[-30:])):.2f} - ${float(max(closes[-30:])):.2f}.
### Momentum Indicators
- **RSI (14-period):** {rsi_display} - {rsi_condition.capitalize()} conditions suggest {rsi_signal} short-term momentum
- **Bollinger Bands:** {'Price is testing the upper resistance level' if upper_bb and current_price > upper_bb * 0.98 else 'Price is approaching support levels' if lower_bb and current_price < lower_bb * 1.02 else 'Price is trading within the consolidation zone'}
### Volume Analysis
Recent trading volume patterns {'show increasing conviction' if forecast_change > 0 else 'indicate distribution pressure' if forecast_change < 0 else 'remain neutral with average participation'}. Volume analysis supports the {'bullish' if forecast_change > 0 else 'bearish' if forecast_change < 0 else 'sideways'} bias indicated by our technical models.
## LSTM Model Analysis
### Forecasting Methodology
Our proprietary LSTM (Long Short-Term Memory) neural network model utilizes 60 days of historical price data to generate 7-day forward projections. The model has been trained on extensive historical datasets and incorporates multiple technical indicators for enhanced predictive accuracy.
### Current Forecast Interpretation
The model projects {forecast_direction} movement with {forecast_strength} conviction, forecasting an average price of ${avg_forecast:.2f} by {datetime.now() + timedelta(days=7):%B %d, %Y}. This represents a {forecast_change:+.2f}% expected change from current levels.
**Model Confidence Factors:**
- Historical accuracy: {'High - Model showing consistent performance' if abs(forecast_change) > 10 else 'Medium - Moderate confidence in projection' if abs(forecast_change) > 5 else 'Low - Weak signal strength'}
- Market conditions: {'Favorable for continuation' if rsi_condition == 'neutral' else 'Caution advised due to extreme readings'}
- Risk assessment: {risk_level} based on projected volatility
### Model Limitations
LSTM models perform best in trending markets and may experience reduced accuracy during periods of high volatility or significant news events. Current projection assumes continuation of existing technical conditions.
## Market Sentiment & News Analysis
### Real-Time Market Intelligence
{real_time_sentiment}
### Technical Sentiment Indicators
Recent market sentiment appears {'positive' if forecast_change > 5 else 'cautious' if forecast_change > -5 else 'negative'} based on technical indicators and AI model projections. The {rsi_signal} RSI reading suggests {'investor enthusiasm may be peaking' if rsi and rsi > 70 else 'potential buying opportunity developing' if rsi and rsi < 30 else 'balanced market participation'}.
External market factors including interest rate expectations and sector rotation patterns should be monitored closely, as they may impact the projected price trajectory.
## Investment Recommendation
**Primary Recommendation: {recommendation}**
**Timeframe:** 1-3 months
**Risk Level:** {risk_level}
**Rationale:**
- Technical setup shows {rsi_condition} momentum with {bb_position}
- AI model projects {forecast_strength} {forecast_direction} movement
- Current positioning suggests {'favorable entry for long positions' if recommendation == 'BUY' else 'reduced exposure recommended' if recommendation == 'SELL' else 'maintain current allocation'}
**Key Catalysts:**
- {'Positive: Model confidence supports upside potential' if forecast_change > 5 else 'Negative: Technical resistance may limit upside' if forecast_change < -5 else 'Neutral: Await clearer directional signals'}
- Monitor RSI for overbought/oversold conditions
- Watch Bollinger Band interactions for breakout signals
**Key Concerns:**
- {'Volatility may increase on overbought conditions' if rsi and rsi > 70 else 'Further downside possible if support breaks' if rsi and rsi < 30 else 'Limited directional conviction'}
- Model projections assume current market conditions persist
- External macroeconomic factors may override technical signals
## Price Targets & Timeline
### Short-Term Targets (1-3 months)
- **Bullish Case:** ${current_price * 1.15:.2f} (+15% from current levels)
- **Base Case:** ${avg_forecast:.2f} ({forecast_change:+.2f}% from current levels)
- **Bearish Case:** ${current_price * 0.85:.2f} (-15% from current levels)
### Long-Term Outlook (6-12 months)
The long-term trajectory will depend on successful execution of {company['name']}'s strategic initiatives and broader market conditions. Current technical setup suggests {'potential for trend continuation' if forecast_change > 0 else 'risk of further deterioration' if forecast_change < 0 else 'consolidation until clearer catalysts emerge'}.
**Critical Support Levels:** ${lower_bb:.2f} (Bollinger Lower), ${current_price * 0.95:.2f} (5% below current)
**Key Resistance Levels:** ${upper_bb:.2f} (Bollinger Upper), ${current_price * 1.05:.2f} (5% above current)
---
*This report is for informational purposes only and should not be considered as investment advice. Always consult with a qualified financial advisor before making investment decisions. Past performance does not guarantee future results.*
"""
def create_price_chart(data, forecast_data, ticker):
"""Create price chart with forecast"""
plt.figure(figsize=(12, 6))
# Historical data
dates = [datetime.strptime(d['date'], '%Y-%m-%d') for d in data]
closes = [d['close'] for d in data]
plt.plot(dates, closes, label='Historical Price', color='blue')
# Forecast data
forecast_dates = [datetime.strptime(f.date, '%Y-%m-%d') for f in forecast_data if f.isForecast]
forecast_prices = [f.price for f in forecast_data if f.isForecast]
if forecast_dates and forecast_prices:
plt.plot(forecast_dates, forecast_prices, label='LSTM Forecast', color='red', linestyle='--')
plt.title(f'{ticker} Stock Price & Forecast')
plt.xlabel('Date')
plt.ylabel('Price ($)')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.subplots_adjust(right=0.8) # Make room for legend
# Save to bytes
buf = io.BytesIO()
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
buf.seek(0)
plt.close()
return buf
def create_technical_chart(data, ticker):
"""Create technical indicators chart"""
plt.figure(figsize=(12, 8))
# Price data
dates = [datetime.strptime(d['date'], '%Y-%m-%d') for d in data]
closes = [d['close'] for d in data]
volumes = [d['volume'] for d in data]
# Subplot 1: Price
plt.subplot(2, 1, 1)
plt.plot(dates, closes, color='blue', label='Close Price')
plt.title(f'{ticker} Technical Analysis')
plt.ylabel('Price ($)')
plt.legend()
plt.grid(True, alpha=0.3)
# Subplot 2: Volume
plt.subplot(2, 1, 2)
plt.bar(dates, volumes, color='green', alpha=0.7, label='Volume')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.legend()
plt.grid(True, alpha=0.3)
plt.tight_layout()
# Save to bytes
buf = io.BytesIO()
plt.savefig(buf, format='png', dpi=150, bbox_inches='tight')
buf.seek(0)
plt.close()
return buf
def generate_word_report(ticker, report_text, price_chart_buf, technical_chart_buf):
"""Generate Word document with charts and Markdown parsing"""
doc = Document()
# Title
title = doc.add_heading(f'{ticker} Stock Analysis Report', 0)
title.alignment = 1 # Center alignment
doc.add_heading('Generated on ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 2)
def add_markdown_paragraph(document, text, style=None):
"""Helper to parse **bold** text within a paragraph"""
if style:
p = document.add_paragraph(style=style)
else:
p = document.add_paragraph()
# Split by ** markers
# Example: "This is **bold** text" -> ["This is ", "bold", " text"]
parts = re.split(r'\*\*(.*?)\*\*', text)
for i, part in enumerate(parts):
if not part: continue
run = p.add_run(part)
# Even indices (0, 2, 4) are normal, Odd indices (1, 3) are bold matches
if i % 2 == 1:
run.bold = True
# Parse the report text line by line
lines = report_text.split('\n')
for line in lines:
line = line.strip()
if not line:
continue
# Handle Headers
if line.startswith('# '):
doc.add_heading(line[2:], 1)
elif line.startswith('## '):
doc.add_heading(line[3:], 2)
elif line.startswith('### '):
doc.add_heading(line[4:], 3)
# Handle Bullet Points - Enhanced detection for various formats
elif (line.startswith('- ') or line.startswith('* ') or
'•' in line or '◦' in line or
(len(line.strip()) > 0 and line.strip()[0] in ['•', '◦', '-', '*'])):
# Clean bullet markers more robustly - handle various Unicode bullet characters
import unicodedata
clean_line = line
# Remove common bullet characters and their variations
bullet_chars = ['•', '◦', '▪', '▫', '●', '○', '■', '□', '-', '*']
for bullet in bullet_chars:
clean_line = clean_line.replace(bullet, '')
# Also remove leading/trailing whitespace and tabs
clean_line = clean_line.strip()
if clean_line: # Only add if there's content after cleaning
add_markdown_paragraph(doc, clean_line, style='List Bullet')
# Handle Standard Paragraphs (with potential inline bolding)
else:
add_markdown_paragraph(doc, line)
# Add Charts
doc.add_page_break()
doc.add_heading('Price Chart & Forecast', 2)
price_chart_buf.seek(0)
doc.add_picture(price_chart_buf, width=Inches(6))
doc.add_heading('Technical Analysis', 2)
technical_chart_buf.seek(0)
doc.add_picture(technical_chart_buf, width=Inches(6))
# Save to bytes
buf = io.BytesIO()
doc.save(buf)
buf.seek(0)
return buf
def generate_pdf_report(ticker, report_text, price_chart_buf, technical_chart_buf):
"""Generate PDF document with charts"""
buf = io.BytesIO()
c = canvas.Canvas(buf, pagesize=letter)
width, height = letter
# Title
c.setFont("Helvetica-Bold", 16)
c.drawString(50, height - 50, f'{ticker} Stock Analysis Report')
c.setFont("Helvetica", 10)
c.drawString(50, height - 70, f'Generated on {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
y_position = height - 100
# Add report text (simplified)
c.setFont("Helvetica", 10)
for line in report_text.split('\n')[:20]: # Limit lines for PDF
if y_position < 100:
c.showPage()
y_position = height - 50
if line.strip() and not line.startswith('#'):
c.drawString(50, y_position, line[:80]) # Truncate long lines
y_position -= 15
# Add charts on new pages
c.showPage()
# Price chart
price_chart_buf.seek(0)
img = ImageReader(price_chart_buf)
c.drawImage(img, 50, height - 400, width=500, height=300)
c.showPage()
# Technical chart
technical_chart_buf.seek(0)
img = ImageReader(technical_chart_buf)
c.drawImage(img, 50, height - 400, width=500, height=300)
c.save()
buf.seek(0)
return buf
@app.get("/predict")
async def predict_stock(ticker: str) -> PredictionResponse:
if ticker not in SUPPORTED_TICKERS:
raise HTTPException(status_code=400, detail=f"Ticker {ticker} not supported. Supported: {SUPPORTED_TICKERS}")
if model is None:
raise HTTPException(status_code=500, detail="Model not loaded")
try:
# Fetch historical data (180 days back)
data = fetch_historical_data(ticker, days=180)
# Check for recent real data (today and yesterday)
now = datetime.now()
yesterday = now - timedelta(days=1)
# Try to fetch recent data including today
recent_data = fetch_recent_data(ticker, data.index[-1], now)
# Combine historical and recent data, preferring real data over predictions
combined_data = data.copy()
# Add any recent real data that we don't already have
real_recent_dates = []
if not recent_data.empty:
for date_idx in recent_data.index:
date_str = date_idx.strftime('%Y-%m-%d')
if date_str not in [idx.strftime('%Y-%m-%d') for idx in combined_data.index]:
# This is new real data, add it
combined_data.loc[date_idx] = recent_data.loc[date_idx]
real_recent_dates.append(date_idx)
# Sort by date
combined_data = combined_data.sort_index()
# Prepare historical data response - now includes real recent data
history = []
for index, row in combined_data.iterrows():
history.append(MarketDataPoint(
date=index.strftime('%Y-%m-%d'),
open=round(float(row['Open']), 2),
high=round(float(row['High']), 2),
low=round(float(row['Low']), 2),
close=round(float(row['Close']), 2),
volume=int(row['Volume'])
))
# Determine how many days we need to forecast
# Only forecast for dates that don't have real data
last_real_date = combined_data.index[-1]
days_to_forecast = 7
# If we have data up to today, we might need fewer predictions
# But we'll still generate 7 days of predictions starting from the day after last real data
forecast_start_date = last_real_date + timedelta(days=1)
# Check for cached predictions first
current_date = datetime.now()
cached_predictions = load_predictions_from_cache(ticker, current_date)
forecast = []
forecast_prices = []
if cached_predictions:
print(f"Using cached predictions for {ticker}")
# Use cached predictions
forecast_prices = [p['price'] for p in cached_predictions]
else:
print(f"Generating new predictions for {ticker}")
# Extract closing prices for prediction from the combined real data
close_prices = combined_data['Close'].values.reshape(-1, 1)
# Create and fit scaler on all available real data
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.fit(close_prices)
# Create input sequence from last LOOK_BACK days of real data
last_sequence = close_prices[-LOOK_BACK:].reshape(-1, 1)
scaled_sequence = scaler.transform(last_sequence).flatten()
# Generate forecast
forecast_prices = generate_forecast(scaled_sequence, scaler, days=days_to_forecast)
# Create forecast response
# Add anchor point (last real data point, not forecast)
last_real = history[-1]
forecast.append(ForecastDataPoint(
date=last_real.date,
price=last_real.close,
isForecast=False
))
# Add forecast points starting from the day after last real data
current_date = datetime.strptime(last_real.date, '%Y-%m-%d')
forecast_points = []
for i, price in enumerate(forecast_prices):
current_date += timedelta(days=1)
forecast_point = ForecastDataPoint(
date=current_date.strftime('%Y-%m-%d'),
price=round(price, 2),
isForecast=True
)
forecast.append(forecast_point)
forecast_points.append(forecast_point)
# Save predictions to cache if they were newly generated
if not cached_predictions and forecast_points:
save_predictions_to_cache(ticker, forecast_points)
return PredictionResponse(history=history, forecast=forecast)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
@app.get("/report/{ticker}")
async def generate_stock_report(ticker: str, format: str = "word"):
"""
Generate comprehensive stock analysis report with charts
format: "word" or "pdf"
"""
if ticker not in SUPPORTED_TICKERS:
raise HTTPException(status_code=400, detail=f"Ticker {ticker} not supported. Supported: {SUPPORTED_TICKERS}")
if model is None:
raise HTTPException(status_code=500, detail="Model not loaded")
try:
# Fetch historical data
data = fetch_historical_data(ticker, days=180)
# Prepare data for analysis
history = []
for index, row in data.iterrows():
history.append({
'date': index.strftime('%Y-%m-%d'),
'open': round(float(row['Open']), 2),
'high': round(float(row['High']), 2),
'low': round(float(row['Low']), 2),
'close': round(float(row['Close']), 2),
'volume': int(row['Volume'])
})
# Generate forecast
close_prices = data['Close'].values.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.fit(close_prices)
last_sequence = close_prices[-LOOK_BACK:].reshape(-1, 1)
scaled_sequence = scaler.transform(last_sequence).flatten()
forecast_prices = generate_forecast(scaled_sequence, scaler, days=7)
forecast_data = []
last_historical = history[-1]
forecast_data.append(ForecastDataPoint(
date=last_historical['date'],
price=last_historical['close'],
isForecast=False
))
current_date = datetime.strptime(last_historical['date'], '%Y-%m-%d')
for i, price in enumerate(forecast_prices):
current_date += timedelta(days=1)
forecast_data.append(ForecastDataPoint(
date=current_date.strftime('%Y-%m-%d'),
price=round(price, 2),
isForecast=True
))
# Generate Gemini report
report_text = generate_gemini_report(ticker, data, forecast_data)
# Create charts
price_chart_buf = create_price_chart(history, forecast_data, ticker)
technical_chart_buf = create_technical_chart(history, ticker)
# Generate document based on format
if format.lower() == "pdf":
doc_buf = generate_pdf_report(ticker, report_text, price_chart_buf, technical_chart_buf)
filename = f"{ticker}_report.pdf"
media_type = "application/pdf"
else: # word
doc_buf = generate_word_report(ticker, report_text, price_chart_buf, technical_chart_buf)
filename = f"{ticker}_report.docx"
media_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
return StreamingResponse(
io.BytesIO(doc_buf.getvalue()),
media_type=media_type,
headers={"Content-Disposition": f"attachment; filename={filename}"}
)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Report generation failed: {str(e)}")
@app.get("/health")
async def health_check():
return {"status": "healthy", "model_loaded": model is not None}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)