Spaces:
Build error
Build error
Update Gradio app with multiple files
Browse files- app.py +93 -35
- data_processor.py +18 -6
- requirements.txt +1 -1
- sentiment_analyzer.py +11 -3
- trading_logic.py +3 -1
app.py
CHANGED
|
@@ -35,7 +35,11 @@ def create_chart_analysis(ticker, interval):
|
|
| 35 |
# Buat chart menggunakan MPLFINANCE (dikembalikan sebagai HTML)
|
| 36 |
chart_html = create_mplfinance_chart(
|
| 37 |
df,
|
| 38 |
-
ticker=f'{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
predictions=predictions
|
| 40 |
)
|
| 41 |
|
|
@@ -52,20 +56,35 @@ def create_chart_analysis(ticker, interval):
|
|
| 52 |
# Create metrics display
|
| 53 |
metrics = {
|
| 54 |
"Ticker": ticker,
|
| 55 |
-
|
|
|
|
| 56 |
"Signal": signal.upper(),
|
| 57 |
-
"Confidence": f"{
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
}
|
| 64 |
|
| 65 |
return chart_html, metrics
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
-
return f"Error creating chart: {
|
|
|
|
|
|
|
| 69 |
|
| 70 |
def analyze_sentiment(ticker):
|
| 71 |
"""Analyze gold/crypto market sentiment"""
|
|
@@ -77,19 +96,33 @@ def analyze_sentiment(ticker):
|
|
| 77 |
fig = go.Figure(go.Indicator(
|
| 78 |
mode="gauge+number+delta",
|
| 79 |
value=sentiment_score,
|
| 80 |
-
domain={
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
gauge={
|
| 84 |
-
'axis': {'range': [-1, 1]
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
'steps': [
|
| 87 |
-
{
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
{'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"}
|
| 90 |
],
|
| 91 |
'threshold': {
|
| 92 |
-
'line': {'color': "black", 'width': 4
|
|
|
|
| 93 |
'thickness': 0.75,
|
| 94 |
'value': 0
|
| 95 |
}
|
|
@@ -132,12 +165,19 @@ def get_fundamentals(ticker):
|
|
| 132 |
fig = go.Figure(go.Indicator(
|
| 133 |
mode="gauge+number",
|
| 134 |
value=gauge_value,
|
| 135 |
-
title={
|
|
|
|
|
|
|
| 136 |
gauge={
|
| 137 |
-
'axis': {'range': gauge_range
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
'steps': [
|
| 140 |
-
{
|
|
|
|
|
|
|
| 141 |
{'range': [gauge_range[1] * 0.3, gauge_range[1] * 0.7], 'color': "rgba(100,100,100,0.3)"},
|
| 142 |
{'range': [gauge_range[1] * 0.7, gauge_range[1]], 'color': "rgba(0,255,0,0.5)"}
|
| 143 |
]
|
|
@@ -162,22 +202,40 @@ with gr.Blocks(
|
|
| 162 |
theme=gr.themes.Default(primary_hue="yellow", secondary_hue="yellow"),
|
| 163 |
title="Ultimate Market Analysis & Prediction",
|
| 164 |
css="""
|
| 165 |
-
.gradio-container {
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
.gr-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
.mpl-chart-container {
|
| 173 |
border: 1px solid #CCCCCC;
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
.chart-title {
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
"""
|
| 182 |
) as demo:
|
| 183 |
|
|
|
|
| 35 |
# Buat chart menggunakan MPLFINANCE (dikembalikan sebagai HTML)
|
| 36 |
chart_html = create_mplfinance_chart(
|
| 37 |
df,
|
| 38 |
+
ticker=f'{
|
| 39 |
+
ticker
|
| 40 |
+
} ({
|
| 41 |
+
interval
|
| 42 |
+
})',
|
| 43 |
predictions=predictions
|
| 44 |
)
|
| 45 |
|
|
|
|
| 56 |
# Create metrics display
|
| 57 |
metrics = {
|
| 58 |
"Ticker": ticker,
|
| 59 |
+
"Current Price": f"${current_price:.2f
|
| 60 |
+
}",
|
| 61 |
"Signal": signal.upper(),
|
| 62 |
+
"Confidence": f"{
|
| 63 |
+
confidence:.1%
|
| 64 |
+
}",
|
| 65 |
+
"Take Profit": f"${
|
| 66 |
+
tp:.2f
|
| 67 |
+
}" if tp else "N/A",
|
| 68 |
+
"Stop Loss": f"${
|
| 69 |
+
sl:.2f
|
| 70 |
+
}" if sl else "N/A",
|
| 71 |
+
"RSI": f"{
|
| 72 |
+
df['RSI'].iloc[-1]:.1f
|
| 73 |
+
}",
|
| 74 |
+
"MACD": f"{
|
| 75 |
+
df['MACD'].iloc[-1]:.4f
|
| 76 |
+
}",
|
| 77 |
+
"Volume": f"{
|
| 78 |
+
df['Volume'].iloc[-1]:,.0f
|
| 79 |
+
}"
|
| 80 |
}
|
| 81 |
|
| 82 |
return chart_html, metrics
|
| 83 |
|
| 84 |
except Exception as e:
|
| 85 |
+
return f"Error creating chart: {
|
| 86 |
+
e
|
| 87 |
+
}", None
|
| 88 |
|
| 89 |
def analyze_sentiment(ticker):
|
| 90 |
"""Analyze gold/crypto market sentiment"""
|
|
|
|
| 96 |
fig = go.Figure(go.Indicator(
|
| 97 |
mode="gauge+number+delta",
|
| 98 |
value=sentiment_score,
|
| 99 |
+
domain={
|
| 100 |
+
'x': [0, 1], 'y': [0, 1]
|
| 101 |
+
},
|
| 102 |
+
title={
|
| 103 |
+
'text': f"{ticker
|
| 104 |
+
} Market Sentiment (Simulated)"},
|
| 105 |
+
delta={
|
| 106 |
+
'reference': 0
|
| 107 |
+
},
|
| 108 |
gauge={
|
| 109 |
+
'axis': {'range': [-1, 1]
|
| 110 |
+
},
|
| 111 |
+
'bar': {
|
| 112 |
+
'color': "#FFD700"
|
| 113 |
+
},
|
| 114 |
'steps': [
|
| 115 |
+
{
|
| 116 |
+
'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
'range': [gauge_range[1] * 0.7, gauge_range[1]], 'color': "rgba(0,255,0,0.5)"
|
| 120 |
+
},
|
| 121 |
{'range': [0.5, 1], 'color': "rgba(0,255,0,0.5)"}
|
| 122 |
],
|
| 123 |
'threshold': {
|
| 124 |
+
'line': {'color': "black", 'width': 4
|
| 125 |
+
},
|
| 126 |
'thickness': 0.75,
|
| 127 |
'value': 0
|
| 128 |
}
|
|
|
|
| 165 |
fig = go.Figure(go.Indicator(
|
| 166 |
mode="gauge+number",
|
| 167 |
value=gauge_value,
|
| 168 |
+
title={
|
| 169 |
+
'text': gauge_title
|
| 170 |
+
},
|
| 171 |
gauge={
|
| 172 |
+
'axis': {'range': gauge_range
|
| 173 |
+
},
|
| 174 |
+
'bar': {
|
| 175 |
+
'color': "#FFD700"
|
| 176 |
+
},
|
| 177 |
'steps': [
|
| 178 |
+
{
|
| 179 |
+
'range': [gauge_range[0], gauge_range[1] * 0.3], 'color': "rgba(255,0,0,0.5)"
|
| 180 |
+
},
|
| 181 |
{'range': [gauge_range[1] * 0.3, gauge_range[1] * 0.7], 'color': "rgba(100,100,100,0.3)"},
|
| 182 |
{'range': [gauge_range[1] * 0.7, gauge_range[1]], 'color': "rgba(0,255,0,0.5)"}
|
| 183 |
]
|
|
|
|
| 202 |
theme=gr.themes.Default(primary_hue="yellow", secondary_hue="yellow"),
|
| 203 |
title="Ultimate Market Analysis & Prediction",
|
| 204 |
css="""
|
| 205 |
+
.gradio-container {
|
| 206 |
+
background-color: #f0f4f9; color: black
|
| 207 |
+
}
|
| 208 |
+
.gr-button-primary {
|
| 209 |
+
background-color: #FFD700 !important; color: #000000 !important
|
| 210 |
+
}
|
| 211 |
+
.gr-button-secondary {
|
| 212 |
+
border-color: #FFD700 !important; color: #000000 !important
|
| 213 |
+
}
|
| 214 |
+
.gr-tab button {
|
| 215 |
+
color: black !important
|
| 216 |
+
}
|
| 217 |
+
.gr-tab button.selected {
|
| 218 |
+
background-color: #FFD700 !important; color: #000000 !important
|
| 219 |
+
}
|
| 220 |
+
.gr-highlighted {
|
| 221 |
+
background-color: #CCCCCC !important
|
| 222 |
+
}
|
| 223 |
+
.anycoder-link {
|
| 224 |
+
color: #FFD700 !important; text-decoration: none; font-weight: bold
|
| 225 |
+
}
|
| 226 |
.mpl-chart-container {
|
| 227 |
border: 1px solid #CCCCCC;
|
| 228 |
+
border-radius: 5px;
|
| 229 |
+
overflow: hidden;
|
| 230 |
+
background: white;
|
| 231 |
+
width: 100%;
|
| 232 |
+
}
|
| 233 |
+
.chart-title {
|
| 234 |
+
color: black !important;
|
| 235 |
+
}
|
| 236 |
+
.metric-label {
|
| 237 |
+
color: black !important;
|
| 238 |
+
}
|
| 239 |
"""
|
| 240 |
) as demo:
|
| 241 |
|
data_processor.py
CHANGED
|
@@ -113,7 +113,9 @@ class DataProcessor:
|
|
| 113 |
"Dominance Index": f"{np.random.uniform(40, 60):.2f}%",
|
| 114 |
"Fear & Greed Index": np.random.choice(["Extreme Fear", "Fear", "Neutral", "Greed", "Extreme Greed"]),
|
| 115 |
"Hash Rate Trend": np.random.choice(["Increasing", "Stable", "Decreasing"]),
|
| 116 |
-
"Institutional Flow (Net)": f"{
|
|
|
|
|
|
|
| 117 |
"Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
|
| 118 |
}
|
| 119 |
else: # Default Gold (GC=F)
|
|
@@ -121,17 +123,27 @@ class DataProcessor:
|
|
| 121 |
"Gold Strength Index": round(np.random.uniform(30, 80), 1),
|
| 122 |
"Dollar Index (DXY)": round(np.random.uniform(90, 110), 1),
|
| 123 |
"Real Interest Rate": f"{np.random.uniform(-2, 5):.2f}%",
|
| 124 |
-
"Gold Volatility": f"{
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
"Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
|
| 128 |
}
|
| 129 |
|
| 130 |
return fundamentals
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
-
print(f"Error fetching fundamentals: {
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# prepare_for_chronos tetap sama, tidak perlu perubahan
|
| 137 |
def prepare_for_chronos(self, df, lookback=100):
|
|
|
|
| 113 |
"Dominance Index": f"{np.random.uniform(40, 60):.2f}%",
|
| 114 |
"Fear & Greed Index": np.random.choice(["Extreme Fear", "Fear", "Neutral", "Greed", "Extreme Greed"]),
|
| 115 |
"Hash Rate Trend": np.random.choice(["Increasing", "Stable", "Decreasing"]),
|
| 116 |
+
"Institutional Flow (Net)": f"{
|
| 117 |
+
np.random.uniform(-100, 100):,.0f
|
| 118 |
+
}M USD",
|
| 119 |
"Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
|
| 120 |
}
|
| 121 |
else: # Default Gold (GC=F)
|
|
|
|
| 123 |
"Gold Strength Index": round(np.random.uniform(30, 80), 1),
|
| 124 |
"Dollar Index (DXY)": round(np.random.uniform(90, 110), 1),
|
| 125 |
"Real Interest Rate": f"{np.random.uniform(-2, 5):.2f}%",
|
| 126 |
+
"Gold Volatility": f"{
|
| 127 |
+
np.random.uniform(10, 40):.1f
|
| 128 |
+
}%",
|
| 129 |
+
"Commercial Hedgers (Net)": f"{
|
| 130 |
+
np.random.uniform(-50000, 50000):,.0f
|
| 131 |
+
}",
|
| 132 |
+
"Managed Money (Net)": f"{
|
| 133 |
+
np.random.uniform(-100000, 100000):,.0f
|
| 134 |
+
}",
|
| 135 |
"Market Sentiment": np.random.choice(["Bullish", "Neutral", "Bearish"]),
|
| 136 |
}
|
| 137 |
|
| 138 |
return fundamentals
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
+
print(f"Error fetching fundamentals: {
|
| 142 |
+
e
|
| 143 |
+
}")
|
| 144 |
+
return {
|
| 145 |
+
"Error": str(e)
|
| 146 |
+
}
|
| 147 |
|
| 148 |
# prepare_for_chronos tetap sama, tidak perlu perubahan
|
| 149 |
def prepare_for_chronos(self, df, lookback=100):
|
requirements.txt
CHANGED
|
@@ -10,4 +10,4 @@ scikit-learn
|
|
| 10 |
safetensors
|
| 11 |
huggingface-hub
|
| 12 |
chronos-forecasting
|
| 13 |
-
mplfinance
|
|
|
|
| 10 |
safetensors
|
| 11 |
huggingface-hub
|
| 12 |
chronos-forecasting
|
| 13 |
+
mplfinance
|
sentiment_analyzer.py
CHANGED
|
@@ -50,17 +50,25 @@ class SentimentAnalyzer:
|
|
| 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: {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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;'>{
|
|
|
|
|
|
|
| 60 |
|
| 61 |
news_html += "</div>"
|
| 62 |
|
| 63 |
return sentiment, news_html
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
-
return 0, f"<p>Error analyzing sentiment: {
|
|
|
|
|
|
|
|
|
| 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: {
|
| 54 |
+
title_color
|
| 55 |
+
};'>Latest {
|
| 56 |
+
ticker
|
| 57 |
+
} News (Simulated)</h4>"
|
| 58 |
|
| 59 |
for news in selected_news:
|
| 60 |
sentiment_label = "🟢" if "positive" in news or "rising" in news or "support" in news or "bullish" in news or "accumulation" in news else \
|
| 61 |
"🔴" if "sell-off" in news or "weighs" in news or "outflows" in news or "Profit-taking" in news or "fear" in news else \
|
| 62 |
"🟡"
|
| 63 |
+
news_html += f"<p style='margin: 10px 0; padding: 10px; background: #E0E0E0; border-radius: 5px; color: black;'>{
|
| 64 |
+
sentiment_label
|
| 65 |
+
} {news}</p>"
|
| 66 |
|
| 67 |
news_html += "</div>"
|
| 68 |
|
| 69 |
return sentiment, news_html
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
return 0, f"<p>Error analyzing sentiment: {
|
| 73 |
+
str(e)
|
| 74 |
+
}</p>"
|
trading_logic.py
CHANGED
|
@@ -80,7 +80,9 @@ class TradingLogic:
|
|
| 80 |
return signal, confidence
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
print(f"Signal generation error: {
|
|
|
|
|
|
|
| 84 |
return "hold", 0.0
|
| 85 |
|
| 86 |
def calculate_tp_sl(self, current_price, atr, signal):
|
|
|
|
| 80 |
return signal, confidence
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
+
print(f"Signal generation error: {
|
| 84 |
+
e
|
| 85 |
+
}")
|
| 86 |
return "hold", 0.0
|
| 87 |
|
| 88 |
def calculate_tp_sl(self, current_price, atr, signal):
|