(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 plotly.graph_objects as go
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()
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_middle = f'BBM_{bb_length}_{bb_std}'
bb_upper = f'BBU_{bb_length}_{bb_std}'
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'),
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'),
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'),
]
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