tter cache

This commit is contained in:
Dobromir Popov 2025-05-29 23:17:03 +03:00
parent 7e4b29fdc2
commit 5a30c5721d

View File

@ -278,6 +278,30 @@ class TradingDashboard:
# Load available models for real trading # Load available models for real trading
self._load_available_models() self._load_available_models()
# Preload essential data to prevent excessive API calls during dashboard updates
logger.info("Preloading essential market data to cache...")
try:
# Preload key timeframes for main symbols to ensure cache is populated
symbols_to_preload = self.config.symbols or ['ETH/USDT', 'BTC/USDT']
timeframes_to_preload = ['1m', '1h', '1d'] # Skip 1s since we use WebSocket for that
for symbol in symbols_to_preload[:2]: # Limit to first 2 symbols
for timeframe in timeframes_to_preload:
try:
# Load data into cache (refresh=True for initial load, then cache will be used)
df = self.data_provider.get_historical_data(symbol, timeframe, limit=100, refresh=True)
if df is not None and not df.empty:
logger.info(f"Preloaded {len(df)} {timeframe} bars for {symbol}")
else:
logger.warning(f"Failed to preload data for {symbol} {timeframe}")
except Exception as e:
logger.warning(f"Error preloading {symbol} {timeframe}: {e}")
logger.info("Preloading completed - cache populated for frequent queries")
except Exception as e:
logger.warning(f"Error during preloading: {e}")
# Create Dash app # Create Dash app
self.app = dash.Dash(__name__, external_stylesheets=[ self.app = dash.Dash(__name__, external_stylesheets=[
'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css', 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css',
@ -829,7 +853,7 @@ class TradingDashboard:
else: else:
# Only try fresh API call if we have no data at all # Only try fresh API call if we have no data at all
try: try:
fresh_data = self.data_provider.get_historical_data(symbol, '1m', limit=1, refresh=True) fresh_data = self.data_provider.get_historical_data(symbol, '1m', limit=1, refresh=False)
if fresh_data is not None and not fresh_data.empty: if fresh_data is not None and not fresh_data.empty:
current_price = float(fresh_data['close'].iloc[-1]) current_price = float(fresh_data['close'].iloc[-1])
data_source = "API" data_source = "API"
@ -4571,7 +4595,7 @@ class TradingDashboard:
symbol=target_symbol, symbol=target_symbol,
timeframe='1m', timeframe='1m',
limit=50, limit=50,
refresh=True refresh=False # Use cache to prevent excessive API calls
) )
else: else:
# Get historical data for other timeframes # Get historical data for other timeframes
@ -4579,7 +4603,7 @@ class TradingDashboard:
symbol=target_symbol, symbol=target_symbol,
timeframe=timeframe, timeframe=timeframe,
limit=50, # Last 50 bars limit=50, # Last 50 bars
refresh=True refresh=False # Use cache to prevent excessive API calls
) )
if df is not None and not df.empty and len(df) >= 10: if df is not None and not df.empty and len(df) >= 10:
@ -4608,7 +4632,7 @@ class TradingDashboard:
symbol='BTC/USDT', symbol='BTC/USDT',
timeframe='1m', timeframe='1m',
limit=50, limit=50,
refresh=True refresh=False # Use cache to prevent excessive API calls
) )
if btc_1s_df is not None and not btc_1s_df.empty and len(btc_1s_df) >= 10: if btc_1s_df is not None and not btc_1s_df.empty and len(btc_1s_df) >= 10: