File size: 3,067 Bytes
b8086d5
 
 
 
 
f27fbb4
b8086d5
 
 
 
 
 
 
 
 
7df1b11
 
 
 
 
 
 
 
 
f27fbb4
b8086d5
7df1b11
 
b8086d5
7df1b11
 
 
 
3e2642e
7df1b11
 
 
 
 
 
 
 
 
 
 
 
b8086d5
7df1b11
b8086d5
7df1b11
f27fbb4
7df1b11
 
 
 
 
 
 
b8086d5
7df1b11
 
b8086d5
 
 
 
 
e3e2069
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
import random
from datetime import datetime

class SentimentAnalyzer:
    def __init__(self):
        self.gold_sources = [
            "Federal Reserve hints at rate pause - positive for gold",
            "Inflation data higher than expected - gold demand rising",
            "Dollar strength weighs on precious metals",
            "Central banks continue gold accumulation",
            "Geopolitical tensions support safe-haven demand",
            "Gold ETFs see outflows amid risk-on sentiment",
            "Technical breakout above resistance level",
            "Profit-taking observed after recent rally"
        ]
        self.bitcoin_sources = [
            "Institutional adoption of Bitcoin accelerates",
            "Regulatory clarity improves - positive for crypto",
            "Bitcoin halving event supports price",
            "Macro uncertainty drives Bitcoin demand",
            "Spot ETF inflows reach record highs",
            "Network hash rate reaches new ATH",
            "Whale accumulation detected on-chain",
            "DeFi TVL growth supports crypto market"
        ]
    
    def analyze_sentiment(self, asset_name):
        """Analyze sentiment for selected asset"""
        try:
            # Select appropriate news sources
            if "Bitcoin" in asset_name:
                sources = self.bitcoin_sources
            else:
                sources = self.gold_sources
                        
            # Generate random sentiment around current market conditions
            base_sentiment = random.uniform(-0.5, 0.5)
                        
            # Add some realistic variation
            if random.random() > 0.7:
                # Strong sentiment event
                sentiment = base_sentiment + random.uniform(-0.5, 0.5)
            else:
                sentiment = base_sentiment

            # Clamp between -1 and 1
            sentiment = max(-1, min(1, sentiment))
                        
            # Generate news summary
            num_news = random.randint(3, 6)
            selected_news = random.sample(sources, num_news)
                        
            news_html = f"<div style='max-height: 300px; overflow-y: auto;'>"
            news_html += f"<h4 style='color: #4169E1;'>{asset_name} Market News</h4>"
                        
            for i, news in enumerate(selected_news, 1):
                sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news or "accelerates" in news or " ATH" in news else \
                                  "🔴" if "weighs" in news or "outflows" in news or "Profit-taking" in news else \
                                  "🟡"
                news_html += f"<p style='margin: 10px 0; padding: 10px; background: rgba(65,105,225,0.05); border-radius: 5px;'>{sentiment_label} {news}</p>"
                        
            news_html += "</div>"
            
            return sentiment, news_html
            
        except Exception as e:
            return 0, f"<p>Error analyzing sentiment: {str(e)}</p>"