-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_interface.py
More file actions
1419 lines (1169 loc) · 55 KB
/
Copy pathllm_interface.py
File metadata and controls
1419 lines (1169 loc) · 55 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
"""
LLM integration for narrative generation and parsing.
"""
import json
import logging
import httpx
import pandas as pd
from pathlib import Path
from config import Config
from httpx import Limits, Timeout
from langchain_openai import ChatOpenAI
from typing import Dict, List, Tuple, Optional
from langchain_core.prompts import PromptTemplate
from models import ParsedRequest, TickerAnalysis, PortfolioMetrics
from tenacity import (
retry,
stop_after_attempt,
wait_exponential,
retry_if_exception_type,
before_sleep_log,
)
logger = logging.getLogger(__name__)
class IntegratedLLMInterface:
"""LLM interface with connection pooling for better performance."""
def __init__(
self,
config: Config,
max_connections: int = 20,
max_keepalive_connections: int = 10,
) -> None:
"""
Initializes the IntegratedLLMInterface with a configuration object and connection pooling settings.
Args:
config (Config): The configuration object.
max_connections (int): The maximum number of concurrent connections.
max_keepalive_connections (int): The maximum number of idle connections to keep alive.
"""
self.config = config
# Create HTTP client with connection pooling
# LangChain's ChatOpenAI uses httpx internally
http_client = self._create_pooled_http_client(
max_connections, max_keepalive_connections
)
# Initialize LLM with connection pooling
self.llm = ChatOpenAI(
api_key=config.openai_api_key,
base_url=config.openai_base_url,
model=config.model_name,
temperature=0.3,
timeout=config.request_timeout,
http_client=http_client, # Use our pooled client
http_async_client=None, # We're not using async
)
# Log LLM setup (without exposing API key)
provider = config._get_provider_from_url()
logger.info(
f"LLM initialized - Provider: {provider}, Model: {config.model_name}, "
f"Temperature: 0.3, Connection pool: {max_keepalive_connections}/{max_connections}"
)
self._setup_prompts()
self._setup_chains()
def _create_pooled_http_client(
self, max_connections: int = 20, max_keepalive: int = 10
) -> "httpx.Client":
"""Create an httpx client with connection pooling.
Args:
max_connections: Maximum total connections
max_keepalive: Maximum idle connections to keep alive
Returns:
httpx.Client with connection pooling configured
"""
import httpx
# Configure connection limits
limits = Limits(
max_connections=max_connections,
max_keepalive_connections=max_keepalive,
keepalive_expiry=30.0, # Keep connections alive for 30 seconds
)
# Configure timeout
timeout = Timeout(
connect=10.0, # Connection timeout
read=self.config.request_timeout, # Read timeout
write=10.0, # Write timeout
pool=5.0, # Pool timeout
)
# Create client with pooling
client = httpx.Client(
limits=limits,
timeout=timeout,
http2=True, # Enable HTTP/2 for multiplexing
follow_redirects=True,
)
logger.info(
f"Created HTTP client with connection pool "
f"(max: {max_connections}, keepalive: {max_keepalive})"
)
return client
def _setup_prompts(self) -> None:
"""Setup prompt templates."""
self.parse_prompt = PromptTemplate(
input_variables=["user_request"],
template="""Parse this financial analysis request into structured parameters.
User Request: {user_request}
Return ONLY valid JSON with:
- tickers: list of stock symbols (uppercase, e.g., ["AAPL", "MSFT"])
- period: one of [1d, 5d, 1mo, 3mo, 6mo, 1y, 2y, 5y, 10y, ytd, max]
- metrics: list of requested metrics
- output_format: "markdown" or "pdf"
Example: {{"tickers": ["AAPL"], "period": "1y", "metrics": ["returns", "risk"], "output_format": "markdown"}}
Return ONLY the JSON, no other text.""",
)
self.narrative_prompt = PromptTemplate(
input_variables=["analysis_data", "period"],
template="""You are a senior financial analyst. Generate a compelling executive summary.
Analysis Period: {period}
Data: {analysis_data}
IMPORTANT GUIDELINES:
1. **Metric Definitions (use these correctly):**
- **avg_daily_return**: This is the DAILY average return (NOT annualized).
* Formula for annualized return: ((1 + avg_daily_return/100)^252 - 1) × 100
* Example: If avg_daily_return = 0.384%, then annualized = ((1.00384)^252 - 1) × 100 = 162.7%
* Example: If avg_daily_return = 0.429%, then annualized = ((1.00429)^252 - 1) × 100 = 194.1%
* ALWAYS use this formula, DO NOT multiply by 252 or use simple scaling
- **daily_volatility**: This is DAILY std dev (NOT annualized). To discuss annualized volatility, calculate: daily_volatility × √252
- **sharpe_ratio**: This is the annualized Sharpe ratio (calculated from annualized returns and volatility, already annualized, don't scale it)
- VaR and max drawdown are percentages (VaR is shown as negative)
- RSI: 0-100 scale where exact value matters
- P/E: Price-to-Earnings ratio (higher = more expensive relative to earnings)
- P/B: Price-to-Book ratio (higher = more expensive relative to book value, NOT "compression")
2. **Technical Interpretation Rules - Be Precise:**
- RSI: Use EXACT values from data. Interpretation scale:
* <30: Oversold
* 30-45: Weak/bearish zone
* 45-55: Neutral zone
* 55-70: Bullish zone (NOT overbought)
* >70: Overbought
DO NOT group tickers with RSI 47 and 65 into the same "neutral-bullish" category. They are in different zones!
- Sharpe >1: Good risk-adjusted returns
- Max DD >-20%: Moderate risk, <-20%: High risk
- P/E ratio: High values (>25) = expensive relative to earnings, Low (<15) = cheap
- P/B ratio: High values (>5) = expensive relative to book value (high valuation), Low (<1) = cheap (possible value opportunity). NEVER say "compression" for high P/B - that's the opposite!
3. **CRITICAL: Consistency Requirements:**
- Before writing, identify the ticker with highest avg_daily_return from the data
- Before writing, identify the ticker with lowest avg_daily_return from the data
- Use these SAME tickers consistently throughout the narrative
- NEVER claim ticker A has "highest returns" in one place and ticker B has "highest returns" elsewhere
- If you mention a metric for a ticker, use the EXACT value from the data (no rounding changes)
4. **When citing numbers:**
- State "daily" or "annualized" explicitly
- Match EXACT values from the data - no rounding changes
- If converting (e.g., daily to annual), show the calculation
- For unusual values, cite accurately and note they're unusual
5. **Write 3-4 paragraphs that:**
- Highlight significant findings with precise numbers
- Use CORRECT interpretations (e.g., don't call RSI 60 "overbought")
- Compare performance across tickers
- Identify risks with supporting data
6. **Avoid these common errors:**
- ❌ Calling RSI 50-70 "overbought" (it's neutral/bullish)
- ❌ Using different values for same metric in different places
- ❌ Contradicting yourself (e.g., saying ticker A has highest return, then saying ticker B has highest return)
- ❌ Making claims without data support (e.g., "above market average" without providing market average)
- ❌ Confusing daily and annualized figures
- ❌ CRITICAL: Incorrect annualization math (e.g., claiming 0.384% daily = 274% annual is WRONG, correct is 162.7%)
7. **Before finalizing, verify:**
- All annualized return calculations use the correct formula: ((1 + daily%/100)^252 - 1) × 100
- All numbers match the source data exactly
- No calculation errors
Be precise, accurate, and professional. Use correct technical definitions.
Return ONLY the narrative text.""",
)
self.review_prompt = PromptTemplate(
input_variables=["report_content", "data_summary"],
template="""Review this financial report for accuracy and quality.
Report (excerpt):
{report_content}
Source Data Summary:
{data_summary}
IMPORTANT: Distinguish between CRITICAL ISSUES and SUGGESTIONS FOR IMPROVEMENT.
**CRITICAL ISSUES** (report in "issues" field):
Only flag items that are FACTUALLY WRONG or MISLEADING:
1. Numbers that don't match source data (>5% discrepancy)
2. Incorrect interpretations (e.g., "RSI 60 is overbought" when overbought is >70)
3. Mathematical errors in calculations
4. Contradictory statements in the same report
5. Misleading claims not supported by data
**SUGGESTIONS** (report in "suggestions" field):
Items that would ENHANCE the report but are not errors:
1. Missing methodology explanations (calculations are correct but process not explained)
2. Missing context (e.g., no industry comparison for P/E ratio)
3. Ambiguous labels (e.g., table headers could be clearer)
4. Additional disclosures that would improve transparency
5. Formatting or presentation improvements
EXAMPLES:
❌ CRITICAL ISSUE: "Report states RSI of 59.5 is 'overbought' but overbought is typically >70"
✅ SUGGESTION: "Report could clarify the methodology for calculating Sharpe ratio"
❌ CRITICAL ISSUE: "Annualized return calculated as 18% but source shows 0.05% daily return (should be ~13%)"
✅ SUGGESTION: "Table headers could specify whether metrics are daily or annualized"
❌ CRITICAL ISSUE: "Report claims P/E is 'well above market average' without providing market average"
✅ SUGGESTION: "P/E ratio could include industry average for comparison"
Return valid JSON with:
- "issues": list of CRITICAL ERRORS only (factually wrong, misleading, contradictory)
- "suggestions": list of optional improvements (missing context, methodology notes, clarity enhancements)
- "quality_score": integer 1-10
* 9-10: Excellent, no critical issues, minimal suggestions
* 7-8: Good, no critical issues, some helpful suggestions
* 5-6: Acceptable, 1-2 minor critical issues or many clarity concerns
* <5: Poor, multiple critical errors
Be precise: Only flag as "issues" if the report is factually wrong or misleading.
Return ONLY the JSON.""",
)
# Options-specific prompts
self.options_narrative_prompt = PromptTemplate(
input_variables=["options_data", "ticker", "spot_price"],
template="""You are an options trading expert. Generate an executive summary of options opportunities.
Ticker: {ticker}
Current Price: ${spot_price}
Options Data: {options_data}
Generate a concise executive summary covering:
1. **Implied Volatility Assessment**
- Current IV vs historical volatility
- IV skew patterns (put vs call)
- Whether options are expensive or cheap
2. **Top Strategy Opportunities** (choose 2-3 most attractive)
- Strategy name and structure
- Entry cost and max profit/loss
- Breakeven points
- Why this strategy makes sense now
IMPORTANT: If you include a markdown table, ensure ALL rows have EXACTLY the same number of columns as the header row.
Count the pipes (|) carefully - every row must have the same count.
3. **Greeks Summary**
- Key risk exposures (Delta, Vega, Theta)
- How Greeks affect position value
4. **Risk Assessment**
- Primary risks for options traders
- Time decay considerations
- Volatility risks
Be specific with numbers. Focus on actionable insights.
Return ONLY the narrative text, no JSON.""",
)
self.options_recommendations_prompt = PromptTemplate(
input_variables=["ticker", "strategies", "market_context", "portfolio_holdings"],
template="""You are an options strategist. Provide personalized strategy recommendations.
Ticker: {ticker}
Identified Strategies: {strategies}
Market Context: {market_context}
Current Holdings: {portfolio_holdings}
Provide 3-5 specific recommendations ranked by attractiveness:
For each recommendation:
1. **Strategy Name** (e.g., "Covered Call - $150 Strike")
2. **Entry Details**: Exact strikes, expiration, premiums
3. **Rationale**: Why this strategy fits the current market conditions
4. **Risk/Reward**: Max profit, max loss, breakeven
5. **Probability of Profit**: Estimated likelihood of success
6. **Best For**: Type of investor (income, speculation, hedging)
CRITICAL TABLE FORMATTING RULES:
- If you create a markdown table, EVERY data row MUST have EXACTLY the same number of columns (pipe characters |) as the header row
- Example: If header has 8 pipes (7 columns), every data row must have 8 pipes
- Do NOT merge cells or skip columns - provide data for every column in every row
- If a value is unknown, use "N/A" or "-" rather than omitting the column
Consider:
- Implied volatility levels (high IV favors selling, low IV favors buying)
- Time to expiration (theta decay)
- Existing portfolio positions (hedging opportunities)
- Current price trends and momentum
Be direct and actionable. Focus on strategies with good risk/reward.
Return ONLY the narrative text.""",
)
self.portfolio_hedging_prompt = PromptTemplate(
input_variables=["portfolio_greeks", "positions", "risk_tolerance"],
template="""You are a portfolio risk manager. Provide hedging recommendations.
Portfolio Greeks: {portfolio_greeks}
Current Positions: {positions}
Risk Tolerance: {risk_tolerance}
Analyze the portfolio's options exposure and provide:
1. **Current Risk Profile**
- Net Delta exposure (directional risk)
- Vega exposure (volatility risk)
- Theta decay (time risk)
- Gamma risk (how Delta changes)
2. **Hedging Priorities** (rank top 3)
- Which Greek needs immediate attention
- Why it's a priority
- Potential impact if unhedged
3. **Specific Hedging Strategies**
For each priority:
- Exact strategy (e.g., "Buy 100 shares SPY to neutralize delta")
- Cost and effectiveness
- Alternative approaches
4. **Portfolio Balancing**
- Concentration risks (if one ticker dominates Greeks)
- Diversification opportunities
- Suggested position adjustments
Be precise with numbers. Provide implementable hedges.
Return ONLY the narrative text.""",
)
def _setup_chains(self) -> None:
"""Setup LangChain chains."""
self.parser_chain = self.parse_prompt | self.llm
self.narrative_chain = self.narrative_prompt | self.llm
self.review_chain = self.review_prompt | self.llm
# Options chains
self.options_narrative_chain = self.options_narrative_prompt | self.llm
self.options_recommendations_chain = self.options_recommendations_prompt | self.llm
self.portfolio_hedging_chain = self.portfolio_hedging_prompt | self.llm
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type((ConnectionError, TimeoutError, OSError)),
before_sleep=before_sleep_log(logger, logging.WARNING),
reraise=True,
)
def parse_natural_language_request(self, user_request: str) -> ParsedRequest:
"""
Parses a natural language request into a structured format.
Automatically retries up to 3 times with exponential backoff for network errors.
Args:
user_request (str): The natural language request from the user.
Returns:
ParsedRequest: A structured request object.
Raises:
ValueError: If parsing fails after retries
ConnectionError: If network errors persist after retries
"""
logger.info("Parsing natural language request")
try:
response = self.parser_chain.invoke({"user_request": user_request})
parsed_text = response.content.strip()
# Clean markdown code blocks
if "```json" in parsed_text:
parsed_text = parsed_text.split("```json")[1].split("```")[0]
elif "```" in parsed_text:
parsed_text = parsed_text.split("```")[1].split("```")[0]
parsed_json = json.loads(parsed_text)
parsed = ParsedRequest(
tickers=[t.upper().strip() for t in parsed_json.get("tickers", [])],
period=parsed_json.get("period", "1y"),
metrics=parsed_json.get("metrics", []),
output_format=parsed_json.get("output_format", "markdown"),
)
parsed.validate()
logger.info(f"Parsed request: {parsed.tickers}")
return parsed
except json.JSONDecodeError as e:
logger.error(f"JSON parse failed: {e}")
raise ValueError("Failed to parse JSON response") from e
except (KeyError, ValueError, TypeError, AttributeError) as e:
logger.error(f"Parse error: {e}")
raise ValueError(f"Parse error: {str(e)}") from e
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type((ConnectionError, TimeoutError, OSError)),
before_sleep=before_sleep_log(logger, logging.WARNING),
reraise=True,
)
def generate_narrative_summary(
self, analyses: Dict[str, TickerAnalysis], period: str
) -> str:
"""
Generates a narrative summary of the financial analysis using the LLM.
Automatically retries up to 3 times with exponential backoff for network errors.
Args:
analyses (Dict[str, TickerAnalysis]): A dictionary of ticker analyses.
period (str): The analysis period.
Returns:
str: The narrative summary.
"""
logger.info("Generating narrative summary with LLM")
# Prepare concise data for LLM with clear labels
analysis_summary = {}
for ticker, analysis in analyses.items():
if not analysis.error:
analysis_summary[ticker] = {
"current_price": f"${analysis.latest_close:.2f}",
"avg_daily_return": f"{analysis.avg_daily_return * 100:.3f}%",
"daily_volatility": f"{analysis.volatility * 100:.2f}%",
"sharpe_ratio": (
round(analysis.advanced_metrics.sharpe_ratio, 2)
if analysis.advanced_metrics.sharpe_ratio
else None
),
"max_drawdown_pct": f"{(analysis.advanced_metrics.max_drawdown or 0) * 100:.1f}%",
"rsi": (
round(analysis.technical_indicators.rsi, 1)
if analysis.technical_indicators.rsi
else None
),
"pe_ratio": (
round(analysis.ratios.get("pe_ratio"), 1)
if analysis.ratios.get("pe_ratio")
else None
),
"pb_ratio": (
round(analysis.ratios.get("price_to_book"), 2)
if analysis.ratios.get("price_to_book")
else None
),
"alerts": analysis.alerts,
}
try:
response = self.narrative_chain.invoke(
{
"analysis_data": json.dumps(analysis_summary, indent=2),
"period": period,
}
)
narrative = response.content.strip()
logger.info("Generated narrative summary")
return narrative
except (KeyError, ValueError, TypeError, AttributeError, OSError) as e:
logger.error(f"Failed to generate narrative: {e}")
return self._fallback_narrative(analyses, period)
def _fallback_narrative(
self, analyses: Dict[str, TickerAnalysis], period: str
) -> str:
"""Fallback narrative if LLM fails."""
successful = {t: a for t, a in analyses.items() if not a.error}
if not successful:
return "Analysis completed but no valid data available."
best_performing_ticker = max(
successful.items(), key=lambda x: x[1].avg_daily_return
)
worst_performing_ticker = min(
successful.items(), key=lambda x: x[1].avg_daily_return
)
return f"""Over the {period} period, {len(successful)} stocks were analyzed.
{best_performing_ticker[0]} showed the strongest performance with a {best_performing_ticker[1].avg_daily_return*100:.2f}% average daily return,
while {worst_performing_ticker[0]} had the weakest returns at {worst_performing_ticker[1].avg_daily_return*100:.2f}%.
Risk metrics varied across the portfolio, with several stocks showing elevated volatility warranting monitoring."""
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, min=1, max=10),
retry=retry_if_exception_type((ConnectionError, TimeoutError, OSError)),
before_sleep=before_sleep_log(logger, logging.WARNING),
reraise=True,
)
def review_report(
self, report_content: str, analyses: Dict[str, TickerAnalysis]
) -> Tuple[List[str], int, Dict]:
"""
Reviews a financial report for accuracy and quality using the LLM.
Automatically retries up to 3 times with exponential backoff for network errors.
Args:
report_content (str): The content of the report to review.
analyses (Dict[str, TickerAnalysis]): A dictionary of ticker analyses.
Returns:
Tuple[List[str], int, Dict]: A tuple containing a list of issues, a quality score, and the full review dictionary.
"""
logger.info("Reviewing report with LLM")
# Prepare data summary
data_summary = {}
for ticker, analysis in analyses.items():
if not analysis.error:
data_summary[ticker] = {
"latest_close": analysis.latest_close,
"avg_daily_return": analysis.avg_daily_return,
"daily_volatility": analysis.volatility,
"sharpe_ratio": analysis.advanced_metrics.sharpe_ratio,
# Include ALL technical indicators so review can verify claims
"rsi": analysis.technical_indicators.rsi,
"stochastic_k": analysis.technical_indicators.stochastic_k,
"stochastic_d": analysis.technical_indicators.stochastic_d,
"macd": analysis.technical_indicators.macd,
"macd_signal": analysis.technical_indicators.macd_signal,
"bollinger_upper": analysis.technical_indicators.bollinger_upper,
"bollinger_lower": analysis.technical_indicators.bollinger_lower,
"bollinger_position": analysis.technical_indicators.bollinger_position,
"atr": analysis.technical_indicators.atr,
"obv": analysis.technical_indicators.obv,
# "vwap": removed - not appropriate for daily data
"ma_200d": analysis.technical_indicators.ma_200d,
"pe_ratio": analysis.ratios.get("pe_ratio"),
"pb_ratio": analysis.ratios.get("price_to_book"),
}
# Warn if report content is truncated
original_length = len(report_content)
max_review_length = 4000
if original_length > max_review_length:
logger.warning(
f"Report content truncated for review: {original_length} chars -> {max_review_length} chars "
f"({original_length - max_review_length} chars omitted)"
)
try:
response = self.review_chain.invoke(
{
"report_content": report_content[:max_review_length],
"data_summary": json.dumps(data_summary, indent=2),
}
)
review_text = response.content.strip()
# Clean markdown
if "```json" in review_text:
review_text = review_text.split("```json")[1].split("```")[0]
elif "```" in review_text:
review_text = review_text.split("```")[1].split("```")[0]
review_json = json.loads(review_text)
issues = review_json.get("issues", [])
suggestions = review_json.get("suggestions", [])
quality_score = review_json.get("quality_score", 7)
logger.info(
f"Review: {len(issues)} issues, {len(suggestions)} suggestions, quality: {quality_score}/10"
)
# Return full review data
full_review = {
"issues": issues,
"suggestions": suggestions,
"quality_score": quality_score,
}
return issues, quality_score, full_review
except (
json.JSONDecodeError,
KeyError,
ValueError,
TypeError,
AttributeError,
OSError,
) as e:
logger.error(f"Review failed: {e}")
return (
[],
7,
{"issues": [], "suggestions": [], "quality_score": 7, "error": str(e)},
)
def _generate_report_header(self) -> List[str]:
"""Generate report header with timestamp.
Returns:
List of header lines
"""
from datetime import datetime, timezone
return [
"# 📊 Financial Analysis Report",
f"*Generated: {datetime.now(timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')}*",
"",
]
def _generate_executive_summary_section(
self, analyses: Dict[str, TickerAnalysis], period: str
) -> List[str]:
"""Generate executive summary section with LLM narrative.
Args:
analyses: All analysis results
period: Analysis period
Returns:
List of section lines
"""
narrative = self.generate_narrative_summary(analyses, period)
return ["## 🎯 Executive Summary", narrative, ""]
def _generate_portfolio_overview_section(
self, portfolio_metrics: Optional[PortfolioMetrics]
) -> List[str]:
"""Generate portfolio overview section if available.
Args:
portfolio_metrics: Portfolio metrics or None
Returns:
List of section lines (empty if no portfolio metrics)
"""
if not portfolio_metrics:
return []
return [
"## 💼 Portfolio Overview",
self._generate_portfolio_section(portfolio_metrics),
"",
]
def _generate_alerts_section(
self, analyses: Dict[str, TickerAnalysis]
) -> List[str]:
"""Generate active alerts section.
Args:
analyses: All analysis results
Returns:
List of section lines (empty if no alerts)
"""
all_alerts = [
a
for analysis in analyses.values()
if not analysis.error
for a in analysis.alerts
]
if not all_alerts:
return []
section = ["## 🚨 Active Alerts"]
for alert in all_alerts:
section.append(f"- {alert}")
section.append("")
return section
def _generate_failures_section(
self, analyses: Dict[str, TickerAnalysis]
) -> List[str]:
"""Generate data quality notes for failed analyses.
Args:
analyses: All analysis results
Returns:
List of section lines (empty if no failures)
"""
failures = {t: a for t, a in analyses.items() if a.error}
if not failures:
return []
section = ["## ⚠️ Data Quality Notes"]
for ticker, analysis in failures.items():
section.append(f"- **{ticker}:** {analysis.error}")
section.append("")
return section
def generate_detailed_report(
self,
analyses: Dict[str, TickerAnalysis],
benchmark_analysis: Optional[TickerAnalysis],
portfolio_metrics: Optional[PortfolioMetrics],
period: str,
comparison_chart_path: Optional[Path] = None,
) -> str:
"""
Generates a comprehensive financial report with an LLM-powered narrative.
Args:
analyses (Dict[str, TickerAnalysis]): A dictionary of ticker analyses.
benchmark_analysis (Optional[TickerAnalysis]): The benchmark analysis.
portfolio_metrics (Optional[PortfolioMetrics]): The portfolio metrics.
period (str): The analysis period.
Returns:
str: The complete markdown report.
"""
report = []
# Header
report.extend(self._generate_report_header())
# Executive Summary
report.extend(self._generate_executive_summary_section(analyses, period))
# Portfolio Overview (if available)
report.extend(self._generate_portfolio_overview_section(portfolio_metrics))
# Key Metrics Table
report.extend(self._generate_metrics_section(analyses))
# Fundamental Analysis
report.append("## 📊 Fundamental Analysis")
report.append(self._generate_fundamental_section(analyses))
report.append("")
# Individual Analysis
report.append("## 📋 Detailed Stock Analysis")
report.append(self._generate_individual_analysis(analyses))
report.append("")
# Charts Section
report.extend(self._generate_charts_section(analyses, comparison_chart_path))
# Options Analysis Section (if available)
report.extend(self._generate_options_section(analyses))
# Risk Analysis
report.append("## ⚠️ Risk Analysis")
report.append(self._generate_risk_analysis(analyses))
report.append("")
# Recommendations
report.append("## 💡 Investment Recommendations")
report.append(self._generate_recommendations(analyses))
report.append("")
# Alerts (if any)
report.extend(self._generate_alerts_section(analyses))
# Failures (if any)
report.extend(self._generate_failures_section(analyses))
return "\n".join(report)
def _generate_portfolio_section(self, portfolio_metrics: PortfolioMetrics) -> str:
"""Generate portfolio overview section."""
lines = []
lines.append(f"**Total Value:** ${portfolio_metrics.total_value:,.2f}")
lines.append(
f"**Portfolio Return:** {portfolio_metrics.portfolio_return*100:.2f}%"
)
lines.append(
f"**Portfolio Volatility:** {portfolio_metrics.portfolio_volatility*100:.2f}%"
)
if portfolio_metrics.portfolio_sharpe:
lines.append(
f"**Portfolio Sharpe Ratio:** {portfolio_metrics.portfolio_sharpe:.2f}"
)
if portfolio_metrics.diversification_ratio:
lines.append(
f"**Diversification Ratio:** {portfolio_metrics.diversification_ratio:.2f}"
)
if portfolio_metrics.concentration_risk:
lines.append(
f"**Concentration Risk (HHI):** {portfolio_metrics.concentration_risk:.3f}"
)
if portfolio_metrics.top_contributors:
lines.append("\n**Top Contributors:**")
for ticker, contribution in portfolio_metrics.top_contributors[:3]:
lines.append(
f"- {ticker}: {contribution*100:.2f}% contribution to return"
)
return "\n".join(lines)
def _generate_metrics_section(self, analyses: Dict[str, TickerAnalysis]) -> List[str]:
"""Generate complete metrics section with table and methodology notes.
Args:
analyses: All analysis results
Returns:
List of section lines
"""
section = [
"## 📈 Key Performance Metrics",
self._generate_metrics_table(analyses),
"",
]
section.extend(self._generate_methodology_notes())
section.append("")
return section
def _generate_methodology_notes(self) -> List[str]:
"""Generate methodology notes explaining metrics.
Returns:
List of methodology note lines
"""
return [
"### Methodology Notes",
"",
"**Returns and Volatility (Daily Metrics):**",
"- **Return (Daily %):** Average daily return from historical price data. These are DAILY figures, not annualized.",
"- **Vol (Daily %):** Standard deviation of daily returns. This is a DAILY figure, not annualized.",
"- **Annualization Method:** To convert daily metrics to annual: Returns use geometric compounding (1 + daily_return)^252 - 1. Volatility is multiplied by √252 ≈ 15.87.",
"",
"**Risk-Adjusted Metrics:**",
"- **Sharpe Ratio:** Risk-adjusted return metric calculated as (annualized return - risk-free rate) / annualized volatility. Values >1 indicate good risk-adjusted performance, >2 is excellent. This metric is already annualized.",
"- **Max DD (%):** Maximum peak-to-trough decline during the analysis period (shown as negative percentage).",
"- **VaR (95%):** Value at Risk at 95% confidence level - daily potential loss in worst 5% of cases (shown as negative percentage). Based on 5th percentile of historical daily returns distribution.",
"",
"**Technical and Fundamental Metrics:**",
"- **RSI:** Relative Strength Index (0-100 scale). Interpretation: <30 = oversold, 30-45 = weak, 45-55 = neutral, 55-70 = bullish, >70 = overbought.",
"- **P/E (TTM):** Price-to-Earnings ratio based on trailing twelve months. Higher values indicate higher valuation relative to earnings.",
"- **P/B:** Price-to-Book ratio. Higher values (e.g., >5) indicate high valuation relative to book value, not compression.",
"- **Rev Growth (YoY %):** Year-over-year revenue growth rate from quarterly financial statements.",
"",
"**Portfolio Metrics:**",
"- **Diversification Ratio:** Ratio of weighted average volatility to portfolio volatility. Values >1 indicate diversification benefit.",
"- **Concentration Risk (HHI):** Herfindahl-Hirschman Index based on portfolio weights. Values closer to 1 indicate higher concentration, closer to 0 indicate better diversification.",
"",
"**Calculation Assumptions:** All metrics assume 252 trading days per year and a risk-free rate of 2% (annualized).",
]
def _generate_metrics_table(self, analyses: Dict[str, TickerAnalysis]) -> str:
"""Generate comprehensive metrics table."""
table = [
"| Ticker | Price ($) | Return (Daily %) | Vol (Daily %) | Sharpe | Max DD (%) | RSI | P/E (TTM) | Rev Growth (YoY %) |",
"|--------|-----------|------------------|---------------|--------|------------|-----|-----------|---------------------|",
]
for ticker, analysis in analyses.items():
if analysis.error:
continue
metrics = analysis.advanced_metrics
indicators = analysis.technical_indicators
ratios = analysis.ratios
fundamentals = analysis.fundamentals
# Safe conversions with NaN checks
sharpe_str = (
f"{metrics.sharpe_ratio:.2f}"
if metrics.sharpe_ratio is not None
and not pd.isna(metrics.sharpe_ratio)
else "N/A"
)
dd_val = (
metrics.max_drawdown
if metrics.max_drawdown is not None
and not pd.isna(metrics.max_drawdown)
else 0
)
dd_str = f"{dd_val*100:.1f}%"
rsi_str = (
f"{indicators.rsi:.0f}"
if indicators.rsi is not None and not pd.isna(indicators.rsi)
else "N/A"
)
pe_str = (
f"{ratios.get('pe_ratio'):.1f}"
if ratios.get("pe_ratio") and not pd.isna(ratios.get("pe_ratio"))
else "N/A"
)
# Check for valid revenue_growth (not None, not NaN)
has_growth = (
fundamentals
and fundamentals.revenue_growth is not None
and not pd.isna(fundamentals.revenue_growth)
)
growth_str = (
f"{fundamentals.revenue_growth*100:.1f}%" if has_growth else "N/A"
)
table.append(
f"| {ticker} | "
f"${analysis.latest_close:.2f} | "
f"{analysis.avg_daily_return*100:.2f}% | "
f"{analysis.volatility*100:.1f}% | "
f"{sharpe_str} | "
f"{dd_str} | "
f"{rsi_str} | "
f"{pe_str} | "
f"{growth_str} |"
)
return "\n".join(table)
def _generate_fundamental_section(self, analyses: Dict[str, TickerAnalysis]) -> str:
"""Generate fundamental analysis section."""
lines = []
for ticker, analysis in analyses.items():
if analysis.error or not analysis.fundamentals:
continue
fundamentals = analysis.fundamentals
if not any(
[
fundamentals.revenue,
fundamentals.net_income,
fundamentals.revenue_growth,
]
):
continue
lines.append(f"### {ticker}")
if fundamentals.revenue:
lines.append(f"- **Revenue:** ${fundamentals.revenue/1e9:.2f}B")
if fundamentals.net_income:
lines.append(f"- **Net Income:** ${fundamentals.net_income/1e9:.2f}B")
if fundamentals.revenue_growth:
lines.append(
f"- **Revenue Growth (YoY):** {fundamentals.revenue_growth*100:.1f}%"
)
if fundamentals.earnings_growth:
lines.append(
f"- **Earnings Growth (YoY):** {fundamentals.earnings_growth*100:.1f}%"
)
if fundamentals.free_cash_flow:
lines.append(
f"- **Free Cash Flow:** ${fundamentals.free_cash_flow/1e9:.2f}B"
)
lines.append("")
return "\n".join(lines) if lines else "Limited fundamental data available."
def _generate_charts_section(
self,
analyses: Dict[str, TickerAnalysis],
comparison_chart_path: Optional[Path] = None,
) -> List[str]:
"""Generate charts section with embedded images.
Args:
analyses: All analysis results
comparison_chart_path: Path to comparison chart (optional)