Spaces:
Sleeping
Sleeping
Update sentiment_analyzer.py
Browse files- sentiment_analyzer.py +38 -37
sentiment_analyzer.py
CHANGED
|
@@ -13,51 +13,52 @@ class SentimentAnalyzer:
|
|
| 13 |
"Technical breakout above resistance level",
|
| 14 |
"Profit-taking observed after recent rally"
|
| 15 |
]
|
| 16 |
-
self.
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
"
|
| 24 |
-
"
|
| 25 |
]
|
| 26 |
|
| 27 |
-
def
|
| 28 |
-
"""Analyze sentiment for
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
if ticker == "BTC-USD":
|
| 35 |
-
base_sentiment = random.uniform(-0.3, 0.7)
|
| 36 |
-
sources = self.crypto_sources
|
| 37 |
-
title_color = "#FFA500"
|
| 38 |
-
else: # GC=F
|
| 39 |
-
base_sentiment = random.uniform(-0.5, 0.5)
|
| 40 |
sources = self.gold_sources
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
sentiment = max(-1, min(1, sentiment))
|
| 46 |
-
|
| 47 |
# Generate news summary
|
| 48 |
-
num_news = random.randint(3,
|
| 49 |
selected_news = random.sample(sources, num_news)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
news_html
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
"🔴" if "sell-off" in news or "weighs" in news or "outflows" in news or "Profit-taking" in news or "fear" in news else \
|
| 58 |
"🟡"
|
| 59 |
-
news_html += f"<p style='margin: 10px 0; padding: 10px; background:
|
| 60 |
-
|
| 61 |
news_html += "</div>"
|
| 62 |
|
| 63 |
return sentiment, news_html
|
|
|
|
| 13 |
"Technical breakout above resistance level",
|
| 14 |
"Profit-taking observed after recent rally"
|
| 15 |
]
|
| 16 |
+
self.bitcoin_sources = [
|
| 17 |
+
"Institutional adoption of Bitcoin accelerates",
|
| 18 |
+
"Regulatory clarity improves - positive for crypto",
|
| 19 |
+
"Bitcoin halving event supports price",
|
| 20 |
+
"Macro uncertainty drives Bitcoin demand",
|
| 21 |
+
"Spot ETF inflows reach record highs",
|
| 22 |
+
"Network hash rate reaches new ATH",
|
| 23 |
+
"Whale accumulation detected on-chain",
|
| 24 |
+
"DeFi TVL growth supports crypto market"
|
| 25 |
]
|
| 26 |
|
| 27 |
+
def analyze_sentiment(self, asset_name):
|
| 28 |
+
"""Analyze sentiment for selected asset"""
|
| 29 |
try:
|
| 30 |
+
# Select appropriate news sources
|
| 31 |
+
if "Bitcoin" in asset_name:
|
| 32 |
+
sources = self.bitcoin_sources
|
| 33 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
sources = self.gold_sources
|
| 35 |
+
|
| 36 |
+
# Generate random sentiment around current market conditions
|
| 37 |
+
base_sentiment = random.uniform(-0.5, 0.5)
|
| 38 |
+
|
| 39 |
+
# Add some realistic variation
|
| 40 |
+
if random.random() > 0.7:
|
| 41 |
+
# Strong sentiment event
|
| 42 |
+
sentiment = base_sentiment + random.uniform(-0.5, 0.5)
|
| 43 |
+
else:
|
| 44 |
+
sentiment = base_sentiment
|
| 45 |
+
|
| 46 |
+
# Clamp between -1 and 1
|
| 47 |
sentiment = max(-1, min(1, sentiment))
|
| 48 |
+
|
| 49 |
# Generate news summary
|
| 50 |
+
num_news = random.randint(3, 6)
|
| 51 |
selected_news = random.sample(sources, num_news)
|
| 52 |
+
|
| 53 |
+
news_html = f"<div style='max-height: 300px; overflow-y: auto;'>"
|
| 54 |
+
news_html += f"<h4 style='color: #4169E1;'>{asset_name} Market News</h4>"
|
| 55 |
+
|
| 56 |
+
for i, news in enumerate(selected_news, 1):
|
| 57 |
+
sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news or "accelerates" in news or " ATH" in news else \
|
| 58 |
+
"🔴" if "weighs" in news or "outflows" in news or "Profit-taking" in news else \
|
|
|
|
| 59 |
"🟡"
|
| 60 |
+
news_html += f"<p style='margin: 10px 0; padding: 10px; background: rgba(65,105,225,0.05); border-radius: 5px;'>{sentiment_label} {news}</p>"
|
| 61 |
+
|
| 62 |
news_html += "</div>"
|
| 63 |
|
| 64 |
return sentiment, news_html
|