diff --git a/realtime.py b/realtime.py index 25b2e4e..7323f3d 100644 --- a/realtime.py +++ b/realtime.py @@ -1661,7 +1661,7 @@ class RealTimeChart: interval = interval_data.get('interval', 1) logger.debug(f"Updating chart with interval {interval}") - # Get candlesticks data + # Get candlesticks data for the selected interval try: df = self.tick_storage.get_candles(interval_seconds=interval) if df.empty and self.ohlcv_cache[f'{interval}s' if interval < 60 else '1m'] is not None: @@ -1670,15 +1670,56 @@ class RealTimeChart: logger.error(f"Error getting candles: {str(e)}") df = pd.DataFrame() - # Create basic layout + # Get data for other timeframes (1m, 1h, 1d) + df_1m = None + df_1h = None + df_1d = None + + try: + # Get 1m candles + if self.ohlcv_cache['1m'] is not None and not self.ohlcv_cache['1m'].empty: + df_1m = self.ohlcv_cache['1m'].copy() + else: + df_1m = self.tick_storage.get_candles(interval_seconds=60) + + # Get 1h candles + if self.ohlcv_cache['1h'] is not None and not self.ohlcv_cache['1h'].empty: + df_1h = self.ohlcv_cache['1h'].copy() + else: + df_1h = self.tick_storage.get_candles(interval_seconds=3600) + + # Get 1d candles + if self.ohlcv_cache['1d'] is not None and not self.ohlcv_cache['1d'].empty: + df_1d = self.ohlcv_cache['1d'].copy() + else: + df_1d = self.tick_storage.get_candles(interval_seconds=86400) + + # Limit the number of candles to display + if df_1m is not None and not df_1m.empty: + df_1m = df_1m.tail(100) + if df_1h is not None and not df_1h.empty: + df_1h = df_1h.tail(48) # Last 2 days + if df_1d is not None and not df_1d.empty: + df_1d = df_1d.tail(30) # Last month + + except Exception as e: + logger.error(f"Error getting additional timeframes: {str(e)}") + + # Create layout with 5 rows (main chart, volume, 1m, 1h, 1d) fig = make_subplots( - rows=2, cols=1, - vertical_spacing=0.08, - subplot_titles=(f'{self.symbol} Price ({interval}s)', 'Volume'), - row_heights=[0.8, 0.2] + rows=5, cols=1, + vertical_spacing=0.03, + subplot_titles=( + f'{self.symbol} Price ({interval}s)', + 'Volume', + '1-Minute Chart', + '1-Hour Chart', + '1-Day Chart' + ), + row_heights=[0.4, 0.15, 0.15, 0.15, 0.15] ) - # Add candlestick chart + # Add candlestick chart for main timeframe if not df.empty and 'open' in df.columns: # Candlestick chart fig.add_trace( @@ -1717,6 +1758,54 @@ class RealTimeChart: row=2, col=1 ) + # Add 1m chart + if df_1m is not None and not df_1m.empty and 'open' in df_1m.columns: + fig.add_trace( + go.Candlestick( + x=df_1m.index, + open=df_1m['open'], + high=df_1m['high'], + low=df_1m['low'], + close=df_1m['close'], + name='1m', + showlegend=False + ), + row=3, col=1 + ) + fig.update_xaxes(title_text="", row=3, col=1) + + # Add 1h chart + if df_1h is not None and not df_1h.empty and 'open' in df_1h.columns: + fig.add_trace( + go.Candlestick( + x=df_1h.index, + open=df_1h['open'], + high=df_1h['high'], + low=df_1h['low'], + close=df_1h['close'], + name='1h', + showlegend=False + ), + row=4, col=1 + ) + fig.update_xaxes(title_text="", row=4, col=1) + + # Add 1d chart + if df_1d is not None and not df_1d.empty and 'open' in df_1d.columns: + fig.add_trace( + go.Candlestick( + x=df_1d.index, + open=df_1d['open'], + high=df_1d['high'], + low=df_1d['low'], + close=df_1d['close'], + name='1d', + showlegend=False + ), + row=5, col=1 + ) + fig.update_xaxes(title_text="", row=5, col=1) + # Add trading info annotation if available if hasattr(self, 'current_signal') and self.current_signal: signal_color = "#33DD33" if self.current_signal == "BUY" else "#FF4444" if self.current_signal == "SELL" else "#BBBBBB" @@ -1750,16 +1839,25 @@ class RealTimeChart: title_text=f"{self.symbol} Real-Time Data", title_x=0.5, xaxis_rangeslider_visible=False, - height=700, + height=1000, # Increased height to accommodate all charts template='plotly_dark', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(25,25,50,1)' ) - # Update axes styling + # Update axes styling for all subplots fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='rgba(128,128,128,0.2)') fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='rgba(128,128,128,0.2)') + # Hide rangesliders for all candlestick charts + fig.update_layout( + xaxis_rangeslider_visible=False, + xaxis2_rangeslider_visible=False, + xaxis3_rangeslider_visible=False, + xaxis4_rangeslider_visible=False, + xaxis5_rangeslider_visible=False + ) + return fig except Exception as e: