Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import yfinance as yf
|
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
from datetime import datetime, timedelta
|
|
|
|
| 8 |
import time
|
| 9 |
import warnings
|
| 10 |
warnings.filterwarnings('ignore')
|
|
@@ -22,10 +23,7 @@ class RealTimeMarketData:
|
|
| 22 |
"""Generate realistic live market data with actual changes"""
|
| 23 |
current_time = datetime.now()
|
| 24 |
|
| 25 |
-
#
|
| 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 |
|
|
@@ -69,7 +67,7 @@ class RealTimeMarketData:
|
|
| 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*
|
| 73 |
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
| 74 |
|
| 75 |
# Calculate change
|
|
@@ -115,7 +113,7 @@ class RealTimeMarketData:
|
|
| 115 |
self.data_history[symbol].pop(0)
|
| 116 |
|
| 117 |
# Generate timestamps
|
| 118 |
-
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i*
|
| 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
|
|
@@ -129,23 +127,6 @@ class RealTimeMarketData:
|
|
| 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):
|
|
@@ -273,7 +254,7 @@ def create_live_dashboard():
|
|
| 273 |
subplot_titles=[
|
| 274 |
'π Live Price Movement (Auto-Refresh)',
|
| 275 |
'π Real-Time Performance',
|
| 276 |
-
'π
|
| 277 |
'π― Market Sentiment'
|
| 278 |
],
|
| 279 |
specs=[
|
|
@@ -407,9 +388,22 @@ def generate_analysis(symbol_input, live_data):
|
|
| 407 |
|
| 408 |
def update_interface(symbol_input=""):
|
| 409 |
"""Main update function called automatically"""
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
# Create auto-refreshing interface
|
| 415 |
with gr.Blocks(
|
|
@@ -417,13 +411,12 @@ with gr.Blocks(
|
|
| 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
|
| 427 |
|
| 428 |
π **Charts and data update automatically - no clicking needed!**
|
| 429 |
""")
|
|
@@ -439,7 +432,7 @@ with gr.Blocks(
|
|
| 439 |
gr.Markdown("""
|
| 440 |
**Tracked Stocks:** AAPL, GOOGL, MSFT, TSLA
|
| 441 |
|
| 442 |
-
β° **Auto-Refresh:** Every
|
| 443 |
οΏ½οΏ½ **Live Data:** Real-time price movements
|
| 444 |
π€ **AI Analysis:** Dynamic recommendations
|
| 445 |
""")
|
|
@@ -450,28 +443,37 @@ with gr.Blocks(
|
|
| 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
|
| 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__":
|
|
|
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
+
import asyncio
|
| 9 |
import time
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings('ignore')
|
|
|
|
| 23 |
"""Generate realistic live market data with actual changes"""
|
| 24 |
current_time = datetime.now()
|
| 25 |
|
| 26 |
+
# Always generate new data for demo purposes
|
|
|
|
|
|
|
|
|
|
| 27 |
self.update_counter += 1
|
| 28 |
self.last_update = current_time
|
| 29 |
|
|
|
|
| 67 |
# Ensure timestamps match
|
| 68 |
current_timestamps = self.timestamps[-len(self.data_history[symbol]):]
|
| 69 |
if len(current_timestamps) != len(self.data_history[symbol]):
|
| 70 |
+
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i*5)).strftime('%H:%M:%S')}"
|
| 71 |
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
| 72 |
|
| 73 |
# Calculate change
|
|
|
|
| 113 |
self.data_history[symbol].pop(0)
|
| 114 |
|
| 115 |
# Generate timestamps
|
| 116 |
+
current_timestamps = [f"{(datetime.now() - timedelta(seconds=i*5)).strftime('%H:%M:%S')}"
|
| 117 |
for i in range(len(self.data_history[symbol])-1, -1, -1)]
|
| 118 |
|
| 119 |
change = ((new_price - self.data_history[symbol][0]) / self.data_history[symbol][0]) * 100
|
|
|
|
| 127 |
'update_count': self.update_counter,
|
| 128 |
'last_updated': datetime.now().strftime('%H:%M:%S')
|
| 129 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
class AI_TradingAgents:
|
| 132 |
def __init__(self):
|
|
|
|
| 254 |
subplot_titles=[
|
| 255 |
'π Live Price Movement (Auto-Refresh)',
|
| 256 |
'π Real-Time Performance',
|
| 257 |
+
'π Recent Price Changes',
|
| 258 |
'π― Market Sentiment'
|
| 259 |
],
|
| 260 |
specs=[
|
|
|
|
| 388 |
|
| 389 |
def update_interface(symbol_input=""):
|
| 390 |
"""Main update function called automatically"""
|
| 391 |
+
try:
|
| 392 |
+
dashboard, live_data = create_live_dashboard()
|
| 393 |
+
analysis = generate_analysis(symbol_input, live_data)
|
| 394 |
+
return dashboard, analysis
|
| 395 |
+
except Exception as e:
|
| 396 |
+
# Return error state
|
| 397 |
+
error_fig = go.Figure()
|
| 398 |
+
error_fig.add_annotation(
|
| 399 |
+
text="π Updating... Please wait",
|
| 400 |
+
xref="paper", yref="paper",
|
| 401 |
+
x=0.5, y=0.5,
|
| 402 |
+
showarrow=False,
|
| 403 |
+
font=dict(size=20)
|
| 404 |
+
)
|
| 405 |
+
error_fig.update_layout(height=400, template='plotly_dark')
|
| 406 |
+
return error_fig, "# π Updating... Please wait"
|
| 407 |
|
| 408 |
# Create auto-refreshing interface
|
| 409 |
with gr.Blocks(
|
|
|
|
| 411 |
primary_hue="blue",
|
| 412 |
secondary_hue="slate"
|
| 413 |
),
|
| 414 |
+
title="π€ Auto-Refresh AI Trading System"
|
|
|
|
| 415 |
) as demo:
|
| 416 |
|
| 417 |
gr.Markdown("""
|
| 418 |
# π€ Real-Time AI Trading System
|
| 419 |
+
## *Auto-Refresh Every 5 Seconds*
|
| 420 |
|
| 421 |
π **Charts and data update automatically - no clicking needed!**
|
| 422 |
""")
|
|
|
|
| 432 |
gr.Markdown("""
|
| 433 |
**Tracked Stocks:** AAPL, GOOGL, MSFT, TSLA
|
| 434 |
|
| 435 |
+
β° **Auto-Refresh:** Every 5 seconds
|
| 436 |
οΏ½οΏ½ **Live Data:** Real-time price movements
|
| 437 |
π€ **AI Analysis:** Dynamic recommendations
|
| 438 |
""")
|
|
|
|
| 443 |
with gr.Tabs():
|
| 444 |
with gr.TabItem("π Live Charts"):
|
| 445 |
chart_output = gr.Plot(
|
| 446 |
+
label="Auto-Refreshing Market Data"
|
|
|
|
| 447 |
)
|
| 448 |
|
| 449 |
with gr.TabItem("π€ AI Analysis"):
|
| 450 |
+
analysis_output = gr.Markdown()
|
|
|
|
|
|
|
| 451 |
|
| 452 |
gr.Markdown(f"""
|
| 453 |
---
|
| 454 |
**π System Status:** Auto-Refresh Active β’ **π
Last Refresh:** {datetime.now().strftime('%H:%M:%S')}
|
| 455 |
+
**π‘ Tip:** The page automatically updates every 5 seconds with live data
|
| 456 |
**π― Tracking:** {len(market_data.symbols)} stocks with real-time AI analysis
|
| 457 |
""")
|
| 458 |
|
| 459 |
+
# Set up auto-refresh using JavaScript
|
| 460 |
demo.load(
|
| 461 |
fn=update_interface,
|
| 462 |
inputs=[symbol_input],
|
| 463 |
outputs=[chart_output, analysis_output]
|
| 464 |
)
|
| 465 |
+
|
| 466 |
+
# Add JavaScript for auto-refresh
|
| 467 |
+
demo.js = """
|
| 468 |
+
<script>
|
| 469 |
+
function autoRefresh() {
|
| 470 |
+
setTimeout(function() {
|
| 471 |
+
location.reload();
|
| 472 |
+
}, 5000); // Refresh every 5 seconds
|
| 473 |
+
}
|
| 474 |
+
window.onload = autoRefresh;
|
| 475 |
+
</script>
|
| 476 |
+
"""
|
| 477 |
|
| 478 |
# Launch the application
|
| 479 |
if __name__ == "__main__":
|