try fixing COB MA and COB data quality

This commit is contained in:
Dobromir Popov
2025-08-09 23:03:45 +03:00
parent 87193f3d6f
commit 31a41785d6
5 changed files with 174 additions and 95 deletions

View File

@ -1059,20 +1059,43 @@ class DataProvider:
return df
def get_historical_data(self, symbol: str, timeframe: str, limit: int = 1000, refresh: bool = False) -> Optional[pd.DataFrame]:
"""Get historical OHLCV data from cache only - no external API calls"""
"""Get historical OHLCV data.
- Prefer cached data for low latency.
- If cache is empty or refresh=True, fetch real data from exchanges.
- Never generate synthetic data.
"""
try:
# Only return cached data - never trigger external API calls
# Serve from cache when available
if symbol in self.cached_data and timeframe in self.cached_data[symbol]:
cached_df = self.cached_data[symbol][timeframe]
if not cached_df.empty:
# Return requested amount from cached data
if not cached_df.empty and not refresh:
return cached_df.tail(limit)
logger.warning(f"No cached data available for {symbol} {timeframe}")
# Cache empty or refresh requested: fetch real data now
df = self._fetch_from_binance(symbol, timeframe, limit)
if (df is None or df.empty):
df = self._fetch_from_mexc(symbol, timeframe, limit)
if df is not None and not df.empty:
df = self._ensure_datetime_index(df)
# Store/merge into cache
if symbol not in self.cached_data:
self.cached_data[symbol] = {}
if timeframe not in self.cached_data[symbol] or self.cached_data[symbol][timeframe].empty:
self.cached_data[symbol][timeframe] = df.tail(1500)
else:
combined_df = pd.concat([self.cached_data[symbol][timeframe], df], ignore_index=False)
combined_df = combined_df[~combined_df.index.duplicated(keep='last')]
combined_df = combined_df.sort_index()
self.cached_data[symbol][timeframe] = combined_df.tail(1500)
logger.info(f"Cached {len(self.cached_data[symbol][timeframe])} candles for {symbol} {timeframe}")
return self.cached_data[symbol][timeframe].tail(limit)
logger.warning(f"No real data available for {symbol} {timeframe} at request time")
return None
except Exception as e:
logger.error(f"Error getting cached data for {symbol} {timeframe}: {e}")
logger.error(f"Error getting historical data for {symbol} {timeframe}: {e}")
return None