omniverse1 commited on
Commit
9bf8edc
·
verified ·
1 Parent(s): 93d1cfe

Update Gradio app with multiple files

Browse files
Files changed (5) hide show
  1. app.py +93 -35
  2. data_processor.py +18 -6
  3. requirements.txt +1 -1
  4. sentiment_analyzer.py +11 -3
  5. 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'{ticker} ({interval})',
 
 
 
 
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
- "Current Price": f"${current_price:.2f}",
 
56
  "Signal": signal.upper(),
57
- "Confidence": f"{confidence:.1%}",
58
- "Take Profit": f"${tp:.2f}" if tp else "N/A",
59
- "Stop Loss": f"${sl:.2f}" if sl else "N/A",
60
- "RSI": f"{df['RSI'].iloc[-1]:.1f}",
61
- "MACD": f"{df['MACD'].iloc[-1]:.4f}",
62
- "Volume": f"{df['Volume'].iloc[-1]:,.0f}"
 
 
 
 
 
 
 
 
 
 
 
 
63
  }
64
 
65
  return chart_html, metrics
66
 
67
  except Exception as e:
68
- return f"Error creating chart: {str(e)}", None
 
 
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={'x': [0, 1], 'y': [0, 1]},
81
- title={'text': f"{ticker} Market Sentiment (Simulated)"},
82
- delta={'reference': 0},
 
 
 
 
 
 
83
  gauge={
84
- 'axis': {'range': [-1, 1]},
85
- 'bar': {'color': "#FFD700"},
 
 
 
86
  'steps': [
87
- {'range': [-1, -0.5], 'color': "rgba(255,0,0,0.5)"},
88
- {'range': [-0.5, 0.5], 'color': "rgba(100,100,100,0.3)"},
 
 
 
 
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={'text': gauge_title},
 
 
136
  gauge={
137
- 'axis': {'range': gauge_range},
138
- 'bar': {'color': "#FFD700"},
 
 
 
139
  'steps': [
140
- {'range': [gauge_range[0], gauge_range[1] * 0.3], 'color': "rgba(255,0,0,0.5)"},
 
 
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 {background-color: #f0f4f9; color: black}
166
- .gr-button-primary {background-color: #FFD700 !important; color: #000000 !important}
167
- .gr-button-secondary {border-color: #FFD700 !important; color: #000000 !important}
168
- .gr-tab button {color: black !important}
169
- .gr-tab button.selected {background-color: #FFD700 !important; color: #000000 !important}
170
- .gr-highlighted {background-color: #CCCCCC !important}
171
- .anycoder-link {color: #FFD700 !important; text-decoration: none; font-weight: bold}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  .mpl-chart-container {
173
  border: 1px solid #CCCCCC;
174
- border-radius: 5px;
175
- overflow: hidden;
176
- background: white;
177
- width: 100%;
178
- }
179
- .chart-title {color: black !important;}
180
- .metric-label {color: black !important;}
 
 
 
 
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"{np.random.uniform(-100, 100):,.0f}M USD",
 
 
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"{np.random.uniform(10, 40):.1f}%",
125
- "Commercial Hedgers (Net)": f"{np.random.uniform(-50000, 50000):,.0f}",
126
- "Managed Money (Net)": f"{np.random.uniform(-100000, 100000):,.0f}",
 
 
 
 
 
 
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: {e}")
134
- return {"Error": str(e)}
 
 
 
 
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: {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
64
 
65
  except Exception as e:
66
- return 0, f"<p>Error analyzing sentiment: {str(e)}</p>"
 
 
 
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: {e}")
 
 
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):