Spaces:
Sleeping
Sleeping
Update sentiment_analyzer.py
Browse files- sentiment_analyzer.py +37 -38
sentiment_analyzer.py
CHANGED
|
@@ -13,52 +13,51 @@ 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 |
sources = self.gold_sources
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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,
|
| 51 |
selected_news = random.sample(sources, num_news)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
news_html
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
"🟡"
|
| 60 |
-
news_html += f"<p style='margin: 10px 0; padding: 10px; background:
|
| 61 |
-
|
| 62 |
news_html += "</div>"
|
| 63 |
|
| 64 |
return sentiment, news_html
|
|
|
|
| 13 |
"Technical breakout above resistance level",
|
| 14 |
"Profit-taking observed after recent rally"
|
| 15 |
]
|
| 16 |
+
self.crypto_sources = [
|
| 17 |
+
"Fed rate hike fear drives BTC sell-off",
|
| 18 |
+
"Institutional adoption pushes Bitcoin price up",
|
| 19 |
+
"Whale wallets show large accumulation activity",
|
| 20 |
+
"Regulatory uncertainty weighs on crypto market",
|
| 21 |
+
"New protocol launch fuels altcoin rally",
|
| 22 |
+
"ETF approval anticipation creates bullish momentum",
|
| 23 |
+
"High funding rates suggest market overheating",
|
| 24 |
+
"Tether minting correlates with short-term pumps"
|
| 25 |
]
|
| 26 |
|
| 27 |
+
def analyze_market_sentiment(self, ticker):
|
| 28 |
+
"""Analyze sentiment for a given market (Simulated)"""
|
| 29 |
try:
|
| 30 |
+
# PENTING: Untuk sentimen riil, Anda harus mengintegrasikan API Berita Finansial (misalnya Finnhub, MarketAux)
|
| 31 |
+
# dan model NLP (misalnya BERT/Transformer) untuk menganalisis berita secara aktual.
|
| 32 |
+
|
| 33 |
+
# Memilih sumber data dan warna berdasarkan Ticker
|
| 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 |
+
title_color = "#FFD700"
|
| 42 |
+
|
| 43 |
+
# Simulasi analisis sentimen
|
| 44 |
+
sentiment = base_sentiment + random.uniform(-0.2, 0.2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
sentiment = max(-1, min(1, sentiment))
|
| 46 |
+
|
| 47 |
# Generate news summary
|
| 48 |
+
num_news = random.randint(3, 5)
|
| 49 |
selected_news = random.sample(sources, num_news)
|
| 50 |
+
|
| 51 |
+
# Tampilan News (menggunakan background terang #E0E0E0 agar terlihat di tema putih)
|
| 52 |
+
news_html = "<div style='max-height: 200px; overflow-y: auto; color: black;'>"
|
| 53 |
+
news_html += f"<h4 style='color: {title_color};'>Latest {ticker} News (Simulated)</h4>"
|
| 54 |
+
|
| 55 |
+
for news in selected_news:
|
| 56 |
+
sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news or "bullish" in news or "accumulation" in news else \
|
| 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: #E0E0E0; border-radius: 5px; color: black;'>{sentiment_label} {news}</p>"
|
| 60 |
+
|
| 61 |
news_html += "</div>"
|
| 62 |
|
| 63 |
return sentiment, news_html
|