better chart

This commit is contained in:
Dobromir Popov
2025-06-25 02:24:25 +03:00
parent cfb53d0fe9
commit 8770038e20
2 changed files with 71 additions and 9 deletions

View File

@ -319,23 +319,47 @@ class CleanTradingDashboard:
def _create_price_chart(self, symbol: str) -> go.Figure:
"""Create 1-minute main chart with 1-second mini chart - Updated every second"""
try:
# Get 1-minute data (main chart) - FIXED for real-time updates
# First try to create 1m bars from WebSocket 1s data
# FIXED: Merge historical + live data instead of replacing
# 1. Get historical 1-minute data as base (180 candles = 3 hours)
df_historical = self.data_provider.get_historical_data(symbol, '1m', limit=180)
# 2. Get WebSocket 1s data and convert to 1m bars
ws_data_raw = self._get_websocket_chart_data(symbol, 'raw')
df_live = None
if ws_data_raw is not None and len(ws_data_raw) > 60:
# Resample 1s data to 1m bars for real-time updating 1m chart
df_main = ws_data_raw.resample('1min').agg({
# Resample 1s data to 1m bars
df_live = ws_data_raw.resample('1min').agg({
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum'
}).dropna().tail(180) # Last 3 hours
main_source = "WebSocket 1m (Real-time)"
}).dropna()
# 3. Merge historical + live data intelligently
if df_historical is not None and not df_historical.empty:
if df_live is not None and not df_live.empty:
# Find overlap point - where live data starts
live_start = df_live.index[0]
# Keep historical data up to live data start
df_historical_clean = df_historical[df_historical.index < live_start]
# Combine: historical (older) + live (newer)
df_main = pd.concat([df_historical_clean, df_live]).tail(180)
main_source = f"Historical + Live ({len(df_historical_clean)} + {len(df_live)} bars)"
else:
# No live data, use historical only
df_main = df_historical
main_source = "Historical 1m"
elif df_live is not None and not df_live.empty:
# No historical data, use live only
df_main = df_live.tail(180)
main_source = "Live 1m (WebSocket)"
else:
# Fallback to historical 1-minute data (3 hours)
df_main = self.data_provider.get_historical_data(symbol, '1m', limit=180)
main_source = "Historical 1m"
# No data at all
df_main = None
main_source = "No data"
# Get 1-second data (mini chart)
ws_data_1s = self._get_websocket_chart_data(symbol, '1s')