diff --git a/ANNOTATE/web/app.py b/ANNOTATE/web/app.py index 48028d0..6f4d71c 100644 --- a/ANNOTATE/web/app.py +++ b/ANNOTATE/web/app.py @@ -1054,31 +1054,42 @@ class AnnotationDashboard: @self.server.route('/api/recalculate-pivots', methods=['POST']) def recalculate_pivots(): - """Recalculate pivot points for merged data""" + """Recalculate pivot points for merged data using cached data from data_loader""" try: data = request.get_json() symbol = data.get('symbol', 'ETH/USDT') timeframe = data.get('timeframe') - timestamps = data.get('timestamps', []) - ohlcv_data = data.get('ohlcv', {}) + # We don't use timestamps/ohlcv from frontend anymore, we use our own consistent data source - if not timeframe or not timestamps: + if not timeframe: return jsonify({ 'success': False, - 'error': {'code': 'INVALID_REQUEST', 'message': 'Missing timeframe or timestamps'} + 'error': {'code': 'INVALID_REQUEST', 'message': 'Missing timeframe'} }) - logger.info(f" Recalculating pivots for {symbol} {timeframe} with {len(timestamps)} candles") + logger.info(f" Recalculating pivots for {symbol} {timeframe} using backend data") - # Convert to DataFrame - df = pd.DataFrame({ - 'open': ohlcv_data.get('open', []), - 'high': ohlcv_data.get('high', []), - 'low': ohlcv_data.get('low', []), - 'close': ohlcv_data.get('close', []), - 'volume': ohlcv_data.get('volume', []) - }) - df.index = pd.to_datetime(timestamps) + if not self.data_loader: + return jsonify({ + 'success': False, + 'error': {'code': 'DATA_LOADER_UNAVAILABLE', 'message': 'Data loader not available'} + }) + + # Fetch latest data from data_loader (which should have the updated cache/DB from previous calls) + # We get enough history for proper pivot calculation + df = self.data_loader.get_data( + symbol=symbol, + timeframe=timeframe, + limit=2500, # Enough for context + direction='latest' + ) + + if df is None or df.empty: + logger.warning(f"No data found for {symbol} {timeframe} to recalculate pivots") + return jsonify({ + 'success': True, + 'pivot_markers': {} + }) # Recalculate pivot markers pivot_markers = self._get_pivot_markers_for_timeframe(symbol, timeframe, df) @@ -1147,8 +1158,10 @@ class AnnotationDashboard: if df is not None and not df.empty: logger.info(f" {timeframe}: {len(df)} candles ({df.index[0]} to {df.index[-1]})") - # Get pivot points for this timeframe - pivot_markers = self._get_pivot_markers_for_timeframe(symbol, timeframe, df) + # Get pivot points for this timeframe (only if we have enough context) + pivot_markers = {} + if len(df) >= 50: + pivot_markers = self._get_pivot_markers_for_timeframe(symbol, timeframe, df) # Convert to format suitable for Plotly chart_data[timeframe] = { diff --git a/ANNOTATE/web/static/js/chart_manager.js b/ANNOTATE/web/static/js/chart_manager.js index 912e03e..68f1270 100644 --- a/ANNOTATE/web/static/js/chart_manager.js +++ b/ANNOTATE/web/static/js/chart_manager.js @@ -1616,22 +1616,18 @@ class ChartManager { */ async recalculatePivots(timeframe, data) { try { + // Don't recalculate if we don't have enough data + if (data.timestamps.length < 50) return; + console.log(` Recalculating pivots for ${timeframe} with ${data.timestamps.length} candles...`); + // Optimized: Only send symbol and timeframe, backend uses its own data const response = await fetch('/api/recalculate-pivots', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ symbol: window.appState?.currentSymbol || 'ETH/USDT', - timeframe: timeframe, - timestamps: data.timestamps, - ohlcv: { - open: data.open, - high: data.high, - low: data.low, - close: data.close, - volume: data.volume - } + timeframe: timeframe }) });