(feat) add traces for indicators and signals

This commit is contained in:
cardosofede
2024-05-21 11:29:19 -05:00
parent 939b0fc42d
commit 2f563c7a83
2 changed files with 65 additions and 13 deletions

View File

@@ -1,21 +1,43 @@
import pandas as pd
import pandas_ta as ta # noqa: F401 import pandas_ta as ta # noqa: F401
import plotly.graph_objects as go import plotly.graph_objects as go
from frontend.visualization import theme from frontend.visualization import theme
def get_bbands_traces(candles, bb_length, bb_std): def get_bbands_traces(df, bb_length, bb_std):
tech_colors = theme.get_color_scheme() tech_colors = theme.get_color_scheme()
candles.ta.bbands(length=bb_length, std=bb_std, append=True) df.ta.bbands(length=bb_length, std=bb_std, append=True)
bb_lower = f'BBL_{bb_length}_{bb_std}' bb_lower = f'BBL_{bb_length}_{bb_std}'
bb_middle = f'BBM_{bb_length}_{bb_std}' bb_middle = f'BBM_{bb_length}_{bb_std}'
bb_upper = f'BBU_{bb_length}_{bb_std}' bb_upper = f'BBU_{bb_length}_{bb_std}'
traces = [ traces = [
go.Scatter(x=candles.index, y=candles[bb_upper], line=dict(color=tech_colors['upper_band']), go.Scatter(x=df.index, y=df[bb_upper], line=dict(color=tech_colors['upper_band']),
name='Upper Band'), name='Upper Band'),
go.Scatter(x=candles.index, y=candles[bb_middle], line=dict(color=tech_colors['middle_band']), go.Scatter(x=df.index, y=df[bb_middle], line=dict(color=tech_colors['middle_band']),
name='Middle Band'), name='Middle Band'),
go.Scatter(x=candles.index, y=candles[bb_lower], line=dict(color=tech_colors['lower_band']), go.Scatter(x=df.index, y=df[bb_lower], line=dict(color=tech_colors['lower_band']),
name='Lower Band'), name='Lower Band'),
] ]
return traces return traces
def get_volume_trace(df):
df.index = pd.to_datetime(df.timestamp, unit='ms')
return go.Bar(x=df.index, y=df['volume'], name="Volume", marker_color=theme.get_color_scheme()["volume"], opacity=0.7)
def get_macd_traces(df, macd_fast, macd_slow, macd_signal):
tech_colors = theme.get_color_scheme()
df.ta.macd(fast=macd_fast, slow=macd_slow, signal=macd_signal, append=True)
macd = f'MACD_{macd_fast}_{macd_slow}_{macd_signal}'
macd_s = f'MACDs_{macd_fast}_{macd_slow}_{macd_signal}'
macd_hist = f'MACDh_{macd_fast}_{macd_slow}_{macd_signal}'
traces = [
go.Scatter(x=df.index, y=df[macd], line=dict(color=tech_colors['macd_line']),
name='MACD Line'),
go.Scatter(x=df.index, y=df[macd_s], line=dict(color=tech_colors['macd_signal']),
name='MACD Signal'),
go.Bar(x=df.index, y=df[macd_hist], name='MACD Histogram',
marker_color=df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"].apply(lambda x: '#FF6347' if x < 0 else '#32CD32'))
]
return traces

View File

@@ -3,9 +3,10 @@ import plotly.graph_objects as go
import pandas_ta as ta # noqa: F401 import pandas_ta as ta # noqa: F401
def add_bbands_with_threshold(fig, candles, bb_length, bb_std, bb_long_threshold, bb_short_threshold, row=1, col=1): def get_bollinger_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold):
tech_colors = theme.get_color_scheme() tech_colors = theme.get_color_scheme()
# Add Bollinger Bands # Add Bollinger Bands
candles = df.copy()
candles.ta.bbands(length=bb_length, std=bb_std, append=True) candles.ta.bbands(length=bb_length, std=bb_std, append=True)
# Generate conditions # Generate conditions
@@ -13,10 +14,39 @@ def add_bbands_with_threshold(fig, candles, bb_length, bb_std, bb_long_threshold
sell_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] > bb_short_threshold] sell_signals = candles[candles[f"BBP_{bb_length}_{bb_std}"] > bb_short_threshold]
# Signals plot # Signals plot
fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals['close'], mode='markers', 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'), marker=dict(color=tech_colors['buy_signal'], size=10, symbol='triangle-up'),
name='Buy Signal'), row=row, col=col) name='Buy Signal'),
fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals['close'], mode='markers', go.Scatter(x=sell_signals.index, y=sell_signals['close'], mode='markers',
marker=dict(color=tech_colors['sell_signal'], size=10, symbol='triangle-down'), marker=dict(color=tech_colors['sell_signal'], size=10, symbol='triangle-down'),
name='Sell Signal'), row=row, col=col) name='Sell Signal')
return fig ]
return traces
def get_macdbb_v1_signal_traces(df, bb_length, bb_std, bb_long_threshold, bb_short_threshold, macd_fast, macd_slow,
macd_signal):
tech_colors = theme.get_color_scheme()
# Add Bollinger Bands
df.ta.bbands(length=bb_length, std=bb_std, append=True)
# Add MACD
df.ta.macd(fast=macd_fast, slow=macd_slow, signal=macd_signal, append=True)
# Decision Logic
bbp = df[f"BBP_{bb_length}_{bb_std}"]
macdh = df[f"MACDh_{macd_fast}_{macd_slow}_{macd_signal}"]
macd = df[f"MACD_{macd_fast}_{macd_slow}_{macd_signal}"]
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