Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,14 +16,21 @@ class RealTimeMarketData:
|
|
| 16 |
self.data_history = {symbol: [] for symbol in symbols}
|
| 17 |
self.timestamps = []
|
| 18 |
self.update_counter = 0
|
|
|
|
| 19 |
|
| 20 |
def generate_live_data(self):
|
| 21 |
"""Generate realistic live market data with actual changes"""
|
| 22 |
-
self.update_counter += 1
|
| 23 |
current_time = datetime.now()
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
self.timestamps.pop(0)
|
| 28 |
|
| 29 |
self.timestamps.append(current_time.strftime('%H:%M:%S'))
|
|
@@ -35,18 +42,17 @@ class RealTimeMarketData:
|
|
| 35 |
# Get actual current price from Yahoo Finance
|
| 36 |
ticker = yf.Ticker(symbol)
|
| 37 |
current_info = ticker.info
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
# Add
|
| 43 |
if symbol in self.last_prices:
|
| 44 |
-
#
|
| 45 |
-
change_pct = np.random.
|
| 46 |
current_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 47 |
else:
|
| 48 |
-
|
| 49 |
-
current_price = current_price * (1 + np.random.uniform(-0.01, 0.01))
|
| 50 |
|
| 51 |
self.last_prices[symbol] = current_price
|
| 52 |
|
|
@@ -56,11 +62,17 @@ class RealTimeMarketData:
|
|
| 56 |
|
| 57 |
self.data_history[symbol].append(current_price)
|
| 58 |
|
| 59 |
-
# Keep only last
|
| 60 |
-
if len(self.data_history[symbol]) >
|
| 61 |
self.data_history[symbol].pop(0)
|
| 62 |
|
| 63 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if len(self.data_history[symbol]) > 1:
|
| 65 |
change = ((current_price - self.data_history[symbol][0]) /
|
| 66 |
self.data_history[symbol][0]) * 100
|
|
@@ -69,27 +81,27 @@ class RealTimeMarketData:
|
|
| 69 |
|
| 70 |
live_data[symbol] = {
|
| 71 |
'prices': self.data_history[symbol].copy(),
|
| 72 |
-
'timestamps':
|
| 73 |
'current_price': current_price,
|
| 74 |
'change': change,
|
| 75 |
'volume': np.random.randint(1000000, 5000000),
|
| 76 |
-
'update_count': self.update_counter
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
-
# Fallback to simulated data
|
| 81 |
print(f"Error with {symbol}: {e}")
|
| 82 |
live_data[symbol] = self._generate_simulated_data(symbol)
|
| 83 |
|
| 84 |
return live_data
|
| 85 |
|
| 86 |
def _generate_simulated_data(self, symbol):
|
| 87 |
-
"""Generate realistic simulated data
|
| 88 |
if symbol not in self.last_prices:
|
| 89 |
self.last_prices[symbol] = np.random.uniform(150, 250)
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
change_pct = np.random.
|
| 93 |
new_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 94 |
self.last_prices[symbol] = new_price
|
| 95 |
|
|
@@ -99,15 +111,12 @@ class RealTimeMarketData:
|
|
| 99 |
|
| 100 |
self.data_history[symbol].append(new_price)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
if len(self.data_history[symbol]) > 20:
|
| 104 |
self.data_history[symbol].pop(0)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
current_timestamps =
|
| 108 |
-
|
| 109 |
-
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i)).strftime('%H:%M:%S')}"
|
| 110 |
-
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
| 111 |
|
| 112 |
change = ((new_price - self.data_history[symbol][0]) / self.data_history[symbol][0]) * 100
|
| 113 |
|
|
@@ -117,8 +126,26 @@ class RealTimeMarketData:
|
|
| 117 |
'current_price': new_price,
|
| 118 |
'change': change,
|
| 119 |
'volume': np.random.randint(1000000, 5000000),
|
| 120 |
-
'update_count': self.update_counter
|
|
|
|
| 121 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
class AI_TradingAgents:
|
| 124 |
def __init__(self):
|
|
@@ -128,7 +155,7 @@ class AI_TradingAgents:
|
|
| 128 |
'risk': {'name': 'Risk Agent', 'emoji': 'π‘οΈ'},
|
| 129 |
'decision': {'name': 'Decision Engine', 'emoji': 'π―'}
|
| 130 |
}
|
| 131 |
-
self.
|
| 132 |
|
| 133 |
def analyze_market(self, symbol, market_data):
|
| 134 |
if symbol not in market_data:
|
|
@@ -136,71 +163,93 @@ class AI_TradingAgents:
|
|
| 136 |
|
| 137 |
data = market_data[symbol]
|
| 138 |
current_price = data['current_price']
|
|
|
|
| 139 |
|
| 140 |
-
# Make analysis
|
| 141 |
-
price_trend = "Bullish" if data['change'] > 0 else "Bearish"
|
| 142 |
-
volatility = abs(data['change'])
|
| 143 |
-
|
| 144 |
analyses = {}
|
| 145 |
|
| 146 |
-
# Research Agent
|
| 147 |
-
if
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
}
|
| 154 |
else:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
# Technical Agent - dynamic analysis
|
| 163 |
-
rsi = "Oversold" if data['change'] < -2 else "Overbought" if data['change'] > 2 else "Neutral"
|
| 164 |
analyses['technical'] = {
|
| 165 |
'emoji': 'π',
|
| 166 |
'title': 'Technical Analysis',
|
| 167 |
-
'analysis': f"**{
|
| 168 |
'confidence': 75
|
| 169 |
}
|
| 170 |
|
| 171 |
-
# Risk Agent
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
analyses['risk'] = {
|
| 174 |
'emoji': 'π‘οΈ',
|
| 175 |
'title': 'Risk Assessment',
|
| 176 |
-
'analysis': f"**{risk_level}
|
| 177 |
'confidence': 80
|
| 178 |
}
|
| 179 |
|
| 180 |
-
# Decision Engine
|
| 181 |
-
if
|
| 182 |
decision = "BUY"
|
| 183 |
confidence = 85
|
| 184 |
-
reason = "Strong
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
else:
|
| 190 |
decision = "HOLD"
|
| 191 |
confidence = 70
|
| 192 |
-
reason = "
|
|
|
|
| 193 |
|
| 194 |
analyses['decision'] = {
|
| 195 |
'emoji': 'π―',
|
| 196 |
'title': 'Trading Decision',
|
| 197 |
-
'analysis': f"**{decision}** π―\n\
|
| 198 |
'confidence': confidence,
|
| 199 |
'decision': decision
|
| 200 |
}
|
| 201 |
|
| 202 |
-
|
| 203 |
-
self.analysis_history[symbol] = analyses
|
| 204 |
return analyses
|
| 205 |
|
| 206 |
def _get_error_analysis(self, symbol):
|
|
@@ -216,25 +265,25 @@ market_data = RealTimeMarketData()
|
|
| 216 |
trading_agents = AI_TradingAgents()
|
| 217 |
|
| 218 |
def create_live_dashboard():
|
| 219 |
-
"""Create dashboard
|
| 220 |
-
# Get fresh data every call
|
| 221 |
live_data = market_data.generate_live_data()
|
| 222 |
|
| 223 |
fig = make_subplots(
|
| 224 |
rows=2, cols=2,
|
| 225 |
subplot_titles=[
|
| 226 |
-
'π Live Price Movement',
|
| 227 |
'π Real-Time Performance',
|
| 228 |
'π Minute-by-Minute Changes',
|
| 229 |
-
'π― Market
|
| 230 |
],
|
| 231 |
specs=[
|
| 232 |
[{"type": "scatter"}, {"type": "bar"}],
|
| 233 |
[{"type": "scatter"}, {"type": "pie"}]
|
| 234 |
-
]
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
-
# 1. Live price lines
|
| 238 |
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4']
|
| 239 |
for i, (symbol, data) in enumerate(live_data.items()):
|
| 240 |
fig.add_trace(
|
|
@@ -242,103 +291,111 @@ def create_live_dashboard():
|
|
| 242 |
x=data['timestamps'],
|
| 243 |
y=data['prices'],
|
| 244 |
mode='lines+markers',
|
| 245 |
-
name=symbol,
|
| 246 |
-
line=dict(color=colors[i], width=3),
|
| 247 |
marker=dict(size=6),
|
| 248 |
-
hovertemplate=f'<b>{symbol}</b><br>%{{x}}<br>$%{{y:.2f}}<extra></extra>'
|
| 249 |
),
|
| 250 |
row=1, col=1
|
| 251 |
)
|
| 252 |
|
| 253 |
-
# 2. Performance bars
|
| 254 |
symbols = list(live_data.keys())
|
| 255 |
changes = [live_data[s]['change'] for s in symbols]
|
|
|
|
| 256 |
|
| 257 |
fig.add_trace(
|
| 258 |
go.Bar(
|
| 259 |
x=symbols,
|
| 260 |
y=changes,
|
| 261 |
-
marker_color=['
|
| 262 |
-
text=[f"{c:+.2f}%" for c in changes],
|
| 263 |
textposition='auto',
|
| 264 |
-
name='
|
| 265 |
),
|
| 266 |
row=1, col=2
|
| 267 |
)
|
| 268 |
|
| 269 |
-
# 3.
|
| 270 |
if live_data:
|
| 271 |
first_symbol = list(live_data.keys())[0]
|
| 272 |
data = live_data[first_symbol]
|
| 273 |
-
if len(data['prices']) >
|
| 274 |
recent_changes = []
|
| 275 |
-
for i in range(1, min(
|
| 276 |
change = ((data['prices'][-i] - data['prices'][-i-1]) / data['prices'][-i-1]) * 100
|
| 277 |
recent_changes.append(change)
|
| 278 |
|
| 279 |
fig.add_trace(
|
| 280 |
go.Scatter(
|
| 281 |
-
x=data['timestamps'][-
|
| 282 |
y=recent_changes[::-1],
|
| 283 |
mode='lines+markers+text',
|
| 284 |
name='Recent Changes',
|
| 285 |
line=dict(color='#FECA57', width=4),
|
| 286 |
-
marker=dict(size=
|
| 287 |
text=[f"{c:+.2f}%" for c in recent_changes[::-1]],
|
| 288 |
textposition='top center'
|
| 289 |
),
|
| 290 |
row=2, col=1
|
| 291 |
)
|
| 292 |
|
| 293 |
-
# 4. Market sentiment
|
| 294 |
bullish = len([d for d in live_data.values() if d['change'] > 0])
|
| 295 |
bearish = len([d for d in live_data.values() if d['change'] < 0])
|
|
|
|
| 296 |
|
| 297 |
fig.add_trace(
|
| 298 |
go.Pie(
|
| 299 |
-
labels=['Bullish', 'Bearish'],
|
| 300 |
-
values=[bullish, bearish],
|
| 301 |
hole=0.4,
|
| 302 |
-
marker_colors=['#00CC96', '#EF553B'],
|
| 303 |
-
name='Sentiment'
|
| 304 |
),
|
| 305 |
row=2, col=2
|
| 306 |
)
|
| 307 |
|
| 308 |
fig.update_layout(
|
| 309 |
height=800,
|
| 310 |
-
title_text=f"π LIVE
|
| 311 |
template='plotly_dark',
|
| 312 |
-
showlegend=True
|
|
|
|
| 313 |
)
|
| 314 |
|
| 315 |
return fig, live_data
|
| 316 |
|
| 317 |
def generate_analysis(symbol_input, live_data):
|
| 318 |
-
"""Generate analysis
|
| 319 |
if not symbol_input:
|
| 320 |
-
#
|
| 321 |
-
analysis_text = "# π Market Overview\n\n"
|
|
|
|
|
|
|
|
|
|
| 322 |
for symbol, data in live_data.items():
|
| 323 |
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 324 |
decision = analysis['decision']
|
| 325 |
|
| 326 |
-
|
|
|
|
| 327 |
analysis_text += f"**Price:** ${data['current_price']:.2f} | "
|
| 328 |
analysis_text += f"**Change:** {data['change']:+.2f}%\n"
|
| 329 |
-
analysis_text += f"**Decision:** {decision['decision']} ({decision['confidence']}%)\n\n"
|
| 330 |
|
| 331 |
return analysis_text
|
| 332 |
else:
|
| 333 |
# Specific symbol analysis
|
| 334 |
-
symbol = symbol_input.upper()
|
| 335 |
if symbol in live_data:
|
| 336 |
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 337 |
|
| 338 |
-
analysis_text = f"# π― {symbol} Analysis\n\n"
|
| 339 |
-
analysis_text += f"**
|
| 340 |
-
analysis_text += f"**Change:** {live_data[symbol]['change']:+.2f}%\n"
|
| 341 |
-
analysis_text += f"**Last
|
|
|
|
| 342 |
|
| 343 |
for agent_type, agent_analysis in analysis.items():
|
| 344 |
analysis_text += f"## {agent_analysis['emoji']} {agent_analysis['title']}\n"
|
|
@@ -346,57 +403,80 @@ def generate_analysis(symbol_input, live_data):
|
|
| 346 |
|
| 347 |
return analysis_text
|
| 348 |
else:
|
| 349 |
-
return f"# β Symbol not
|
| 350 |
|
| 351 |
def update_interface(symbol_input=""):
|
| 352 |
-
"""Main update function
|
| 353 |
dashboard, live_data = create_live_dashboard()
|
| 354 |
analysis = generate_analysis(symbol_input, live_data)
|
| 355 |
return dashboard, analysis
|
| 356 |
|
| 357 |
-
# Create
|
| 358 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
gr.Markdown("""
|
| 361 |
# π€ Real-Time AI Trading System
|
| 362 |
-
## *
|
| 363 |
|
| 364 |
-
**Charts
|
| 365 |
""")
|
| 366 |
|
| 367 |
with gr.Row():
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
with gr.Tabs():
|
| 376 |
-
with gr.TabItem("π Live
|
| 377 |
-
chart_output = gr.Plot(
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
with gr.TabItem("π€ AI Analysis"):
|
| 380 |
-
analysis_output = gr.Markdown(
|
|
|
|
|
|
|
| 381 |
|
| 382 |
gr.Markdown(f"""
|
| 383 |
---
|
| 384 |
-
|
| 385 |
-
|
|
|
|
| 386 |
""")
|
| 387 |
|
| 388 |
-
#
|
| 389 |
-
|
| 390 |
fn=update_interface,
|
| 391 |
inputs=[symbol_input],
|
| 392 |
outputs=[chart_output, analysis_output]
|
| 393 |
)
|
| 394 |
-
|
| 395 |
-
# Load initial data
|
| 396 |
-
demo.load(
|
| 397 |
-
fn=lambda: update_interface(""),
|
| 398 |
-
outputs=[chart_output, analysis_output]
|
| 399 |
-
)
|
| 400 |
|
|
|
|
| 401 |
if __name__ == "__main__":
|
| 402 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
self.data_history = {symbol: [] for symbol in symbols}
|
| 17 |
self.timestamps = []
|
| 18 |
self.update_counter = 0
|
| 19 |
+
self.last_update = datetime.now()
|
| 20 |
|
| 21 |
def generate_live_data(self):
|
| 22 |
"""Generate realistic live market data with actual changes"""
|
|
|
|
| 23 |
current_time = datetime.now()
|
| 24 |
|
| 25 |
+
# Update every 10 seconds
|
| 26 |
+
if (current_time - self.last_update).seconds < 10 and self.update_counter > 0:
|
| 27 |
+
return self.get_current_data()
|
| 28 |
+
|
| 29 |
+
self.update_counter += 1
|
| 30 |
+
self.last_update = current_time
|
| 31 |
+
|
| 32 |
+
# Keep only last 15 timestamps for performance
|
| 33 |
+
if len(self.timestamps) > 15:
|
| 34 |
self.timestamps.pop(0)
|
| 35 |
|
| 36 |
self.timestamps.append(current_time.strftime('%H:%M:%S'))
|
|
|
|
| 42 |
# Get actual current price from Yahoo Finance
|
| 43 |
ticker = yf.Ticker(symbol)
|
| 44 |
current_info = ticker.info
|
| 45 |
+
base_price = current_info.get('currentPrice',
|
| 46 |
+
current_info.get('regularMarketPrice',
|
| 47 |
+
current_info.get('previousClose', 150)))
|
| 48 |
|
| 49 |
+
# Add realistic random movement
|
| 50 |
if symbol in self.last_prices:
|
| 51 |
+
# More visible changes for demo (1-3%)
|
| 52 |
+
change_pct = np.random.uniform(-2, 2)
|
| 53 |
current_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 54 |
else:
|
| 55 |
+
current_price = base_price
|
|
|
|
| 56 |
|
| 57 |
self.last_prices[symbol] = current_price
|
| 58 |
|
|
|
|
| 62 |
|
| 63 |
self.data_history[symbol].append(current_price)
|
| 64 |
|
| 65 |
+
# Keep only last 15 prices
|
| 66 |
+
if len(self.data_history[symbol]) > 15:
|
| 67 |
self.data_history[symbol].pop(0)
|
| 68 |
|
| 69 |
+
# Ensure timestamps match
|
| 70 |
+
current_timestamps = self.timestamps[-len(self.data_history[symbol]):]
|
| 71 |
+
if len(current_timestamps) != len(self.data_history[symbol]):
|
| 72 |
+
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i*10)).strftime('%H:%M:%S')}"
|
| 73 |
+
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
| 74 |
+
|
| 75 |
+
# Calculate change
|
| 76 |
if len(self.data_history[symbol]) > 1:
|
| 77 |
change = ((current_price - self.data_history[symbol][0]) /
|
| 78 |
self.data_history[symbol][0]) * 100
|
|
|
|
| 81 |
|
| 82 |
live_data[symbol] = {
|
| 83 |
'prices': self.data_history[symbol].copy(),
|
| 84 |
+
'timestamps': current_timestamps,
|
| 85 |
'current_price': current_price,
|
| 86 |
'change': change,
|
| 87 |
'volume': np.random.randint(1000000, 5000000),
|
| 88 |
+
'update_count': self.update_counter,
|
| 89 |
+
'last_updated': current_time.strftime('%H:%M:%S')
|
| 90 |
}
|
| 91 |
|
| 92 |
except Exception as e:
|
|
|
|
| 93 |
print(f"Error with {symbol}: {e}")
|
| 94 |
live_data[symbol] = self._generate_simulated_data(symbol)
|
| 95 |
|
| 96 |
return live_data
|
| 97 |
|
| 98 |
def _generate_simulated_data(self, symbol):
|
| 99 |
+
"""Generate realistic simulated data"""
|
| 100 |
if symbol not in self.last_prices:
|
| 101 |
self.last_prices[symbol] = np.random.uniform(150, 250)
|
| 102 |
|
| 103 |
+
# Visible price movement for demo
|
| 104 |
+
change_pct = np.random.uniform(-2.5, 2.5)
|
| 105 |
new_price = self.last_prices[symbol] * (1 + change_pct/100)
|
| 106 |
self.last_prices[symbol] = new_price
|
| 107 |
|
|
|
|
| 111 |
|
| 112 |
self.data_history[symbol].append(new_price)
|
| 113 |
|
| 114 |
+
if len(self.data_history[symbol]) > 15:
|
|
|
|
| 115 |
self.data_history[symbol].pop(0)
|
| 116 |
|
| 117 |
+
# Generate timestamps
|
| 118 |
+
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i*10)).strftime('%H:%M:%S')}"
|
| 119 |
+
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
|
|
|
|
|
|
| 120 |
|
| 121 |
change = ((new_price - self.data_history[symbol][0]) / self.data_history[symbol][0]) * 100
|
| 122 |
|
|
|
|
| 126 |
'current_price': new_price,
|
| 127 |
'change': change,
|
| 128 |
'volume': np.random.randint(1000000, 5000000),
|
| 129 |
+
'update_count': self.update_counter,
|
| 130 |
+
'last_updated': datetime.now().strftime('%H:%M:%S')
|
| 131 |
}
|
| 132 |
+
|
| 133 |
+
def get_current_data(self):
|
| 134 |
+
"""Return current data without updating"""
|
| 135 |
+
live_data = {}
|
| 136 |
+
for symbol in self.symbols:
|
| 137 |
+
if symbol in self.data_history and self.data_history[symbol]:
|
| 138 |
+
live_data[symbol] = {
|
| 139 |
+
'prices': self.data_history[symbol].copy(),
|
| 140 |
+
'timestamps': self.timestamps[-len(self.data_history[symbol]):],
|
| 141 |
+
'current_price': self.last_prices.get(symbol, 150),
|
| 142 |
+
'change': ((self.last_prices.get(symbol, 150) - self.data_history[symbol][0]) /
|
| 143 |
+
self.data_history[symbol][0] * 100) if self.data_history[symbol] else 0,
|
| 144 |
+
'volume': np.random.randint(1000000, 5000000),
|
| 145 |
+
'update_count': self.update_counter,
|
| 146 |
+
'last_updated': self.last_update.strftime('%H:%M:%S')
|
| 147 |
+
}
|
| 148 |
+
return live_data
|
| 149 |
|
| 150 |
class AI_TradingAgents:
|
| 151 |
def __init__(self):
|
|
|
|
| 155 |
'risk': {'name': 'Risk Agent', 'emoji': 'π‘οΈ'},
|
| 156 |
'decision': {'name': 'Decision Engine', 'emoji': 'π―'}
|
| 157 |
}
|
| 158 |
+
self.last_analysis = {}
|
| 159 |
|
| 160 |
def analyze_market(self, symbol, market_data):
|
| 161 |
if symbol not in market_data:
|
|
|
|
| 163 |
|
| 164 |
data = market_data[symbol]
|
| 165 |
current_price = data['current_price']
|
| 166 |
+
change = data['change']
|
| 167 |
|
| 168 |
+
# Make analysis dynamic based on price changes
|
|
|
|
|
|
|
|
|
|
| 169 |
analyses = {}
|
| 170 |
|
| 171 |
+
# Research Agent
|
| 172 |
+
if change > 1.5:
|
| 173 |
+
research_text = f"**Bullish Momentum** π\n\nβ’ Strong price action\nβ’ Positive market sentiment\nβ’ Institutional interest growing\nβ’ **Recommendation: BUY** (88% confidence)\nβ’ Target: ${current_price * 1.12:.2f}"
|
| 174 |
+
research_conf = 88
|
| 175 |
+
elif change < -1.5:
|
| 176 |
+
research_text = f"**Bearish Pressure** π\n\nβ’ Price weakness evident\nβ’ Consider waiting for stabilization\nβ’ Support at ${current_price * 0.95:.2f}\nβ’ **Recommendation: HOLD** (72% confidence)"
|
| 177 |
+
research_conf = 72
|
|
|
|
| 178 |
else:
|
| 179 |
+
research_text = f"**Consolidation Phase** βοΈ\n\nβ’ Sideways movement\nβ’ Awaiting catalyst\nβ’ Good risk-reward setup\nβ’ **Recommendation: HOLD** (78% confidence)"
|
| 180 |
+
research_conf = 78
|
| 181 |
+
|
| 182 |
+
analyses['research'] = {
|
| 183 |
+
'emoji': 'π',
|
| 184 |
+
'title': 'Fundamental Analysis',
|
| 185 |
+
'analysis': research_text,
|
| 186 |
+
'confidence': research_conf
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
# Technical Agent
|
| 190 |
+
rsi_level = "Overbought" if change > 2 else "Oversold" if change < -2 else "Neutral"
|
| 191 |
+
trend = "Bullish" if change > 0 else "Bearish"
|
| 192 |
|
|
|
|
|
|
|
| 193 |
analyses['technical'] = {
|
| 194 |
'emoji': 'π',
|
| 195 |
'title': 'Technical Analysis',
|
| 196 |
+
'analysis': f"**{rsi_level} - {trend} Trend**\n\nβ’ Price: ${current_price:.2f}\nβ’ Change: {change:+.2f}%\nβ’ RSI: {65 if change > 0 else 35}\nβ’ Volume: {data['volume']:,}\nβ’ Momentum: {'Positive' if change > 0 else 'Negative'}",
|
| 197 |
'confidence': 75
|
| 198 |
}
|
| 199 |
|
| 200 |
+
# Risk Agent
|
| 201 |
+
volatility = abs(change)
|
| 202 |
+
if volatility > 3:
|
| 203 |
+
risk_level = "HIGH"
|
| 204 |
+
position_size = "1-2%"
|
| 205 |
+
stop_loss = "10%"
|
| 206 |
+
elif volatility > 1.5:
|
| 207 |
+
risk_level = "MEDIUM"
|
| 208 |
+
position_size = "2-3%"
|
| 209 |
+
stop_loss = "8%"
|
| 210 |
+
else:
|
| 211 |
+
risk_level = "LOW"
|
| 212 |
+
position_size = "3-4%"
|
| 213 |
+
stop_loss = "6%"
|
| 214 |
+
|
| 215 |
analyses['risk'] = {
|
| 216 |
'emoji': 'π‘οΈ',
|
| 217 |
'title': 'Risk Assessment',
|
| 218 |
+
'analysis': f"**{risk_level} RISK**\n\nβ’ Volatility: {volatility:.1f}%\nβ’ Position Size: {position_size}\nβ’ Stop-Loss: {stop_loss}\nβ’ Risk-Reward: 1:{3 if risk_level == 'LOW' else 2}\nβ’ Monitoring: {'Intensive' if risk_level == 'HIGH' else 'Standard'}",
|
| 219 |
'confidence': 80
|
| 220 |
}
|
| 221 |
|
| 222 |
+
# Decision Engine
|
| 223 |
+
if change > 2 and volatility < 4:
|
| 224 |
decision = "BUY"
|
| 225 |
confidence = 85
|
| 226 |
+
reason = "Strong uptrend with controlled risk"
|
| 227 |
+
action = "Enter long position with trailing stop"
|
| 228 |
+
elif change < -2:
|
| 229 |
+
decision = "SELL"
|
| 230 |
+
confidence = 78
|
| 231 |
+
reason = "Significant downward pressure"
|
| 232 |
+
action = "Consider short opportunities or wait"
|
| 233 |
+
elif abs(change) < 0.5:
|
| 234 |
+
decision = "HOLD"
|
| 235 |
+
confidence = 65
|
| 236 |
+
reason = "Minimal movement, awaiting direction"
|
| 237 |
+
action = "Monitor for breakout signals"
|
| 238 |
else:
|
| 239 |
decision = "HOLD"
|
| 240 |
confidence = 70
|
| 241 |
+
reason = "Moderate movement, needs confirmation"
|
| 242 |
+
action = "Wait for clearer trend establishment"
|
| 243 |
|
| 244 |
analyses['decision'] = {
|
| 245 |
'emoji': 'π―',
|
| 246 |
'title': 'Trading Decision',
|
| 247 |
+
'analysis': f"**{decision}** π―\n\n**Confidence:** {confidence}%\n**Current Price:** ${current_price:.2f}\n**Price Change:** {change:+.2f}%\n**Reason:** {reason}\n\n**Action:** {action}",
|
| 248 |
'confidence': confidence,
|
| 249 |
'decision': decision
|
| 250 |
}
|
| 251 |
|
| 252 |
+
self.last_analysis[symbol] = analyses
|
|
|
|
| 253 |
return analyses
|
| 254 |
|
| 255 |
def _get_error_analysis(self, symbol):
|
|
|
|
| 265 |
trading_agents = AI_TradingAgents()
|
| 266 |
|
| 267 |
def create_live_dashboard():
|
| 268 |
+
"""Create auto-updating dashboard"""
|
|
|
|
| 269 |
live_data = market_data.generate_live_data()
|
| 270 |
|
| 271 |
fig = make_subplots(
|
| 272 |
rows=2, cols=2,
|
| 273 |
subplot_titles=[
|
| 274 |
+
'π Live Price Movement (Auto-Refresh)',
|
| 275 |
'π Real-Time Performance',
|
| 276 |
'π Minute-by-Minute Changes',
|
| 277 |
+
'π― Market Sentiment'
|
| 278 |
],
|
| 279 |
specs=[
|
| 280 |
[{"type": "scatter"}, {"type": "bar"}],
|
| 281 |
[{"type": "scatter"}, {"type": "pie"}]
|
| 282 |
+
],
|
| 283 |
+
vertical_spacing=0.12
|
| 284 |
)
|
| 285 |
|
| 286 |
+
# 1. Live price lines
|
| 287 |
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4']
|
| 288 |
for i, (symbol, data) in enumerate(live_data.items()):
|
| 289 |
fig.add_trace(
|
|
|
|
| 291 |
x=data['timestamps'],
|
| 292 |
y=data['prices'],
|
| 293 |
mode='lines+markers',
|
| 294 |
+
name=f'{symbol}',
|
| 295 |
+
line=dict(color=colors[i % len(colors)], width=3),
|
| 296 |
marker=dict(size=6),
|
| 297 |
+
hovertemplate=f'<b>{symbol}</b><br>%{{x}}<br>$%{{y:.2f}}<br>Change: {data["change"]:+.2f}%<extra></extra>'
|
| 298 |
),
|
| 299 |
row=1, col=1
|
| 300 |
)
|
| 301 |
|
| 302 |
+
# 2. Performance bars
|
| 303 |
symbols = list(live_data.keys())
|
| 304 |
changes = [live_data[s]['change'] for s in symbols]
|
| 305 |
+
current_prices = [live_data[s]['current_price'] for s in symbols]
|
| 306 |
|
| 307 |
fig.add_trace(
|
| 308 |
go.Bar(
|
| 309 |
x=symbols,
|
| 310 |
y=changes,
|
| 311 |
+
marker_color=['#00CC96' if c > 0 else '#EF553B' for c in changes],
|
| 312 |
+
text=[f"${p:.2f}\n({c:+.2f}%)" for p, c in zip(current_prices, changes)],
|
| 313 |
textposition='auto',
|
| 314 |
+
name='Performance'
|
| 315 |
),
|
| 316 |
row=1, col=2
|
| 317 |
)
|
| 318 |
|
| 319 |
+
# 3. Recent changes
|
| 320 |
if live_data:
|
| 321 |
first_symbol = list(live_data.keys())[0]
|
| 322 |
data = live_data[first_symbol]
|
| 323 |
+
if len(data['prices']) > 3:
|
| 324 |
recent_changes = []
|
| 325 |
+
for i in range(1, min(4, len(data['prices']))):
|
| 326 |
change = ((data['prices'][-i] - data['prices'][-i-1]) / data['prices'][-i-1]) * 100
|
| 327 |
recent_changes.append(change)
|
| 328 |
|
| 329 |
fig.add_trace(
|
| 330 |
go.Scatter(
|
| 331 |
+
x=data['timestamps'][-3:],
|
| 332 |
y=recent_changes[::-1],
|
| 333 |
mode='lines+markers+text',
|
| 334 |
name='Recent Changes',
|
| 335 |
line=dict(color='#FECA57', width=4),
|
| 336 |
+
marker=dict(size=10),
|
| 337 |
text=[f"{c:+.2f}%" for c in recent_changes[::-1]],
|
| 338 |
textposition='top center'
|
| 339 |
),
|
| 340 |
row=2, col=1
|
| 341 |
)
|
| 342 |
|
| 343 |
+
# 4. Market sentiment
|
| 344 |
bullish = len([d for d in live_data.values() if d['change'] > 0])
|
| 345 |
bearish = len([d for d in live_data.values() if d['change'] < 0])
|
| 346 |
+
neutral = len([d for d in live_data.values() if d['change'] == 0])
|
| 347 |
|
| 348 |
fig.add_trace(
|
| 349 |
go.Pie(
|
| 350 |
+
labels=['Bullish', 'Bearish', 'Neutral'],
|
| 351 |
+
values=[bullish, bearish, neutral],
|
| 352 |
hole=0.4,
|
| 353 |
+
marker_colors=['#00CC96', '#EF553B', '#636EFA'],
|
| 354 |
+
name='Market Sentiment'
|
| 355 |
),
|
| 356 |
row=2, col=2
|
| 357 |
)
|
| 358 |
|
| 359 |
fig.update_layout(
|
| 360 |
height=800,
|
| 361 |
+
title_text=f"π AUTO-REFRESHING LIVE DASHBOARD β’ Update #{market_data.update_counter} β’ {datetime.now().strftime('%H:%M:%S')}",
|
| 362 |
template='plotly_dark',
|
| 363 |
+
showlegend=True,
|
| 364 |
+
font=dict(size=12)
|
| 365 |
)
|
| 366 |
|
| 367 |
return fig, live_data
|
| 368 |
|
| 369 |
def generate_analysis(symbol_input, live_data):
|
| 370 |
+
"""Generate dynamic analysis"""
|
| 371 |
if not symbol_input:
|
| 372 |
+
# Market overview
|
| 373 |
+
analysis_text = f"# π Live Market Overview\n\n"
|
| 374 |
+
analysis_text += f"**Last Update:** {datetime.now().strftime('%H:%M:%S')}\n"
|
| 375 |
+
analysis_text += f"**Total Updates:** {market_data.update_counter}\n\n"
|
| 376 |
+
|
| 377 |
for symbol, data in live_data.items():
|
| 378 |
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 379 |
decision = analysis['decision']
|
| 380 |
|
| 381 |
+
trend_emoji = "π’" if data['change'] > 0 else "π΄" if data['change'] < 0 else "π‘"
|
| 382 |
+
analysis_text += f"## {trend_emoji} {symbol}\n"
|
| 383 |
analysis_text += f"**Price:** ${data['current_price']:.2f} | "
|
| 384 |
analysis_text += f"**Change:** {data['change']:+.2f}%\n"
|
| 385 |
+
analysis_text += f"**Decision:** {decision['decision']} ({decision['confidence']}% confidence)\n\n"
|
| 386 |
|
| 387 |
return analysis_text
|
| 388 |
else:
|
| 389 |
# Specific symbol analysis
|
| 390 |
+
symbol = symbol_input.upper().strip()
|
| 391 |
if symbol in live_data:
|
| 392 |
analysis = trading_agents.analyze_market(symbol, live_data)
|
| 393 |
|
| 394 |
+
analysis_text = f"# π― {symbol} Live Analysis\n\n"
|
| 395 |
+
analysis_text += f"**Current Price:** ${live_data[symbol]['current_price']:.2f}\n"
|
| 396 |
+
analysis_text += f"**24h Change:** {live_data[symbol]['change']:+.2f}%\n"
|
| 397 |
+
analysis_text += f"**Last Updated:** {datetime.now().strftime('%H:%M:%S')}\n"
|
| 398 |
+
analysis_text += f"**Update Count:** #{market_data.update_counter}\n\n"
|
| 399 |
|
| 400 |
for agent_type, agent_analysis in analysis.items():
|
| 401 |
analysis_text += f"## {agent_analysis['emoji']} {agent_analysis['title']}\n"
|
|
|
|
| 403 |
|
| 404 |
return analysis_text
|
| 405 |
else:
|
| 406 |
+
return f"# β Symbol Not Found\n\n'{symbol}' is not in our tracked symbols. Try: {', '.join(market_data.symbols)}"
|
| 407 |
|
| 408 |
def update_interface(symbol_input=""):
|
| 409 |
+
"""Main update function called automatically"""
|
| 410 |
dashboard, live_data = create_live_dashboard()
|
| 411 |
analysis = generate_analysis(symbol_input, live_data)
|
| 412 |
return dashboard, analysis
|
| 413 |
|
| 414 |
+
# Create auto-refreshing interface
|
| 415 |
+
with gr.Blocks(
|
| 416 |
+
theme=gr.themes.Soft(
|
| 417 |
+
primary_hue="blue",
|
| 418 |
+
secondary_hue="slate"
|
| 419 |
+
),
|
| 420 |
+
title="π€ Auto-Refresh AI Trading System",
|
| 421 |
+
refresh_interval=10 # Auto-refresh every 10 seconds
|
| 422 |
+
) as demo:
|
| 423 |
|
| 424 |
gr.Markdown("""
|
| 425 |
# π€ Real-Time AI Trading System
|
| 426 |
+
## *Auto-Refresh Every 10 Seconds*
|
| 427 |
|
| 428 |
+
π **Charts and data update automatically - no clicking needed!**
|
| 429 |
""")
|
| 430 |
|
| 431 |
with gr.Row():
|
| 432 |
+
with gr.Column(scale=1):
|
| 433 |
+
gr.Markdown("### π― Stock Selection")
|
| 434 |
+
symbol_input = gr.Textbox(
|
| 435 |
+
label="Enter Stock Symbol",
|
| 436 |
+
placeholder="AAPL, TSLA... (empty for market overview)",
|
| 437 |
+
max_lines=1
|
| 438 |
+
)
|
| 439 |
+
gr.Markdown("""
|
| 440 |
+
**Tracked Stocks:** AAPL, GOOGL, MSFT, TSLA
|
| 441 |
+
|
| 442 |
+
β° **Auto-Refresh:** Every 10 seconds
|
| 443 |
+
π **Live Data:** Real-time price movements
|
| 444 |
+
π€ **AI Analysis:** Dynamic recommendations
|
| 445 |
+
""")
|
| 446 |
+
|
| 447 |
+
with gr.Column(scale=2):
|
| 448 |
+
gr.Markdown("### π Live Auto-Refresh Dashboard")
|
| 449 |
|
| 450 |
with gr.Tabs():
|
| 451 |
+
with gr.TabItem("π Live Charts"):
|
| 452 |
+
chart_output = gr.Plot(
|
| 453 |
+
label="Auto-Refreshing Market Data",
|
| 454 |
+
every=10 # Auto-refresh every 10 seconds
|
| 455 |
+
)
|
| 456 |
|
| 457 |
with gr.TabItem("π€ AI Analysis"):
|
| 458 |
+
analysis_output = gr.Markdown(
|
| 459 |
+
every=10 # Auto-refresh every 10 seconds
|
| 460 |
+
)
|
| 461 |
|
| 462 |
gr.Markdown(f"""
|
| 463 |
---
|
| 464 |
+
**π System Status:** Auto-Refresh Active β’ **π
Last Refresh:** {datetime.now().strftime('%H:%M:%S')}
|
| 465 |
+
**π‘ Tip:** The page automatically updates every 10 seconds with live data
|
| 466 |
+
**π― Tracking:** {len(market_data.symbols)} stocks with real-time AI analysis
|
| 467 |
""")
|
| 468 |
|
| 469 |
+
# Set up auto-refresh
|
| 470 |
+
demo.load(
|
| 471 |
fn=update_interface,
|
| 472 |
inputs=[symbol_input],
|
| 473 |
outputs=[chart_output, analysis_output]
|
| 474 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
|
| 476 |
+
# Launch the application
|
| 477 |
if __name__ == "__main__":
|
| 478 |
+
demo.launch(
|
| 479 |
+
server_name="0.0.0.0",
|
| 480 |
+
server_port=7860,
|
| 481 |
+
share=True
|
| 482 |
+
)
|