-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpt4o_stock_comparator.py
More file actions
1407 lines (1289 loc) · 62.8 KB
/
gpt4o_stock_comparator.py
File metadata and controls
1407 lines (1289 loc) · 62.8 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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
sys.stderr = open(os.devnull, 'w')
import yfinance as yf
from dotenv import load_dotenv
from openai import OpenAI
from datetime import datetime
import platform
import subprocess
import re
import json
import argparse
import pandas as pd
import matplotlib.pyplot as plt
import openpyxl
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm
from colorama import init, Fore, Style
import base64
from weasyprint import HTML
import time
import signal
import threading
import warnings
import traceback
import contextlib
import feedparser
from collections import Counter
from typing import List, Dict, Any
import pytest
from analysis import StockAnalyzer
# After all imports and initializations, restore stderr
# (so Python errors are visible during runtime)
sys.stderr = sys.__stderr__
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
raise ValueError("❌ OPENAI_API_KEY not found in .env file.")
client = OpenAI(api_key=api_key)
SECTOR_ETFS = {
'Technology': 'XLK',
'Financial Services': 'XLF',
'Healthcare': 'XLV',
'Consumer Cyclical': 'XLY',
'Consumer Defensive': 'XLP',
'Industrials': 'XLI',
'Energy': 'XLE',
'Utilities': 'XLU',
'Materials': 'XLB',
'Real Estate': 'XLRE',
'Communication Services': 'XLC',
}
# In-memory cache for yfinance Ticker objects and their history
_ticker_cache = {}
_history_cache = {}
@contextlib.contextmanager
def suppress_stderr():
with open(os.devnull, 'w') as devnull:
old_stderr = sys.stderr
sys.stderr = devnull
try:
yield
finally:
sys.stderr = old_stderr
def get_ticker_cached(ticker):
try:
with suppress_stderr():
if ticker not in _ticker_cache:
_ticker_cache[ticker] = yf.Ticker(ticker)
return _ticker_cache[ticker]
except Exception as e:
print(Fore.RED + f"[Error] Could not cache ticker {ticker}: {e}" + Style.RESET_ALL)
return None
def get_history_cached(ticker, period="max", show_progress=False, retries=2):
key = (ticker, period)
if key not in _history_cache:
for attempt in range(retries+1):
try:
with suppress_stderr():
if show_progress:
tqdm.write(f"Fetching data for {ticker} ({period})...")
_history_cache[key] = get_ticker_cached(ticker).history(period=period)
break
except Exception as e:
if attempt < retries:
print(Fore.YELLOW + f"[Retry {attempt+1}] Error fetching history for {ticker}: {e}" + Style.RESET_ALL)
time.sleep(1)
else:
print(Fore.RED + f"[Error] Could not fetch history for {ticker}: {e}" + Style.RESET_ALL)
_history_cache[key] = pd.DataFrame()
return _history_cache[key]
def get_sector_etf(sector):
return SECTOR_ETFS.get(sector, None)
def get_stock_data(ticker, retries=2):
for attempt in range(retries+1):
try:
ticker_obj = get_ticker_cached(ticker)
if not ticker_obj:
return None
with suppress_stderr():
info = ticker_obj.info
if not info or "longName" not in info:
print(Fore.YELLOW + f"⚠️ {ticker} returned no valid financial data." + Style.RESET_ALL)
return None
return {
"name": info.get("longName", "N/A"),
"sector": info.get("sector", "N/A"),
"market_cap": info.get("marketCap", "N/A"),
"price": info.get("currentPrice", "N/A"),
"pe_ratio": info.get("trailingPE", "N/A"),
"beta": info.get("beta", "N/A"),
"dividend_yield": info.get("dividendYield", "N/A"),
}
except Exception as e:
if attempt < retries:
print(Fore.YELLOW + f"[Retry {attempt+1}] Error fetching data for {ticker}: {e}" + Style.RESET_ALL)
time.sleep(1)
else:
print(Fore.RED + f"[Error] Could not fetch data for {ticker}: {e}" + Style.RESET_ALL)
return None
def get_top_stock_picks(profile=None, prefer_sectors=None, exclude_sectors=None, risk_tolerance=None):
profile_text = f"for a {profile} investor" if profile else ""
prefer_text = f" Prefer sectors: {', '.join(prefer_sectors)}." if prefer_sectors else ""
exclude_text = f" Exclude sectors: {', '.join(exclude_sectors)}." if exclude_sectors else ""
risk_text = f" Risk tolerance: {risk_tolerance}." if risk_tolerance else ""
prompt = (
f"Based on recent market conditions, provide a diversified set of investment recommendations {profile_text}. "
f"Consider the following criteria:\n"
f"1. Include a mix of:\n"
f" - Large, mid, and small-cap stocks\n"
f" - ETFs (index, sector, thematic)\n"
f" - Options strategies (if risk tolerance allows)\n"
f" - Futures contracts (if risk tolerance allows)\n"
f" - Crypto assets (if risk tolerance allows)\n"
f" - Fixed income (bonds, preferred shares)\n"
f"2. Diversification requirements:\n"
f" - Across different sectors and industries\n"
f" - Different market caps\n"
f" - Various investment vehicles\n"
f"3. Risk considerations:\n"
f" - Match recommendations to the specified risk tolerance\n"
f" - Include defensive positions if risk tolerance is low\n"
f" - Consider market conditions and volatility\n"
f"4. Current market context:\n"
f" - Recent market trends\n"
f" - Economic indicators\n"
f" - Sector rotations\n"
f"5. Specific preferences:\n"
f"{prefer_text}\n"
f"{exclude_text}\n"
f"{risk_text}\n\n"
f"For each asset class or category (Large-Cap Stocks, Mid-Cap Stocks, Small-Cap Stocks, ETFs, Options Strategies, Futures Contracts, Crypto Assets, Fixed Income), provide at least three distinct recommendations. For each, include:\n"
f"1. Symbol and name\n"
f"2. Asset type (stock/ETF/option/future/crypto/bond)\n"
f"3. Brief rationale\n"
f"4. Risk level\n"
f"5. Time horizon\n"
f"6. Key metrics or considerations\n\n"
f"Format the response in clear sections with bullet points for easy reading."
)
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a comprehensive market strategist and financial analyst with expertise in all asset classes."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=1000
)
return response.choices[0].message.content
except Exception as e:
print(f"[ERROR] OpenAI API call failed: {e}")
traceback.print_exc()
return "Error: Could not fetch top picks."
def generate_comparison(ticker1, data1, ticker2, data2):
prompt = f"""
Compare the following two companies based on these financial metrics:
{ticker1}:
- Name: {data1['name']}
- Sector: {data1['sector']}
- Market Cap: {data1['market_cap']}
- Price: {data1['price']}
- P/E Ratio: {data1['pe_ratio']}
- Beta: {data1['beta']}
- Dividend Yield: {data1['dividend_yield']}
{ticker2}:
- Name: {data2['name']}
- Sector: {data2['sector']}
- Market Cap: {data2['market_cap']}
- Price: {data2['price']}
- P/E Ratio: {data2['pe_ratio']}
- Beta: {data2['beta']}
- Dividend Yield: {data2['dividend_yield']}
Provide an objective comparison of their valuation, growth potential, and risk profile. Keep the analysis under 200 words.
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a financial analyst."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=350
)
return response.choices[0].message.content
def summarize_sentiment(comparison):
followup = (
"Please provide a one-sentence summary for the following investment comparison. "
"Focus on sentiment and clarity for a non-technical investor audience:\n\n"
+ comparison
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You write simplified summaries for investors."},
{"role": "user", "content": followup}
],
temperature=0.6,
max_tokens=100
)
return response.choices[0].message.content.strip()
def extract_tickers_from_text(text):
# Extract possible tickers (2-5 uppercase letters)
candidates = re.findall(r"\b[A-Z]{2,5}\b", text)
# Validate each candidate using get_stock_data
valid_tickers = []
for t in candidates:
if get_stock_data(t):
valid_tickers.append(t)
return valid_tickers
def get_backtest_data(ticker1, ticker2):
t1_hist = get_history_cached(ticker1, period="6mo")
t2_hist = get_history_cached(ticker2, period="6mo")
def calc_return(hist, ticker):
if hist is not None and len(hist) > 1:
close_prices = hist["Close"].dropna()
if close_prices.empty or len(close_prices) < 2:
print(Fore.YELLOW + f"⚠️ {ticker}: Not enough valid price data for backtest period." + Style.RESET_ALL)
return "N/A", None, None
start_price = close_prices.iloc[0]
end_price = close_prices.iloc[-1]
start_date = close_prices.index[0]
end_date = close_prices.index[-1]
ret = round(((end_price - start_price) / start_price) * 100, 2)
return ret, start_date, end_date
else:
print(Fore.YELLOW + f"⚠️ {ticker}: Not enough data for backtest period." + Style.RESET_ALL)
return "N/A", None, None
t1_return, t1_start, t1_end = calc_return(t1_hist, ticker1)
t2_return, t2_start, t2_end = calc_return(t2_hist, ticker2)
# Print the actual dates used
print(Fore.CYAN + f"\nBacktest period for {ticker1}: {t1_start.date() if t1_start else 'N/A'} to {t1_end.date() if t1_end else 'N/A'}" + Style.RESET_ALL)
print(Fore.CYAN + f"Backtest period for {ticker2}: {t2_start.date() if t2_start else 'N/A'} to {t2_end.date() if t2_end else 'N/A'}" + Style.RESET_ALL)
return t1_return, t2_return
def get_period_returns_and_volatility(ticker, periods, custom_start=None, custom_end=None):
hist = get_history_cached(ticker, period="max")
hist.index = hist.index.tz_localize(None) # Make index timezone-naive
hist = hist[hist.index <= pd.Timestamp.today()]
results = {}
for label, days in periods.items():
if days == 'YTD':
start = pd.Timestamp(datetime.now().year, 1, 1)
else:
start = pd.Timestamp.today() - pd.Timedelta(days=days)
start = pd.to_datetime(start).tz_localize(None) # Ensure timezone-naive
period_hist = hist[hist.index >= start]
if len(period_hist) < 2:
results[label] = {'return': None, 'volatility': None, 'drawdown': None}
continue
ret = (period_hist['Close'].iloc[-1] - period_hist['Close'].iloc[0]) / period_hist['Close'].iloc[0] * 100
vol = period_hist['Close'].pct_change().rolling(window=21).std().mean() * (252 ** 0.5) * 100
# Max drawdown
roll_max = period_hist['Close'].cummax()
drawdown = ((period_hist['Close'] - roll_max) / roll_max).min() * 100
results[label] = {'return': round(ret, 2), 'volatility': round(vol, 2), 'drawdown': round(drawdown, 2)}
return results
def get_earnings_dates(ticker, start=None, end=None):
try:
cal = get_ticker_cached(ticker).earnings_dates
if start:
cal = cal[cal.index >= pd.to_datetime(start)]
if end:
cal = cal[cal.index <= pd.to_datetime(end)]
return cal.index.to_pydatetime().tolist()
except Exception:
return []
def plot_price_with_earnings(ticker1, ticker2, start=None, end=None, sector1=None, sector2=None, show_progress=True):
# Parallel fetch for all histories with progress bar
tickers_needed = [ticker1, ticker2, 'SPY']
if sector1 and sector1 == sector2:
etf = get_sector_etf(sector1)
if etf:
tickers_needed.append(etf)
if show_progress:
for t in tqdm(tickers_needed, desc="Fetching price data"):
get_history_cached(t, period="max", show_progress=False)
else:
with ThreadPoolExecutor() as executor:
for t in tickers_needed:
executor.submit(get_history_cached, t, period="max")
hist1 = get_history_cached(ticker1, period="max")
hist2 = get_history_cached(ticker2, period="max")
hist1.index = hist1.index.tz_localize(None)
hist2.index = hist2.index.tz_localize(None)
if start:
start = pd.to_datetime(start).tz_localize(None)
hist1 = hist1[hist1.index >= start]
hist2 = hist2[hist2.index >= start]
if end:
end = pd.to_datetime(end).tz_localize(None)
hist1 = hist1[hist1.index <= end]
hist2 = hist2[hist2.index <= end]
plt.figure(figsize=(14, 7))
plt.plot(hist1.index, hist1['Close'], label=f'{ticker1} Close', linewidth=2)
plt.plot(hist2.index, hist2['Close'], label=f'{ticker2} Close', linewidth=2)
# Moving averages for ticker1
if len(hist1) >= 50:
plt.plot(hist1.index, hist1['Close'].rolling(window=50).mean(), label=f'{ticker1} 50d MA', color='blue', linestyle='-.', linewidth=1)
if len(hist1) >= 200:
plt.plot(hist1.index, hist1['Close'].rolling(window=200).mean(), label=f'{ticker1} 200d MA', color='blue', linestyle=':', linewidth=1)
# Moving averages for ticker2
if len(hist2) >= 50:
plt.plot(hist2.index, hist2['Close'].rolling(window=50).mean(), label=f'{ticker2} 50d MA', color='orange', linestyle='-.', linewidth=1)
if len(hist2) >= 200:
plt.plot(hist2.index, hist2['Close'].rolling(window=200).mean(), label=f'{ticker2} 200d MA', color='orange', linestyle=':', linewidth=1)
# Sector ETF overlay (if both stocks are in the same sector)
etf = None
if sector1 and sector1 == sector2:
etf = get_sector_etf(sector1)
if etf:
hist_etf = get_history_cached(etf, period="max")
hist_etf.index = hist_etf.index.tz_localize(None)
if start:
hist_etf = hist_etf[hist_etf.index >= start]
if end:
hist_etf = hist_etf[hist_etf.index <= end]
plt.plot(hist_etf.index, hist_etf['Close'], label=f'{etf} (Sector ETF)', color='green', linestyle=':')
# SPY overlay
hist_spy = get_history_cached('SPY', period="max")
hist_spy.index = hist_spy.index.tz_localize(None)
if start:
hist_spy = hist_spy[hist_spy.index >= start]
if end:
hist_spy = hist_spy[hist_spy.index <= end]
plt.plot(hist_spy.index, hist_spy['Close'], label='SPY (S&P 500)', color='black', linestyle='--', linewidth=1.5)
# Earnings dates
earnings1 = get_earnings_dates(ticker1, start, end)
earnings2 = get_earnings_dates(ticker2, start, end)
for edate in earnings1:
plt.axvline(edate, color='blue', linestyle='--', alpha=0.3, label=f'{ticker1} Earnings' if edate == earnings1[0] else "")
for edate in earnings2:
plt.axvline(edate, color='orange', linestyle='--', alpha=0.3, label=f'{ticker2} Earnings' if edate == earnings2[0] else "")
# Annotate most recent earnings
if earnings1:
plt.annotate(f'{ticker1} Last Earnings', xy=(earnings1[-1], hist1['Close'].loc[earnings1[-1]] if earnings1[-1] in hist1.index else hist1['Close'].iloc[-1]),
xytext=(0, 20), textcoords='offset points', arrowprops=dict(arrowstyle='->'), color='blue')
if earnings2:
plt.annotate(f'{ticker2} Last Earnings', xy=(earnings2[-1], hist2['Close'].loc[earnings2[-1]] if earnings2[-1] in hist2.index else hist2['Close'].iloc[-1]),
xytext=(0, -30), textcoords='offset points', arrowprops=dict(arrowstyle='->'), color='orange')
plt.legend()
plt.title(f'{ticker1} vs {ticker2} Price History with Earnings Dates' + (f' and {etf}' if etf else ''))
plt.xlabel('Date')
plt.ylabel('Close Price (USD)')
plt.grid(True, linestyle='--', alpha=0.5)
plt.tight_layout()
chart_file = f"{ticker1}_{ticker2}_price_chart.png"
plt.savefig(chart_file)
plt.close()
return chart_file
def export_html_to_pdf(html_file, pdf_file):
HTML(html_file).write_pdf(pdf_file)
def save_outputs(ticker1, data1, ticker2, data2, top_picks, comparison, summary, backtest, timestamp):
md_file = f"gpt4o_stock_comparison_{timestamp}.md"
json_file = f"gpt4o_stock_data_{timestamp}.json"
header = f"===== GPT-4o Stock Comparison =====\nGenerated on: {timestamp}\nCompared: {ticker1} vs {ticker2}\n"
backtest_note = f"\n📉 Backtest Results (6-Month Return):\n- {ticker1}: {backtest[0]}%\n- {ticker2}: {backtest[1]}%\n"
markdown = f"""# GPT-4o Stock Comparison\n**Generated:** {timestamp}\n**Tickers Compared:** `{ticker1}` vs `{ticker2}`\n\n## 📈 GPT-4o Daily Top Picks\n{top_picks}\n\n## 🧠 Comparison Result\n{comparison}\n\n## 💬 Summary\n{summary}\n\n## 📉 Backtest (6-Month Performance)\n- `{ticker1}`: **{backtest[0]}%**\n- `{ticker2}`: **{backtest[1]}%**\n """
with open(md_file, "w", encoding="utf-8") as f:
f.write(markdown)
with open(json_file, "w", encoding="utf-8") as f:
json.dump({ticker1: data1, ticker2: data2}, f, indent=4)
print(f"\n✅ Analysis saved to: {md_file}, {json_file}")
if platform.system() == "Windows":
os.startfile(md_file)
elif platform.system() == "Darwin":
subprocess.call(["open", md_file])
elif platform.system() == "Linux":
subprocess.call(["xdg-open", md_file])
def get_valid_ticker(prompt_text):
while True:
user_input = input(prompt_text).upper().strip()
if "," in user_input or " " in user_input:
print(Fore.YELLOW + "⚠️ Please enter a single stock ticker (no commas or spaces). Try again." + Style.RESET_ALL)
continue
data = get_stock_data(user_input)
if data:
return user_input, data
print(Fore.RED + f"❌ {user_input} is not a valid ticker. Please try again." + Style.RESET_ALL)
def generate_sector_macro_commentary(ticker1, data1, ticker2, data2):
year = datetime.now().year
sector1 = data1.get('sector', 'N/A')
sector2 = data2.get('sector', 'N/A')
if sector1 == sector2:
sector_info = f"both {ticker1} and {ticker2} are in the {sector1} sector"
else:
sector_info = f"{ticker1} is in the {sector1} sector, while {ticker2} is in the {sector2} sector"
prompt = (
f"Provide a brief, up-to-date macroeconomic and sector context for a stock comparison where {sector_info}. "
f"Discuss any recent sector rotation, macro cycles, or notable events that could impact performance in {year}. "
f"Keep it under 120 words and focus on what an investor should know about the sector and macro backdrop."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a financial market strategist."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=200
)
return response.choices[0].message.content.strip()
def generate_scenario_risk_commentary(ticker1, data1, ticker2, data2, scenarios=None):
year = datetime.now().year
sector1 = data1.get('sector', 'N/A')
sector2 = data2.get('sector', 'N/A')
if sector1 == sector2:
sector_info = f"both {ticker1} and {ticker2} are in the {sector1} sector"
else:
sector_info = f"{ticker1} is in the {sector1} sector, while {ticker2} is in the {sector2} sector"
scenario_text = f"Scenarios to consider: {', '.join(scenarios)}. " if scenarios else ""
prompt = (
f"For a stock comparison where {sector_info}, provide:\n"
f"1. A brief scenario analysis: What could happen to these stocks if interest rates rise or fall in {year}?\n"
f"2. A risk commentary: What are the main risks for these stocks/sectors in the next year?\n"
f"{scenario_text}Keep it under 120 words and focus on practical, investor-relevant insights."
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a financial risk analyst."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=250
)
return response.choices[0].message.content.strip()
def get_outperformance_summary(ticker1, ticker2, stats1, stats2, stats_spy):
summary_lines = []
for label in stats1.keys():
r1 = stats1[label]['return']
r2 = stats2[label]['return']
r_spy = stats_spy[label]['return']
best = None
if all(isinstance(x, (int, float)) for x in [r1, r2, r_spy]):
if r1 >= r2 and r1 >= r_spy:
best = ticker1
elif r2 >= r1 and r2 >= r_spy:
best = ticker2
else:
best = 'SPY'
summary_lines.append(f"{label}: {best} outperformed ({ticker1}: {r1}%, {ticker2}: {r2}%, SPY: {r_spy}%)")
else:
summary_lines.append(f"{label}: Insufficient data to determine outperformance.")
return summary_lines
def export_to_excel(ticker1, ticker2, stats1, stats2, stats_spy, backtest, summary, filename, outperf_summary=None):
# Prepare returns/volatility table
periods = list(stats1.keys())
data = []
for label in periods:
s1 = stats1.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s2 = stats2.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s_spy = stats_spy.get(label, {'return': None, 'volatility': None, 'drawdown': None})
data.append({
'Period': label,
f'{ticker1} Return': s1['return'],
f'{ticker1} Volatility': s1['volatility'],
f'{ticker1} DD': s1['drawdown'],
f'{ticker2} Return': s2['return'],
f'{ticker2} Volatility': s2['volatility'],
f'{ticker2} DD': s2['drawdown'],
'SPY Return': s_spy['return'],
'SPY Volatility': s_spy['volatility'],
'SPY DD': s_spy['drawdown'],
})
df = pd.DataFrame(data)
# Backtest and summary
with pd.ExcelWriter(filename) as writer:
df.to_excel(writer, index=False, sheet_name='Returns & Volatility')
pd.DataFrame({'Backtest': [f'{ticker1}: {backtest[0]}%', f'{ticker2}: {backtest[1]}%']}).to_excel(writer, index=False, sheet_name='Backtest')
pd.DataFrame({'Summary': [summary]}).to_excel(writer, index=False, sheet_name='Summary')
if outperf_summary:
pd.DataFrame({'Outperformance': outperf_summary}).to_excel(writer, index=False, sheet_name='Outperformance')
# Conditional formatting
wb = writer.book
ws = writer.sheets['Returns & Volatility']
from openpyxl.formatting.rule import ColorScaleRule
# Find columns for returns and volatility
for col in range(2, 11): # columns B-K (returns/vol)
col_letter = chr(65 + col - 1)
if 'Return' in ws.cell(row=1, column=col).value:
ws.conditional_formatting.add(f'{col_letter}2:{col_letter}{len(df)+1}',
ColorScaleRule(start_type='min', start_color='F8696B',
mid_type='percentile', mid_value=50, mid_color='FFEB84',
end_type='max', end_color='63BE7B'))
elif 'Vol' in ws.cell(row=1, column=col).value:
ws.conditional_formatting.add(f'{col_letter}2:{col_letter}{len(df)+1}',
ColorScaleRule(start_type='min', start_color='63BE7B',
mid_type='percentile', mid_value=50, mid_color='FFEB84',
end_type='max', end_color='F8696B'))
def analyze_diversification_and_critique(top_picks):
tickers = extract_tickers_from_text(top_picks)
sector_counts = {}
ticker_sectors = {}
for t in tickers:
data = get_stock_data(t)
sector = data['sector'] if data else 'N/A'
ticker_sectors[t] = sector
if sector not in sector_counts:
sector_counts[sector] = 0
sector_counts[sector] += 1
# Diversification score: number of unique sectors / number of picks
unique_sectors = len([s for s in sector_counts if s != 'N/A'])
diversification_score = unique_sectors / max(1, len(tickers))
warning = ""
if unique_sectors < max(2, len(tickers)//2):
warning = f"⚠️ Diversification Warning: {len(tickers)-unique_sectors} picks share the same sector(s). Sectors: {sector_counts}"
# GPT critique
critique_prompt = (
f"Here is a list of stock picks and their sectors: {ticker_sectors}. "
f"Please provide a critical analysis of this list, focusing on sector/industry concentration, valuation, and macro headwinds. "
f"Warn if the list is not diversified or is exposed to specific risks. Keep it under 80 words."
)
critique = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a critical financial analyst."},
{"role": "user", "content": critique_prompt}
],
temperature=0.7,
max_tokens=150
).choices[0].message.content.strip()
return warning, critique, ticker_sectors
def export_to_html(filename, top_picks, comparison, summary, stats1, stats2, stats_spy, ticker1, ticker2, outperf_summary, macro_comment, scenario_comment, chart_file=None, diversification_warning=None, picks_critique=None, picks_sectors=None):
def html_table():
periods = list(stats1.keys())
rows = []
for label in periods:
s1 = stats1.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s2 = stats2.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s_spy = stats_spy.get(label, {'return': None, 'volatility': None, 'drawdown': None})
def fmt_ret(val):
if isinstance(val, (int, float)):
color = 'green' if val > 0 else 'red' if val < 0 else 'orange'
return f"<td style='color:{color};font-weight:bold'>{val}</td>"
return "<td style='color:orange'>N/A</td>"
def fmt_vol(val):
if isinstance(val, (int, float)):
if val < 20:
color = 'green'
elif val < 35:
color = 'goldenrod'
else:
color = 'red'
return f"<td style='color:{color}'>{val}</td>"
return "<td style='color:orange'>N/A</td>"
def fmt_dd(val):
if isinstance(val, (int, float)):
if val < -20:
color = 'red'
elif val < -10:
color = 'orange'
else:
color = 'green'
return f"<td style='color:{color};font-weight:bold'>{val}</td>"
return "<td style='color:orange'>N/A</td>"
rows.append(f"<tr><td>{label}</td>" +
fmt_ret(s1['return']) + fmt_vol(s1['volatility']) + fmt_dd(s1['drawdown']) +
fmt_ret(s2['return']) + fmt_vol(s2['volatility']) + fmt_dd(s2['drawdown']) +
fmt_ret(s_spy['return']) + fmt_vol(s_spy['volatility']) + fmt_dd(s_spy['drawdown']) +
"</tr>")
return """
<table border='1' cellpadding='4' cellspacing='0'>
<tr><th>Period</th><th>{0} Return</th><th>{0} Vol</th><th>{0} DD</th><th>{1} Return</th><th>{1} Vol</th><th>{1} DD</th><th>SPY Return</th><th>SPY Vol</th><th>SPY DD</th></tr>
{2}
</table>
""".format(ticker1, ticker2, "\n".join(rows))
def html_outperf():
return "<ul>" + "".join(f"<li>{line}</li>" for line in outperf_summary) + "</ul>"
def html_img():
if chart_file:
with open(chart_file, "rb") as imgf:
img_b64 = base64.b64encode(imgf.read()).decode('utf-8')
return f"<img src='data:image/png;base64,{img_b64}' style='max-width:100%;height:auto;border:1px solid #ccc;margin:10px 0;'>"
return ""
def html_sectors():
if picks_sectors:
return "<ul>" + "".join(f"<li>{t}: {s}</li>" for t, s in picks_sectors.items()) + "</ul>"
return ""
html = f"""
<html><head><meta charset='utf-8'><title>GPT-4o Stock Comparison</title>
<style>body{{font-family:sans-serif;max-width:900px;margin:0 auto;padding:2em;background:#f9f9f9;}}h1,h2{{color:#2a4d7a;}}.section{{margin-bottom:2em;}}</style>
<style>.textblock { white-space: pre-wrap; word-break: break-word; }
table { max-width: 100%; word-break: break-word; }
img { max-width: 100%; height: auto; }</style>
</head><body>
<h1>GPT-4o Stock Comparison</h1>
<div class='section'><b>Generated:</b> {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</div>
<div class='section'><h2>Top Picks</h2><div class='textblock'>{top_picks}</div></div>
<div class='section'><h2>Comparison</h2><pre>{comparison}</pre></div>
<div class='section'><h2>Summary</h2><pre>{summary}</pre></div>
<div class='section'><h2>Returns & Volatility</h2>{html_table()}</div>
<div class='section'><h2>Outperformance by Period</h2>{html_outperf()}</div>
<div class='section'><h2>Macro Context</h2><pre>{macro_comment}</pre></div>
<div class='section'><h2>Scenario & Risk Commentary</h2><pre>{scenario_comment}</pre></div>
<div class='section'><h2>Chart</h2>{html_img()}</div>
</body></html>
"""
with open(filename, "w", encoding="utf-8") as f:
f.write(html)
def get_recent_news(ticker, n=5):
try:
news = get_ticker_cached(ticker).news
if not news:
return []
return [(item['title'], item.get('publisher', ''), item.get('link', '')) for item in news[:n]]
except Exception:
return []
# Save/load user settings
SETTINGS_FILE = "gpt4o_user_settings.json"
def save_user_settings(settings):
try:
with open(SETTINGS_FILE, "w", encoding="utf-8") as f:
json.dump(settings, f, indent=2)
print(Fore.GREEN + f"[Settings] Saved to {SETTINGS_FILE}" + Style.RESET_ALL)
except Exception as e:
print(Fore.RED + f"[Settings] Error saving settings: {e}" + Style.RESET_ALL)
def load_user_settings():
try:
with open(SETTINGS_FILE, "r", encoding="utf-8") as f:
settings = json.load(f)
print(Fore.GREEN + f"[Settings] Loaded from {SETTINGS_FILE}" + Style.RESET_ALL)
return settings
except Exception:
return {}
# Portfolio rebalancing suggestion (simple version)
def suggest_portfolio_rebalance(portfolio_tickers):
sector_counts = {}
for t in portfolio_tickers:
data = get_stock_data(t)
sector = data['sector'] if data else 'N/A'
sector_counts[sector] = sector_counts.get(sector, 0) + 1
if len(sector_counts) == 1:
print(Fore.YELLOW + f"[Rebalance] All portfolio tickers are in {list(sector_counts.keys())[0]}. Consider adding other sectors for diversification." + Style.RESET_ALL)
elif len(sector_counts) < len(portfolio_tickers)//2:
print(Fore.YELLOW + f"[Rebalance] Portfolio is concentrated in a few sectors: {sector_counts}. Consider rebalancing for better diversification." + Style.RESET_ALL)
else:
print(Fore.GREEN + "[Rebalance] Portfolio is reasonably diversified by sector." + Style.RESET_ALL)
# Sector/industry breakdown pie chart
def plot_sector_breakdown(tickers, filename="sector_breakdown.png"):
import matplotlib.pyplot as plt
sector_counts = {}
for t in tickers:
data = get_stock_data(t)
sector = data['sector'] if data else 'N/A'
sector_counts[sector] = sector_counts.get(sector, 0) + 1
labels = list(sector_counts.keys())
sizes = list(sector_counts.values())
plt.figure(figsize=(6,6))
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=140)
plt.title('Sector/Industry Breakdown')
plt.tight_layout()
plt.savefig(filename)
plt.close()
print(Fore.CYAN + f"[Chart] Sector breakdown saved to: {filename}" + Style.RESET_ALL)
# CSV export for summary/returns
def export_to_csv(ticker1, ticker2, stats1, stats2, stats_spy, filename):
try:
periods = list(stats1.keys())
data = []
for label in periods:
s1 = stats1.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s2 = stats2.get(label, {'return': None, 'volatility': None, 'drawdown': None})
s_spy = stats_spy.get(label, {'return': None, 'volatility': None, 'drawdown': None})
data.append({
'Period': label,
f'{ticker1} Return': s1['return'],
f'{ticker1} Volatility': s1['volatility'],
f'{ticker1} DD': s1['drawdown'],
f'{ticker2} Return': s2['return'],
f'{ticker2} Volatility': s2['volatility'],
f'{ticker2} DD': s2['drawdown'],
'SPY Return': s_spy['return'],
'SPY Volatility': s_spy['volatility'],
'SPY DD': s_spy['drawdown'],
})
df = pd.DataFrame(data)
df.to_csv(filename, index=False)
print(Fore.CYAN + f"[CSV export] Results saved to: {filename}" + Style.RESET_ALL)
except Exception as e:
print(Fore.RED + f"[CSV export] Error: {e}" + Style.RESET_ALL)
# Ticker autocomplete (stub, only works in some terminals)
def autocomplete_ticker(prompt_text, valid_tickers=None):
try:
import readline
if valid_tickers:
def completer(text, state):
options = [i for i in valid_tickers if i.startswith(text.upper())]
if state < len(options):
return options[state]
return None
readline.set_completer(completer)
readline.parse_and_bind("tab: complete")
return input(prompt_text)
except Exception:
return input(prompt_text)
# Helper: Fetch recent market news headlines (Yahoo Finance, Google News)
def fetch_market_news(n=50, focus_topics=None):
feeds = [
'https://finance.yahoo.com/rss/topstories',
'https://news.google.com/rss/search?q=stock+market',
'https://news.google.com/rss/search?q=acquisition+OR+merger+OR+AI+OR+OpenAI+OR+geopolitics+OR+sanctions+OR+IPO+OR+earnings',
]
headlines = []
for url in feeds:
try:
d = feedparser.parse(url)
for entry in d.entries[:n]:
headlines.append(entry.title)
except Exception as e:
print(Fore.YELLOW + f"[News] Error fetching feed {url}: {e}" + Style.RESET_ALL)
# Filter by focus topics if provided
if focus_topics:
focus_topics = [t.lower() for t in focus_topics]
headlines = [h for h in headlines if any(t in h.lower() for t in focus_topics)]
return headlines[:n]
# Helper: Simple entity/event extraction from headlines (stub for now)
def extract_entities_events(headlines):
# TODO: Use spaCy or similar for robust extraction
keywords = ['acquisition', 'merger', 'AI', 'OpenAI', 'IPO', 'lawsuit', 'regulation', 'sanction', 'geopolitics', 'partnership', 'deal', 'breakthrough', 'earnings', 'expansion', 'layoff', 'strike', 'scandal', 'hack', 'cyber', 'antitrust', 'approval', 'FDA', 'SEC', 'FTC', 'buyout', 'investment', 'funding', 'bankruptcy', 'collapse', 'AI', 'cloud', 'quantum', 'chip', 'semiconductor', 'defense', 'energy', 'EV', 'battery', 'China', 'Russia', 'Ukraine', 'Middle East', 'Israel', 'Palestine', 'Taiwan', 'India', 'Africa', 'BRICS']
companies = []
events = []
for h in headlines:
for k in keywords:
if k.lower() in h.lower():
events.append((k, h))
# Simple company extraction: look for uppercase words (tickers or names)
companies += re.findall(r'\b[A-Z]{2,5}\b', h)
return companies, events
# Enhanced: News-driven picks logic
def get_news_driven_picks(top_picks, focus_topics=None):
headlines = fetch_market_news(n=50, focus_topics=focus_topics)
companies, events = extract_entities_events(headlines)
# Count company mentions
company_counts = Counter(companies)
# Extract tickers from top picks
top_tickers = extract_tickers_from_text(top_picks)
# Boost tickers in top picks if mentioned in news
boosted = [t for t in top_tickers if t in company_counts and company_counts[t] > 0]
# Add new tickers from news if not in top picks
extra = [c for c, count in company_counts.items() if c not in top_tickers and count > 1]
# Compose explanation
explanations = []
for t in boosted:
explanations.append(f"{t}: In top picks and mentioned in recent news headlines.")
for c in extra:
explanations.append(f"{c}: Not in top picks but frequently mentioned in recent news.")
# Highlight major events
event_lines = [f"- {k}: {h}" for k, h in events]
return boosted + extra, explanations, event_lines
def filter_outliers(self, posts: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
# TODO: Use NLP/statistics to flag outlier or suspicious posts
return posts # For now, no filtering
def test_top_picks():
"""Test that top_picks returns a list of tickers."""
analyzer = StockAnalyzer()
picks = analyzer.top_picks()
assert isinstance(picks, list)
assert all(isinstance(t, str) for t in picks)
def test_compare():
"""Test that compare returns a dict with the correct keys."""
analyzer = StockAnalyzer()
tickers = ["AAPL", "MSFT"]
result = analyzer.compare(tickers)
assert isinstance(result, dict)
assert set(result.keys()) == set(tickers)
def test_portfolio_analysis():
"""Test that portfolio_analysis returns a dict with a summary key."""
analyzer = StockAnalyzer()
portfolio = ["AAPL", "GOOGL"]
result = analyzer.portfolio_analysis(portfolio, amount=10000)
assert isinstance(result, dict)
assert "summary" in result
def safe_float(val):
try:
return float(val)
except (TypeError, ValueError):
return None
def analyze_portfolio(portfolio_tickers, amount):
"""Analyze a portfolio of stocks with proper error handling and validation."""
if not portfolio_tickers or not amount:
print(Fore.RED + "❌ Please provide both portfolio tickers and amount." + Style.RESET_ALL)
return
# Validate tickers and get data
valid_tickers = []
ticker_data = {}
total_value = 0
for ticker in portfolio_tickers:
data = get_stock_data(ticker)
if data:
valid_tickers.append(ticker)
ticker_data[ticker] = data
total_value += data['price']
else:
print(Fore.YELLOW + f"⚠️ Skipping invalid ticker: {ticker}" + Style.RESET_ALL)
if not valid_tickers:
print(Fore.RED + "❌ No valid tickers in portfolio." + Style.RESET_ALL)
return
# Calculate allocations
n = len(valid_tickers)
allocation = amount / n
allocations = {ticker: allocation for ticker in valid_tickers}
# Calculate portfolio metrics
pe_ratios = [safe_float(data['pe_ratio']) for data in ticker_data.values()]
pe_ratios = [x for x in pe_ratios if x is not None]
div_yields = [safe_float(data['dividend_yield']) for data in ticker_data.values()]
div_yields = [x for x in div_yields if x is not None]
avg_pe = sum(pe_ratios) / len(pe_ratios) if pe_ratios else None
avg_div_yield = sum(div_yields) / len(div_yields) if div_yields else None
# Print portfolio analysis
print(Fore.CYAN + "\n💼 Portfolio Analysis:" + Style.RESET_ALL)
print(f"Total Value: ${amount:,.2f}")
print(f"Number of Positions: {n}")
print(f"Average Position Size: ${allocation:,.2f}")
if avg_pe:
print(f"Average P/E Ratio: {avg_pe:.2f}")
if avg_div_yield:
print(f"Average Dividend Yield: {avg_div_yield:.2f}%")
print(Fore.CYAN + "\nIndividual Positions:" + Style.RESET_ALL)
print(f"{'Ticker':<8} | {'Allocation':>12} | {'P/E':>8} | {'Div Yield':>10} | {'Sector':<15}")
print("-" * 65)
def format_float(val, width=8, precision=2, suffix=''):
try:
return f"{float(val):>{width}.{precision}f}{suffix}"
except (TypeError, ValueError):
return f"{'N/A':>{width}}{suffix}"
for ticker in valid_tickers:
data = ticker_data[ticker]
pe_str = format_float(data['pe_ratio'])
div_str = format_float(data['dividend_yield'], width=9, precision=2, suffix='%')
print(f"{ticker:<8} | ${allocation:>10,.2f} | {pe_str} | {div_str} | {data['sector']:<15}")
# Calculate and display returns
print(Fore.CYAN + "\nHistorical Returns:" + Style.RESET_ALL)
periods = {'1mo': 30, '3mo': 90, 'YTD': 'YTD', '1yr': 365}
for ticker in valid_tickers:
returns = get_period_returns_and_volatility(ticker, periods)
print(f"\n{ticker}:")
for period, data in returns.items():
if data['return'] is not None:
print(f" {period}: {data['return']:>6.2f}% (Vol: {data['volatility']:.2f}%)")
# Generate portfolio critique
critique_prompt = (
f"Analyze this portfolio: {valid_tickers}\n"
f"Total Value: ${amount:,.2f}\n"
f"Average P/E: {avg_pe:.2f}\n"
f"Average Dividend Yield: {avg_div_yield:.2f}%\n"
f"Sectors: {[data['sector'] for data in ticker_data.values()]}\n"
"Provide a brief critique focusing on diversification, risk, and potential improvements."
)
critique = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a portfolio analyst."},
{"role": "user", "content": critique_prompt}
],
temperature=0.7,
max_tokens=200
).choices[0].message.content.strip()
print(Fore.CYAN + "\n🧐 Portfolio Critique:" + Style.RESET_ALL)
print(critique)
# Generate sector breakdown chart
plot_sector_breakdown(valid_tickers, filename="portfolio_sector_breakdown.png")
print(Fore.GREEN + f"\n✅ Sector breakdown chart saved to: portfolio_sector_breakdown.png" + Style.RESET_ALL)
def run_all_features(include_social=False, user_profile=None, profile_summary=None):
"""Run all available features of the stock analyzer and generate an HTML report."""
print(Fore.CYAN + "\n=== Running All Features ===" + Style.RESET_ALL)
# Get top picks
print(Fore.CYAN + "\n=== Top Picks ===" + Style.RESET_ALL)
top_picks = get_top_stock_picks(profile=user_profile['experience'] if user_profile else None, risk_tolerance=user_profile['risk'] if user_profile else None)
print(top_picks)
# Compare stocks
print(Fore.CYAN + "\n=== Compare Stocks ===" + Style.RESET_ALL)
while True:
print("Enter tickers to compare (comma-separated) [AAPL,MSFT,GOOGL]: ", end='')
tickers_input = input().strip() or "AAPL,MSFT,GOOGL"
tickers = [t.strip().upper() for t in tickers_input.split(",")]
valid_tickers = [t for t in tickers if is_valid_ticker(t)]
invalid_tickers = [t for t in tickers if not is_valid_ticker(t)]
if not valid_tickers:
print(Fore.RED + "❌ No valid tickers entered. Please try again." + Style.RESET_ALL)
continue
if invalid_tickers:
print(Fore.YELLOW + f"⚠️ Skipping invalid tickers: {', '.join(invalid_tickers)}" + Style.RESET_ALL)
break
ticker_data = {t: get_stock_data(t) for t in valid_tickers}
comparisons = []
summaries = []
backtests = []
for i in range(len(valid_tickers)):
for j in range(i + 1, len(valid_tickers)):
t1, t2 = valid_tickers[i], valid_tickers[j]
if t1 in ticker_data and t2 in ticker_data:
comparison = generate_comparison(t1, ticker_data[t1], t2, ticker_data[t2])
summary = summarize_sentiment(comparison)
backtest = get_backtest_data(t1, t2)
comparisons.append((t1, t2, comparison))
summaries.append((t1, t2, summary))
backtests.append((t1, t2, backtest))
print(Fore.CYAN + f"\n=== {t1} vs {t2} ===" + Style.RESET_ALL)
print(comparison)
print(Fore.GREEN + f"\nSummary: {summary}" + Style.RESET_ALL)
print(Fore.YELLOW + f"\nBacktest (6-month return):" + Style.RESET_ALL)
print(format_backtest_result(t1, backtest[0]))
print(format_backtest_result(t2, backtest[1]))
# Portfolio analysis
print(Fore.CYAN + "\n=== Portfolio Analysis ===" + Style.RESET_ALL)
# Loop until user enters at least one valid ticker