Spaces:
Runtime error
Runtime error
Update services/chart_analysis.py
Browse files- services/chart_analysis.py +91 -43
services/chart_analysis.py
CHANGED
|
@@ -5,63 +5,111 @@ from datetime import datetime
|
|
| 5 |
from utils.file_handlers import save_chat_history
|
| 6 |
from utils.prompts import create_analysis_prompt
|
| 7 |
|
| 8 |
-
|
| 9 |
-
def __init__(self, claude_service):
|
| 10 |
-
self.claude_service = claude_service
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
prompt = create_analysis_prompt(patterns, indicators)
|
| 21 |
-
|
| 22 |
-
# Get analysis from Claude
|
| 23 |
analysis = self.claude_service.analyze_image(image_data, prompt)
|
| 24 |
|
| 25 |
if analysis:
|
| 26 |
return {
|
| 27 |
'timestamp': datetime.now().isoformat(),
|
| 28 |
'chart_type': chart_type,
|
| 29 |
-
'analysis': analysis
|
|
|
|
| 30 |
}
|
| 31 |
return None
|
| 32 |
except Exception as e:
|
| 33 |
st.error(f"Error in chart analysis: {str(e)}")
|
| 34 |
return None
|
| 35 |
|
| 36 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from utils.file_handlers import save_chat_history
|
| 6 |
from utils.prompts import create_analysis_prompt
|
| 7 |
|
| 8 |
+
# services/chart_analysis.py
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
class ChartAnalysisService:
|
| 11 |
+
def analyze_multiple_charts(self, image_data_list, patterns, indicators, comparison_type="Individual Analysis"):
|
| 12 |
+
"""Analyze multiple charts and provide comprehensive analysis"""
|
| 13 |
try:
|
| 14 |
+
results = []
|
| 15 |
+
individual_analyses = []
|
| 16 |
+
|
| 17 |
+
# First get individual analyses
|
| 18 |
+
for idx, image_data in enumerate(image_data_list):
|
| 19 |
+
chart_type = self.claude_service.detect_chart_type(image_data)
|
| 20 |
+
st.info(f"Chart {idx + 1} detected as: {chart_type}")
|
| 21 |
+
|
| 22 |
+
analysis = self.analyze_single_chart(image_data, patterns, indicators, chart_type)
|
| 23 |
+
if analysis:
|
| 24 |
+
individual_analyses.append(analysis)
|
| 25 |
+
|
| 26 |
+
# If we have multiple charts, perform comparison analysis
|
| 27 |
+
if len(individual_analyses) > 1 and comparison_type != "Individual Analysis":
|
| 28 |
+
comparison_prompt = self.create_comparison_prompt(
|
| 29 |
+
individual_analyses,
|
| 30 |
+
comparison_type,
|
| 31 |
+
patterns,
|
| 32 |
+
indicators
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Combine all images for the comparison analysis
|
| 36 |
+
comparative_analysis = self.claude_service.analyze_multiple_images(
|
| 37 |
+
image_data_list,
|
| 38 |
+
comparison_prompt
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
if comparative_analysis:
|
| 42 |
+
results.append({
|
| 43 |
+
'timestamp': datetime.now().isoformat(),
|
| 44 |
+
'analysis_type': comparison_type,
|
| 45 |
+
'analysis': comparative_analysis,
|
| 46 |
+
'num_charts': len(image_data_list)
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
# Add individual analyses
|
| 50 |
+
results.extend(individual_analyses)
|
| 51 |
+
return results
|
| 52 |
+
except Exception as e:
|
| 53 |
+
st.error(f"Error in multiple chart analysis: {str(e)}")
|
| 54 |
+
return None
|
| 55 |
|
| 56 |
+
def analyze_single_chart(self, image_data, patterns, indicators, chart_type=None):
|
| 57 |
+
"""Analyze a single chart"""
|
| 58 |
+
try:
|
| 59 |
prompt = create_analysis_prompt(patterns, indicators)
|
|
|
|
|
|
|
| 60 |
analysis = self.claude_service.analyze_image(image_data, prompt)
|
| 61 |
|
| 62 |
if analysis:
|
| 63 |
return {
|
| 64 |
'timestamp': datetime.now().isoformat(),
|
| 65 |
'chart_type': chart_type,
|
| 66 |
+
'analysis': analysis,
|
| 67 |
+
'analysis_type': 'Individual'
|
| 68 |
}
|
| 69 |
return None
|
| 70 |
except Exception as e:
|
| 71 |
st.error(f"Error in chart analysis: {str(e)}")
|
| 72 |
return None
|
| 73 |
|
| 74 |
+
def create_comparison_prompt(self, individual_analyses, comparison_type, patterns, indicators):
|
| 75 |
+
"""Create a prompt for comparing multiple charts"""
|
| 76 |
+
if comparison_type == "Correlated Analysis":
|
| 77 |
+
return """Analyze these charts together and provide insights on their relationships:
|
| 78 |
+
1. Correlation Analysis
|
| 79 |
+
- Identify any price correlations
|
| 80 |
+
- Note common patterns or divergences
|
| 81 |
+
- Highlight synchronized movements
|
| 82 |
|
| 83 |
+
2. Relative Strength Comparison
|
| 84 |
+
- Compare relative performance
|
| 85 |
+
- Identify leader/laggard relationships
|
| 86 |
+
- Note any rotation patterns
|
| 87 |
+
|
| 88 |
+
3. Market Implications
|
| 89 |
+
- What these relationships suggest
|
| 90 |
+
- Potential trading opportunities
|
| 91 |
+
- Risk considerations
|
| 92 |
+
|
| 93 |
+
4. Summary
|
| 94 |
+
Provide a clear summary of the key relationships and trading implications.
|
| 95 |
+
"""
|
| 96 |
+
else: # Market Trend Analysis
|
| 97 |
+
return """Analyze these charts to identify broader market patterns:
|
| 98 |
+
1. Market Trend Analysis
|
| 99 |
+
- Identify common trend directions
|
| 100 |
+
- Note sector/market momentum
|
| 101 |
+
- Highlight any broader pattern formations
|
| 102 |
+
|
| 103 |
+
2. Market Condition Assessment
|
| 104 |
+
- Overall market health
|
| 105 |
+
- Risk levels
|
| 106 |
+
- Potential market turning points
|
| 107 |
+
|
| 108 |
+
3. Trading Implications
|
| 109 |
+
- Sector rotation opportunities
|
| 110 |
+
- Risk management considerations
|
| 111 |
+
- Timeframe recommendations
|
| 112 |
+
|
| 113 |
+
4. Summary
|
| 114 |
+
Provide a clear summary of the market condition and trading implications.
|
| 115 |
+
"""
|