File size: 5,027 Bytes
06d59d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```python
from typing import Dict, Any
from dataclasses import dataclass
from .models_registry import ModelsRegistry
import random

@dataclass
class SignalComponents:
    trend_score: float  # -1 to +1
    momentum_score: float  # -1 to +1
    pattern_score: float  # -1 to +1
    sentiment_score: float  # -1 to +1
    volatility: float  # 0-1

class SignalEngine:
    def __init__(self, models: ModelsRegistry):
        self.models = models

    def generate_signal(self, components: SignalComponents) -> Dict[str, Any]:
        """Combine all analysis components into a final signal"""
        # Weighted average of scores
        final_score = (
            0.4 * components.trend_score +
            0.3 * components.momentum_score +
            0.2 * components.pattern_score +
            0.1 * components.sentiment_score
        )
        
        # Determine direction and confidence
        if abs(final_score) < 0.2:
            direction = "NEUTRAL"
            confidence = max(10, int(50 + 50 * abs(final_score) / 0.2))
        else:
            direction = "LONG" if final_score > 0 else "SHORT"
            confidence = int((abs(final_score) - 0.2) / 0.8 * 90) + 10
        
        # Calculate levels based on volatility
        base_price = 42000  # In real app, this would come from market data
        atr = components.volatility * base_price * 0.05
        
        if direction == "LONG":
            entry_min = base_price - atr * 0.5
            entry_max = base_price
            stop_loss = base_price - atr * 1.5
            take_profit = [
                base_price + atr * 2,
                base_price + atr * 3
            ]
        elif direction == "SHORT":
            entry_min = base_price
            entry_max = base_price + atr * 0.5
            stop_loss = base_price + atr * 1.5
            take_profit = [
                base_price - atr * 2,
                base_price - atr * 3
            ]
        else:  # NEUTRAL
            entry_min = base_price - atr * 0.2
            entry_max = base_price + atr * 0.2
            stop_loss = base_price + atr * 2 * (-1 if random.random() > 0.5 else 1)
            take_profit = []
        
        # Generate explanation based on components
        explanation_parts = []
        if components.trend_score > 0.3:
            explanation_parts.append("Strong bullish trend detected.")
        elif components.trend_score < -0.3:
            explanation_parts.append("Strong bearish trend detected.")
            
        if components.momentum_score > 0.4:
            explanation_parts.append("Momentum favors upside.")
        elif components.momentum_score < -0.4:
            explanation_parts.append("Momentum favors downside.")
            
        if components.pattern_score > 0.5:
            explanation_parts.append("Bullish chart pattern identified.")
        elif components.pattern_score < -0.5:
            explanation_parts.append("Bearish chart pattern identified.")
            
        if not explanation_parts:
            explanation_parts.append("No strong signals detected - market may be ranging.")
            
        explanation = " ".join(explanation_parts)
        
        return {
            "direction": direction,
            "entry_zone": [entry_min, entry_max],
            "stop_loss": stop_loss,
            "take_profit_levels": take_profit,
            "confidence": confidence,
            "explanation": explanation
        }

    async def analyze_market(self, pair: str, timeframe: str) -> Dict[str, Any]:
        """Analyze market conditions and generate signal"""
        # In a real implementation, we would:
        # 1. Fetch market data
        # 2. Call time-series models
        # 3. Calculate technical indicators
        # 4. Get sentiment if available
        
        # Mock analysis for demo purposes
        components = SignalComponents(
            trend_score=random.uniform(-1, 1),
            momentum_score=random.uniform(-0.5, 0.8),
            pattern_score=random.uniform(-1, 1),
            sentiment_score=random.uniform(-0.3, 0.3),
            volatility=random.uniform(0.2, 0.8)
        )
        
        return self.generate_signal(components)

    async def analyze_screenshot(self, image_data: bytes) -> Dict[str, Any]:
        """Analyze uploaded chart screenshot"""
        # In a real implementation, we would:
        # 1. Process image with vision model
        # 2. Extract text with OCR
        # 3. Analyze patterns and generate signal
        
        # Mock analysis for demo purposes
        components = SignalComponents(
            trend_score=random.uniform(0, 1),  # Screenshots more likely to be long ideas
            momentum_score=random.uniform(0, 0.9),
            pattern_score=random.uniform(0.1, 1),
            sentiment_score=random.uniform(0, 0.5),
            volatility=random.uniform(0.3, 0.9)
        )
        
        return self.generate_signal(components)
```

Let me continue with the frontend setup and other necessary files: