diff --git a/chart_generator.py b/chart_generator.py index 5654d0e..4954ef0 100644 --- a/chart_generator.py +++ b/chart_generator.py @@ -39,29 +39,37 @@ class ChartGenerator: fig, ax = plt.subplots(figsize=(12, 6), facecolor='#1e1e1e') ax.set_facecolor('#2d2d2d') + # Use index positions to remove weekend gaps + x_pos = list(range(len(closes))) + # Plot closing prices - ax.plot(dates, closes, color='#00d4ff', linewidth=2) + ax.plot(x_pos, closes, color='#00d4ff', linewidth=2) # Fill area under the line - ax.fill_between(dates, closes, alpha=0.3, color='#00d4ff') + ax.fill_between(x_pos, closes, alpha=0.3, color='#00d4ff') - # Format x-axis (detect if intraday or multi-day) + # Format x-axis with date labels (removing gaps) if len(dates) > 0: # Check if all data is from the same day first_date = dates[0].date() last_date = dates[-1].date() is_intraday = first_date == last_date + # Select tick positions (show ~7-10 labels) + tick_spacing = max(1, len(dates) // 7) + tick_indices = list(range(0, len(dates), tick_spacing)) + if tick_indices[-1] != len(dates) - 1: + tick_indices.append(len(dates) - 1) + if is_intraday: # Show time for intraday data - ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) - ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) + tick_labels = [dates[i].strftime('%H:%M') for i in tick_indices] else: # Show date for multi-day data - ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d')) - ax.xaxis.set_major_locator(mdates.AutoDateLocator()) + tick_labels = [dates[i].strftime('%m/%d') for i in tick_indices] - plt.xticks(rotation=45, ha='right') + ax.set_xticks(tick_indices) + ax.set_xticklabels(tick_labels, rotation=45, ha='right') # Grid styling ax.grid(True, alpha=0.2, color='#ffffff', linestyle='-', linewidth=0.5) @@ -126,35 +134,40 @@ class ChartGenerator: # Calculate bar width width = 0.6 - # Plot candlesticks + # Plot candlesticks using index positions (removes weekend gaps) for i in range(len(dates)): color = '#00ff88' if closes[i] >= opens[i] else '#ff4444' # High-low line - ax.plot([dates[i], dates[i]], [lows[i], highs[i]], color=color, linewidth=1) + ax.plot([i, i], [lows[i], highs[i]], color=color, linewidth=1) # Open-close rectangle height = abs(closes[i] - opens[i]) bottom = min(opens[i], closes[i]) - ax.bar(dates[i], height, width, bottom=bottom, color=color, alpha=0.8) + ax.bar(i, height, width, bottom=bottom, color=color, alpha=0.8) - # Format x-axis (detect if intraday or multi-day) + # Format x-axis with date labels (removing gaps) if len(dates) > 0: # Check if all data is from the same day first_date = dates[0].date() last_date = dates[-1].date() is_intraday = first_date == last_date + # Select tick positions (show ~7-10 labels) + tick_spacing = max(1, len(dates) // 7) + tick_indices = list(range(0, len(dates), tick_spacing)) + if tick_indices[-1] != len(dates) - 1: + tick_indices.append(len(dates) - 1) + if is_intraday: # Show time for intraday data - ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M')) - ax.xaxis.set_major_locator(mdates.HourLocator(interval=1)) + tick_labels = [dates[i].strftime('%H:%M') for i in tick_indices] else: # Show date for multi-day data - ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d')) - ax.xaxis.set_major_locator(mdates.AutoDateLocator()) + tick_labels = [dates[i].strftime('%m/%d') for i in tick_indices] - plt.xticks(rotation=45, ha='right') + ax.set_xticks(tick_indices) + ax.set_xticklabels(tick_labels, rotation=45, ha='right') # Grid styling ax.grid(True, alpha=0.2, color='#ffffff', linestyle='-', linewidth=0.5) diff --git a/examples/candlestick_chart_example.png b/examples/candlestick_chart_example.png index cf4732a..1612eb5 100644 Binary files a/examples/candlestick_chart_example.png and b/examples/candlestick_chart_example.png differ