diff --git a/frontend/visualization/indicators.py b/frontend/visualization/indicators.py index 0f21665..93f86bf 100644 --- a/frontend/visualization/indicators.py +++ b/frontend/visualization/indicators.py @@ -41,3 +41,42 @@ def get_macd_traces(df, macd_fast, macd_slow, macd_signal): marker_color=df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"].apply(lambda x: '#FF6347' if x < 0 else '#32CD32')) ] return traces + + +def get_supertrend_traces(df, length, multiplier): + tech_colors = theme.get_color_scheme() + df.ta.supertrend(length=length, multiplier=multiplier, append=True) + supertrend_d = f'SUPERTd_{length}_{multiplier}' + supertrend = f'SUPERT_{length}_{multiplier}' + df = df[df[supertrend] > 0] + + # Create segments for line with different colors + segments = [] + current_segment = {"x": [], "y": [], "color": None} + + for i in range(len(df)): + if i == 0 or df[supertrend_d].iloc[i] == df[supertrend_d].iloc[i - 1]: + current_segment["x"].append(df.index[i]) + current_segment["y"].append(df[supertrend].iloc[i]) + current_segment["color"] = tech_colors['buy'] if df[supertrend_d].iloc[i] == 1 else tech_colors['sell'] + else: + segments.append(current_segment) + current_segment = {"x": [df.index[i - 1], df.index[i]], + "y": [df[supertrend].iloc[i - 1], df[supertrend].iloc[i]], + "color": tech_colors['buy'] if df[supertrend_d].iloc[i] == 1 else tech_colors['sell']} + + segments.append(current_segment) + + # Create traces from segments + traces = [ + go.Scatter( + x=segment["x"], + y=segment["y"], + mode='lines', + line=dict(color=segment["color"], width=2), + name='SuperTrend' + ) for segment in segments + ] + + return traces + diff --git a/frontend/visualization/signals.py b/frontend/visualization/signals.py index 6762c7c..1cbec29 100644 --- a/frontend/visualization/signals.py +++ b/frontend/visualization/signals.py @@ -3,17 +3,8 @@ import plotly.graph_objects as go import pandas_ta as ta # noqa: F401 -def get_bollinger_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold): +def get_signal_traces(buy_signals, sell_signals): tech_colors = theme.get_color_scheme() - # Add Bollinger Bands - candles = df.copy() - candles.ta.bbands(length=bb_length, std=bb_std, append=True) - - # Generate conditions - buy_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] < bb_long_threshold] - sell_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] > bb_short_threshold] - - # Signals plot traces = [ go.Scatter(x=buy_signals.index, y=buy_signals['close'], mode='markers', marker=dict(color=tech_colors['buy_signal'], size=10, symbol='triangle-up'), @@ -24,6 +15,17 @@ def get_bollinger_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_ ] return traces +def get_bollinger_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold): + # Add Bollinger Bands + candles = df.copy() + candles.ta.bbands(length=bb_length, std=bb_std, append=True) + + # Generate conditions + buy_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] < bb_long_threshold] + sell_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] > bb_short_threshold] + + return get_signal_traces(buy_signals, sell_signals) + def get_macdbb_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold, macd_fast, macd_slow, macd_signal): @@ -40,13 +42,16 @@ def get_macdbb_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_sho buy_signals = df[(bbp < bb_long_threshold) & (macdh > 0) & (macd < 0)] sell_signals = df[(bbp > bb_short_threshold) & (macdh < 0) & (macd > 0)] - # Signals plot - traces = [ - go.Scatter(x=buy_signals.index, y=buy_signals['close'], mode='markers', - marker=dict(color=tech_colors['buy_signal'], size=10, symbol='triangle-up'), - name='Buy Signal'), - go.Scatter(x=sell_signals.index, y=sell_signals['close'], mode='markers', - marker=dict(color=tech_colors['sell_signal'], size=10, symbol='triangle-down'), - name='Sell Signal') - ] - return traces + return get_signal_traces(buy_signals, sell_signals) + + +def get_supertrend_v1_signal_traces(df, length, multiplier, percentage_threshold): + # Add indicators + df.ta.supertrend(length=length, multiplier=multiplier, append=True) + df["percentage_distance"] = abs(df["close"] - df[f"SUPERT_{length}_{multiplier}"]) / df["close"] + + # Generate long and short conditions + buy_signals = df[(df[f"SUPERTd_{length}_{multiplier}"] == 1) & (df["percentage_distance"] < percentage_threshold)] + sell_signals = df[(df[f"SUPERTd_{length}_{multiplier}"] == -1) & (df["percentage_distance"] < percentage_threshold)] + + return get_signal_traces(buy_signals, sell_signals)