Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# app.py - Real-Time Auto-Refresh Trading System
|
| 2 |
import gradio as gr
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
@@ -32,20 +31,20 @@ class RealTimeMarketData:
|
|
| 32 |
|
| 33 |
# Add new timestamp
|
| 34 |
self.timestamps.append(current_time.strftime('%H:%M:%S'))
|
| 35 |
-
if len(self.timestamps) >
|
| 36 |
self.timestamps.pop(0)
|
| 37 |
|
| 38 |
live_data = {}
|
| 39 |
|
| 40 |
for symbol in self.symbols:
|
| 41 |
# Generate realistic price movement
|
| 42 |
-
change_pct = np.random.normal(0, 0.8)
|
| 43 |
new_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 44 |
self.last_prices[symbol] = new_price
|
| 45 |
|
| 46 |
# Add to history
|
| 47 |
self.data_history[symbol].append(new_price)
|
| 48 |
-
if len(self.data_history[symbol]) >
|
| 49 |
self.data_history[symbol].pop(0)
|
| 50 |
|
| 51 |
# Calculate metrics
|
|
@@ -80,61 +79,142 @@ class AI_TradingAgents:
|
|
| 80 |
data = market_data[symbol]
|
| 81 |
current_price = data['current_price']
|
| 82 |
change = data['change']
|
|
|
|
| 83 |
|
| 84 |
analyses = {}
|
| 85 |
|
| 86 |
-
# Research Agent
|
| 87 |
if change > 2:
|
| 88 |
-
research_text = f"**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
research_conf = 85
|
| 90 |
elif change < -2:
|
| 91 |
-
research_text = f"**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
research_conf = 70
|
| 93 |
else:
|
| 94 |
-
research_text = f"**
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
analyses['research'] = {
|
| 98 |
'emoji': 'π',
|
| 99 |
-
'title': '
|
| 100 |
'analysis': research_text,
|
| 101 |
'confidence': research_conf
|
| 102 |
}
|
| 103 |
|
| 104 |
-
# Technical Agent
|
|
|
|
| 105 |
trend = "Bullish" if change > 0 else "Bearish"
|
|
|
|
|
|
|
|
|
|
| 106 |
analyses['technical'] = {
|
| 107 |
'emoji': 'π',
|
| 108 |
-
'title': 'Technical',
|
| 109 |
-
'analysis': f"**{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
'confidence': 75
|
| 111 |
}
|
| 112 |
|
| 113 |
-
# Risk Agent
|
| 114 |
-
volatility
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
analyses['risk'] = {
|
| 117 |
'emoji': 'π‘οΈ',
|
| 118 |
-
'title': 'Risk',
|
| 119 |
-
'analysis': f"**{risk_level} RISK
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
'confidence': 80
|
| 121 |
}
|
| 122 |
|
| 123 |
-
# Decision Engine
|
| 124 |
-
if change > 2:
|
| 125 |
decision = "BUY"
|
| 126 |
confidence = 85
|
|
|
|
|
|
|
| 127 |
elif change < -2:
|
| 128 |
decision = "SELL"
|
| 129 |
-
confidence =
|
|
|
|
|
|
|
| 130 |
else:
|
| 131 |
decision = "HOLD"
|
| 132 |
-
confidence =
|
|
|
|
|
|
|
| 133 |
|
| 134 |
analyses['decision'] = {
|
| 135 |
'emoji': 'π―',
|
| 136 |
-
'title': 'Decision',
|
| 137 |
-
'analysis': f"**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
'confidence': confidence,
|
| 139 |
'decision': decision
|
| 140 |
}
|
|
@@ -142,186 +222,162 @@ class AI_TradingAgents:
|
|
| 142 |
return analyses
|
| 143 |
|
| 144 |
def _get_error_analysis(self, symbol):
|
|
|
|
| 145 |
return {
|
| 146 |
-
'research': {'emoji': 'π', 'title': 'Research', 'analysis':
|
| 147 |
-
'technical': {'emoji': 'π', 'title': 'Technical', 'analysis':
|
| 148 |
-
'risk': {'emoji': 'π‘οΈ', 'title': 'Risk', 'analysis':
|
| 149 |
-
'decision': {'emoji': 'π―', 'title': 'Decision', 'analysis':
|
| 150 |
}
|
| 151 |
|
| 152 |
# Initialize components
|
| 153 |
market_data = RealTimeMarketData()
|
| 154 |
trading_agents = AI_TradingAgents()
|
| 155 |
|
| 156 |
-
def
|
| 157 |
-
"""
|
| 158 |
live_data = market_data.generate_live_data()
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
data.append({
|
| 163 |
-
'Symbol': symbol,
|
| 164 |
-
'Price': f"${info['current_price']:.2f}",
|
| 165 |
-
'Change': f"{info['change']:+.2f}%",
|
| 166 |
-
'Volume': f"{info['volume']:,}",
|
| 167 |
-
'Last Update': info['last_updated'],
|
| 168 |
-
'Update #': info['update_count']
|
| 169 |
-
})
|
| 170 |
-
|
| 171 |
-
df = pd.DataFrame(data)
|
| 172 |
-
return df
|
| 173 |
-
|
| 174 |
-
def get_live_chart():
|
| 175 |
-
"""Create live updating chart"""
|
| 176 |
-
live_data = market_data.generate_live_data()
|
| 177 |
|
| 178 |
-
|
|
|
|
|
|
|
| 179 |
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
fig.add_trace(go.Scatter(
|
| 183 |
-
x=data['timestamps'],
|
| 184 |
-
y=data['prices'],
|
| 185 |
-
mode='lines+markers',
|
| 186 |
-
name=symbol,
|
| 187 |
-
line=dict(color=colors[i], width=3),
|
| 188 |
-
marker=dict(size=6)
|
| 189 |
-
))
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
template='plotly_dark',
|
| 194 |
-
height=400
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
return fig
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
fig = go.Figure()
|
| 207 |
-
fig.add_trace(go.Bar(
|
| 208 |
-
x=symbols,
|
| 209 |
-
y=changes,
|
| 210 |
-
marker_color=['green' if c > 0 else 'red' for c in changes],
|
| 211 |
-
text=[f"{c:+.2f}%" for c in changes],
|
| 212 |
-
textposition='auto'
|
| 213 |
-
))
|
| 214 |
-
|
| 215 |
-
fig.update_layout(
|
| 216 |
-
title="Today's Performance",
|
| 217 |
-
template='plotly_dark',
|
| 218 |
-
height=300
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
return fig
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
live_data = market_data.generate_live_data()
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
analysis
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 244 |
-
report = f"# π― {symbol} Analysis\n\n"
|
| 245 |
-
report += f"**Price:** ${live_data[symbol]['current_price']:.2f}\n"
|
| 246 |
-
report += f"**Change:** {live_data[symbol]['change']:+.2f}%\n\n"
|
| 247 |
-
|
| 248 |
-
for agent_type, agent_analysis in analysis.items():
|
| 249 |
-
report += f"### {agent_analysis['emoji']} {agent_analysis['title']}\n"
|
| 250 |
-
report += f"{agent_analysis['analysis']}\n\n"
|
| 251 |
-
|
| 252 |
-
return report
|
| 253 |
-
else:
|
| 254 |
-
return f"# β Symbol not found: {symbol}"
|
| 255 |
|
| 256 |
# Create the interface
|
| 257 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="
|
| 258 |
|
| 259 |
gr.Markdown("""
|
| 260 |
-
# π€
|
| 261 |
-
## *
|
| 262 |
|
| 263 |
-
|
| 264 |
""")
|
| 265 |
|
| 266 |
with gr.Row():
|
| 267 |
with gr.Column(scale=1):
|
| 268 |
symbol_input = gr.Textbox(
|
| 269 |
-
label="Stock Symbol",
|
| 270 |
-
placeholder="AAPL, TSLA...
|
| 271 |
max_lines=1
|
| 272 |
)
|
| 273 |
gr.Markdown("""
|
| 274 |
-
**
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
""")
|
| 277 |
|
| 278 |
with gr.Column(scale=2):
|
| 279 |
-
gr.Markdown("###
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
dataframe = gr.DataFrame(
|
| 285 |
-
label="Live Stock Prices",
|
| 286 |
-
every=3, # Auto-refresh every 3 seconds
|
| 287 |
-
value=get_live_dataframe
|
| 288 |
-
)
|
| 289 |
-
|
| 290 |
-
with gr.Column():
|
| 291 |
-
# Performance chart
|
| 292 |
-
performance_chart = gr.Plot(
|
| 293 |
-
label="Performance",
|
| 294 |
-
every=3,
|
| 295 |
-
value=get_performance_chart
|
| 296 |
)
|
| 297 |
|
| 298 |
with gr.Row():
|
| 299 |
-
#
|
| 300 |
-
|
| 301 |
-
label="
|
| 302 |
-
every=
|
| 303 |
-
value=
|
| 304 |
-
)
|
| 305 |
-
|
| 306 |
-
with gr.Row():
|
| 307 |
-
# Analysis report
|
| 308 |
-
analysis_report = gr.Markdown(
|
| 309 |
-
label="AI Analysis",
|
| 310 |
-
every=3,
|
| 311 |
-
value=lambda: get_analysis_report("")
|
| 312 |
)
|
| 313 |
|
| 314 |
gr.Markdown(f"""
|
| 315 |
---
|
| 316 |
-
**π Live
|
| 317 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
""")
|
| 319 |
|
| 320 |
-
# Update analysis when symbol
|
| 321 |
symbol_input.change(
|
| 322 |
-
fn=
|
| 323 |
inputs=[symbol_input],
|
| 324 |
-
outputs=[
|
| 325 |
)
|
| 326 |
|
| 327 |
# Launch the app
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
|
|
|
| 31 |
|
| 32 |
# Add new timestamp
|
| 33 |
self.timestamps.append(current_time.strftime('%H:%M:%S'))
|
| 34 |
+
if len(self.timestamps) > 15:
|
| 35 |
self.timestamps.pop(0)
|
| 36 |
|
| 37 |
live_data = {}
|
| 38 |
|
| 39 |
for symbol in self.symbols:
|
| 40 |
# Generate realistic price movement
|
| 41 |
+
change_pct = np.random.normal(0, 0.8)
|
| 42 |
new_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 43 |
self.last_prices[symbol] = new_price
|
| 44 |
|
| 45 |
# Add to history
|
| 46 |
self.data_history[symbol].append(new_price)
|
| 47 |
+
if len(self.data_history[symbol]) > 15:
|
| 48 |
self.data_history[symbol].pop(0)
|
| 49 |
|
| 50 |
# Calculate metrics
|
|
|
|
| 79 |
data = market_data[symbol]
|
| 80 |
current_price = data['current_price']
|
| 81 |
change = data['change']
|
| 82 |
+
volatility = abs(change)
|
| 83 |
|
| 84 |
analyses = {}
|
| 85 |
|
| 86 |
+
# Research Agent - Fundamental Analysis
|
| 87 |
if change > 2:
|
| 88 |
+
research_text = f"""**Fundamental Analysis - Strong Bullish Momentum** π
|
| 89 |
+
|
| 90 |
+
β’ Revenue Growth: +18% YoY
|
| 91 |
+
β’ Expanding profit margins
|
| 92 |
+
β’ Market leadership position
|
| 93 |
+
β’ Positive institutional sentiment
|
| 94 |
+
β’ Strong balance sheet
|
| 95 |
+
|
| 96 |
+
**Recommendation: BUY** (85% confidence)
|
| 97 |
+
**Price Target: ${current_price * 1.15:.2f}**"""
|
| 98 |
research_conf = 85
|
| 99 |
elif change < -2:
|
| 100 |
+
research_text = f"""**Fundamental Analysis - Bearish Pressure** π
|
| 101 |
+
|
| 102 |
+
β’ Valuation concerns emerging
|
| 103 |
+
β’ Increasing competitive pressures
|
| 104 |
+
β’ Wait for better entry point
|
| 105 |
+
β’ Support at ${current_price * 0.95:.2f}
|
| 106 |
+
β’ Monitor earnings closely
|
| 107 |
+
|
| 108 |
+
**Recommendation: HOLD** (70% confidence)"""
|
| 109 |
research_conf = 70
|
| 110 |
else:
|
| 111 |
+
research_text = f"""**Fundamental Analysis - Consolidation Phase** βοΈ
|
| 112 |
+
|
| 113 |
+
β’ Steady revenue growth: +8% YoY
|
| 114 |
+
β’ Strong cash flow generation
|
| 115 |
+
β’ Solid market position
|
| 116 |
+
β’ Attractive risk-reward setup
|
| 117 |
+
|
| 118 |
+
**Recommendation: HOLD** (78% confidence)"""
|
| 119 |
+
research_conf = 78
|
| 120 |
|
| 121 |
analyses['research'] = {
|
| 122 |
'emoji': 'π',
|
| 123 |
+
'title': 'Fundamental Analysis',
|
| 124 |
'analysis': research_text,
|
| 125 |
'confidence': research_conf
|
| 126 |
}
|
| 127 |
|
| 128 |
+
# Technical Agent - Technical Analysis
|
| 129 |
+
rsi_level = "Overbought" if change > 2 else "Oversold" if change < -2 else "Neutral"
|
| 130 |
trend = "Bullish" if change > 0 else "Bearish"
|
| 131 |
+
support = current_price * 0.95
|
| 132 |
+
resistance = current_price * 1.05
|
| 133 |
+
|
| 134 |
analyses['technical'] = {
|
| 135 |
'emoji': 'π',
|
| 136 |
+
'title': 'Technical Analysis',
|
| 137 |
+
'analysis': f"""**{rsi_level} Conditions - {trend} Trend**
|
| 138 |
+
|
| 139 |
+
β’ Current Price: ${current_price:.2f}
|
| 140 |
+
β’ 24h Change: {change:+.2f}%
|
| 141 |
+
β’ Key Levels:
|
| 142 |
+
- Support: ${support:.2f}
|
| 143 |
+
- Resistance: ${resistance:.2f}
|
| 144 |
+
β’ RSI: {65 if change > 0 else 35} ({rsi_level})
|
| 145 |
+
β’ Volume: {data['volume']:,}
|
| 146 |
+
β’ Momentum: {'Positive' if change > 0 else 'Negative'}""",
|
| 147 |
'confidence': 75
|
| 148 |
}
|
| 149 |
|
| 150 |
+
# Risk Agent - Risk Management
|
| 151 |
+
if volatility > 3:
|
| 152 |
+
risk_level = "HIGH"
|
| 153 |
+
position_size = "1-2%"
|
| 154 |
+
stop_loss = "10%"
|
| 155 |
+
risk_reward = "1:2"
|
| 156 |
+
elif volatility > 1.5:
|
| 157 |
+
risk_level = "MEDIUM"
|
| 158 |
+
position_size = "2-3%"
|
| 159 |
+
stop_loss = "8%"
|
| 160 |
+
risk_reward = "1:2.5"
|
| 161 |
+
else:
|
| 162 |
+
risk_level = "LOW"
|
| 163 |
+
position_size = "3-4%"
|
| 164 |
+
stop_loss = "6%"
|
| 165 |
+
risk_reward = "1:3"
|
| 166 |
+
|
| 167 |
analyses['risk'] = {
|
| 168 |
'emoji': 'π‘οΈ',
|
| 169 |
+
'title': 'Risk Assessment',
|
| 170 |
+
'analysis': f"""**{risk_level} RISK PROFILE**
|
| 171 |
+
|
| 172 |
+
β’ Volatility: {volatility:.1f}%
|
| 173 |
+
β’ Recommended Position: {position_size} of portfolio
|
| 174 |
+
β’ Stop-Loss: {stop_loss} below entry
|
| 175 |
+
β’ Risk-Reward Ratio: {risk_reward}
|
| 176 |
+
β’ Maximum Drawdown: 12%
|
| 177 |
+
β’ Correlation: Low with portfolio
|
| 178 |
+
β’ Monitoring: {'Intensive' if risk_level == 'HIGH' else 'Standard'}""",
|
| 179 |
'confidence': 80
|
| 180 |
}
|
| 181 |
|
| 182 |
+
# Decision Engine - Final Decision
|
| 183 |
+
if change > 2 and volatility < 4:
|
| 184 |
decision = "BUY"
|
| 185 |
confidence = 85
|
| 186 |
+
reason = "Strong bullish momentum with favorable risk metrics and positive fundamentals"
|
| 187 |
+
action = "Enter long position with trailing stop at 8%. Target 15-20% upside."
|
| 188 |
elif change < -2:
|
| 189 |
decision = "SELL"
|
| 190 |
+
confidence = 78
|
| 191 |
+
reason = "Significant downward pressure with deteriorating technicals"
|
| 192 |
+
action = "Consider short opportunities or wait for stabilization. Set tight stop-loss."
|
| 193 |
else:
|
| 194 |
decision = "HOLD"
|
| 195 |
+
confidence = 72
|
| 196 |
+
reason = "Consolidation phase with mixed signals. Awaiting clearer market direction."
|
| 197 |
+
action = "Monitor for breakout above resistance or breakdown below support."
|
| 198 |
|
| 199 |
analyses['decision'] = {
|
| 200 |
'emoji': 'π―',
|
| 201 |
+
'title': 'Trading Decision',
|
| 202 |
+
'analysis': f"""**FINAL DECISION: {decision}** π―
|
| 203 |
+
|
| 204 |
+
**Confidence Level:** {confidence}%
|
| 205 |
+
**Current Price:** ${current_price:.2f}
|
| 206 |
+
**Price Change:** {change:+.2f}%
|
| 207 |
+
|
| 208 |
+
**Rationale:**
|
| 209 |
+
{reason}
|
| 210 |
+
|
| 211 |
+
**Execution Plan:**
|
| 212 |
+
{action}
|
| 213 |
+
|
| 214 |
+
**Key Factors:**
|
| 215 |
+
β’ Fundamental Score: {analyses['research']['confidence']}%
|
| 216 |
+
β’ Technical Score: {analyses['technical']['confidence']}%
|
| 217 |
+
β’ Risk Assessment: {analyses['risk']['confidence']}%""",
|
| 218 |
'confidence': confidence,
|
| 219 |
'decision': decision
|
| 220 |
}
|
|
|
|
| 222 |
return analyses
|
| 223 |
|
| 224 |
def _get_error_analysis(self, symbol):
|
| 225 |
+
error_msg = "Data temporarily unavailable. Please check symbol spelling or try again later."
|
| 226 |
return {
|
| 227 |
+
'research': {'emoji': 'π', 'title': 'Research', 'analysis': error_msg, 'confidence': 0},
|
| 228 |
+
'technical': {'emoji': 'π', 'title': 'Technical', 'analysis': error_msg, 'confidence': 0},
|
| 229 |
+
'risk': {'emoji': 'π‘οΈ', 'title': 'Risk', 'analysis': error_msg, 'confidence': 0},
|
| 230 |
+
'decision': {'emoji': 'π―', 'title': 'Decision', 'analysis': error_msg, 'confidence': 0, 'decision': 'HOLD'}
|
| 231 |
}
|
| 232 |
|
| 233 |
# Initialize components
|
| 234 |
market_data = RealTimeMarketData()
|
| 235 |
trading_agents = AI_TradingAgents()
|
| 236 |
|
| 237 |
+
def get_ai_agents_analysis(symbol_input=""):
|
| 238 |
+
"""Get detailed analysis from all AI agents"""
|
| 239 |
live_data = market_data.generate_live_data()
|
| 240 |
|
| 241 |
+
if not symbol_input:
|
| 242 |
+
return "# π€ AI Agents Analysis\n\n**Please enter a stock symbol to see detailed analysis from all AI agents**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
symbol = symbol_input.upper()
|
| 245 |
+
if symbol not in live_data:
|
| 246 |
+
return f"# β Symbol Not Found\n\n'{symbol}' is not in our tracked symbols. Try: {', '.join(market_data.symbols)}"
|
| 247 |
|
| 248 |
+
# Get analysis from all agents
|
| 249 |
+
analysis = trading_agents.analyze_market(symbol, live_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
+
detailed_report = f"""
|
| 252 |
+
# π€ AI Agents Specialized Analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
## π {symbol} - Real-time Analysis
|
| 255 |
+
**Current Price:** ${live_data[symbol]['current_price']:.2f}
|
| 256 |
+
**24h Change:** {live_data[symbol]['change']:+.2f}%
|
| 257 |
+
**Last Update:** {datetime.now().strftime('%H:%M:%S')}
|
| 258 |
+
**Analysis Cycle:** #{market_data.update_counter}
|
| 259 |
+
|
| 260 |
+
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
## {analysis['research']['emoji']} {analysis['research']['title']}
|
| 263 |
+
**Confidence Level:** {analysis['research']['confidence']}%
|
| 264 |
+
|
| 265 |
+
{analysis['research']['analysis']}
|
| 266 |
+
|
| 267 |
+
---
|
| 268 |
+
|
| 269 |
+
## {analysis['technical']['emoji']} {analysis['technical']['title']}
|
| 270 |
+
**Confidence Level:** {analysis['technical']['confidence']}%
|
| 271 |
+
|
| 272 |
+
{analysis['technical']['analysis']}
|
| 273 |
+
|
| 274 |
+
---
|
| 275 |
+
|
| 276 |
+
## {analysis['risk']['emoji']} {analysis['risk']['title']}
|
| 277 |
+
**Confidence Level:** {analysis['risk']['confidence']}%
|
| 278 |
+
|
| 279 |
+
{analysis['risk']['analysis']}
|
| 280 |
+
|
| 281 |
+
---
|
| 282 |
+
|
| 283 |
+
## {analysis['decision']['emoji']} {analysis['decision']['title']}
|
| 284 |
+
**Confidence Level:** {analysis['decision']['confidence']}%
|
| 285 |
+
|
| 286 |
+
{analysis['decision']['analysis']}
|
| 287 |
+
|
| 288 |
+
---
|
| 289 |
+
|
| 290 |
+
### π― Multi-Agent Consensus
|
| 291 |
+
- **Overall Confidence:** {np.mean([a['confidence'] for a in analysis.values()]):.1f}%
|
| 292 |
+
- **Final Recommendation:** {analysis['decision']['decision']}
|
| 293 |
+
- **Risk Level:** {'High' if analysis['risk']['confidence'] < 70 else 'Medium' if analysis['risk']['confidence'] < 80 else 'Low'}
|
| 294 |
+
- **Market Outlook:** {'Bullish' if analysis['research']['confidence'] > 80 else 'Neutral' if analysis['research']['confidence'] > 65 else 'Bearish'}
|
| 295 |
+
|
| 296 |
+
*Analysis generated by Multi-Agent AI Trading System*
|
| 297 |
+
"""
|
| 298 |
+
return detailed_report
|
| 299 |
+
|
| 300 |
+
def get_agents_performance():
|
| 301 |
+
"""Get performance overview of all agents"""
|
| 302 |
live_data = market_data.generate_live_data()
|
| 303 |
|
| 304 |
+
performance_data = []
|
| 305 |
+
for symbol in market_data.symbols:
|
| 306 |
+
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 307 |
+
performance_data.append({
|
| 308 |
+
'Symbol': symbol,
|
| 309 |
+
'Price': f"${live_data[symbol]['current_price']:.2f}",
|
| 310 |
+
'Change': f"{live_data[symbol]['change']:+.2f}%",
|
| 311 |
+
'Research': f"{analysis['research']['confidence']}%",
|
| 312 |
+
'Technical': f"{analysis['technical']['confidence']}%",
|
| 313 |
+
'Risk': f"{analysis['risk']['confidence']}%",
|
| 314 |
+
'Decision': analysis['decision']['decision'],
|
| 315 |
+
'Confidence': f"{analysis['decision']['confidence']}%"
|
| 316 |
+
})
|
| 317 |
+
|
| 318 |
+
df = pd.DataFrame(performance_data)
|
| 319 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
# Create the interface
|
| 322 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="AI Agents Analysis Dashboard") as demo:
|
| 323 |
|
| 324 |
gr.Markdown("""
|
| 325 |
+
# π€ AI Agents Specialized Analysis
|
| 326 |
+
## *Multi-Agent AI Trading System - Professional Analysis*
|
| 327 |
|
| 328 |
+
**Real-time analysis from 4 specialized AI agents working in coordination**
|
| 329 |
""")
|
| 330 |
|
| 331 |
with gr.Row():
|
| 332 |
with gr.Column(scale=1):
|
| 333 |
symbol_input = gr.Textbox(
|
| 334 |
+
label="Enter Stock Symbol",
|
| 335 |
+
placeholder="e.g., AAPL, TSLA, GOOGL...",
|
| 336 |
max_lines=1
|
| 337 |
)
|
| 338 |
gr.Markdown("""
|
| 339 |
+
**Available Symbols:** AAPL, GOOGL, MSFT, TSLA
|
| 340 |
+
|
| 341 |
+
**π€ AI Agents:**
|
| 342 |
+
- π Research Agent: Fundamental Analysis
|
| 343 |
+
- π Technical Agent: Price & Patterns
|
| 344 |
+
- π‘οΈ Risk Agent: Risk Management
|
| 345 |
+
- π― Decision Engine: Final Recommendation
|
| 346 |
""")
|
| 347 |
|
| 348 |
with gr.Column(scale=2):
|
| 349 |
+
gr.Markdown("### π Agents Performance Overview")
|
| 350 |
+
agents_performance = gr.DataFrame(
|
| 351 |
+
label="AI Agents Confidence Levels",
|
| 352 |
+
every=5,
|
| 353 |
+
value=get_agents_performance
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
)
|
| 355 |
|
| 356 |
with gr.Row():
|
| 357 |
+
# AI Agents Detailed Analysis
|
| 358 |
+
agents_analysis = gr.Markdown(
|
| 359 |
+
label="Specialized AI Agents Analysis",
|
| 360 |
+
every=5,
|
| 361 |
+
value=get_ai_agents_analysis
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
)
|
| 363 |
|
| 364 |
gr.Markdown(f"""
|
| 365 |
---
|
| 366 |
+
**π Live AI Analysis Active** β’ **Last Analysis:** {datetime.now().strftime('%H:%M:%S')}
|
| 367 |
+
*Each agent provides specialized analysis updated every 5 seconds*
|
| 368 |
+
|
| 369 |
+
**π Analysis Includes:**
|
| 370 |
+
- Fundamental company research and growth prospects
|
| 371 |
+
- Technical price patterns and market structure
|
| 372 |
+
- Comprehensive risk assessment and position sizing
|
| 373 |
+
- Final trading decision with execution plan
|
| 374 |
""")
|
| 375 |
|
| 376 |
+
# Update analysis when symbol changes
|
| 377 |
symbol_input.change(
|
| 378 |
+
fn=get_ai_agents_analysis,
|
| 379 |
inputs=[symbol_input],
|
| 380 |
+
outputs=[agents_analysis]
|
| 381 |
)
|
| 382 |
|
| 383 |
# Launch the app
|