Files
daytrade/charting/indicators.py

55 lines
2.3 KiB
Python

from bokeh.io import output_file, save
from bokeh.plotting import figure, show
from bokeh.layouts import column
from bokeh.models import Range1d, Span, ColumnDataSource, HoverTool
from loguru import logger
from charting import repository
def generate_macd_with_rsi_chart(data, filename=None):
"""
Generates a chart displaying MACD, RSI, and Close data with hover tools.
"""
data = repository.fill_and_trim_data(data)
shared_x_range = figure(x_axis_type="datetime").x_range
# Create Data Sources
source_macd = ColumnDataSource({'Date': data['Date'], 'MACD': data['MACD'], 'MACD_Signal': data['MACD_Signal'], 'MACD_Hist': data['MACD_Hist']})
source_close = ColumnDataSource({'Date': data['Date'], 'Close': data['Close']})
source_rsi = ColumnDataSource({'Date': data['Date'], 'RSI': data['RSI']})
# Create chart for MACD (multiple lines with histogram)
macd_lines = [
{'y_column': 'MACD', 'color': 'blue', 'legend_label': 'MACD'},
{'y_column': 'MACD_Signal', 'color': 'red', 'legend_label': 'Signal'}
]
macd_histogram = {'y_column': 'MACD_Hist', 'color': 'green', 'legend_label': 'Histogram'}
hover_tooltips_macd = [
('Date', '@Date{%F}'),
('MACD', '@MACD{0.2f}'),
('Signal', '@MACD_Signal{0.2f}'),
('Histogram', '@MACD_Hist{0.2f}')
]
p_macd = repository.create_chart_for_macd("MACD", 800, 300, shared_x_range, source_macd, macd_lines, macd_histogram, hover_tooltips_macd)
# Create Close Price and RSI charts
p_close = repository.create_chart_with_single_line("Price Data", 800, 300, shared_x_range, source_close, 'Close', [('Date', '@Date{%F}'), ('Close', '$@Close{0.2f}')], "blue", "Close Price")
p_rsi = repository.create_chart_with_single_line("RSI", 800, 200, shared_x_range, source_rsi, 'RSI', [('Date', '@Date{%F}'), ('RSI', '@RSI{0.2f}')], "orange", "RSI")
# Add horizontal lines for RSI thresholds
p_rsi.add_layout(Span(location=30, dimension='width', line_color='green', line_dash='dashed', line_width=1))
p_rsi.add_layout(Span(location=70, dimension='width', line_color='red', line_dash='dashed', line_width=1))
# Combine charts
p = column(p_close, p_macd, p_rsi)
# Save or show
if filename:
output_file(filename)
save(p)
else:
show(p)