Skip to content

Commit

Permalink
(feat) add candles graph line mode for min interval resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasgaudino committed Nov 20, 2023
1 parent 723c948 commit 52c7067
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pages/strategy_performance/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
# Get Page Filtered Strategy Data
page_filtered_strategy_data = single_market_strategy_data.get_filtered_strategy_data(start_time_page, end_time_page)
page_performance_charts = PerformanceGraphs(page_filtered_strategy_data)
candles_chart = page_performance_charts.candles_graph(candles_df)
candles_chart = page_performance_charts.candles_graph(candles_df, interval=interval)

# Show auxiliary charts
intraday_tab, returns_tab, returns_data_tab, positions_tab, other_metrics_tab = st.tabs(["Intraday", "Returns", "Returns Data", "Positions", "Other Metrics"])
Expand Down
41 changes: 27 additions & 14 deletions utils/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
BULLISH_COLOR = "rgba(97, 199, 102, 0.9)"
BEARISH_COLOR = "rgba(255, 102, 90, 0.9)"
FEE_COLOR = "rgba(51, 0, 51, 0.9)"
MIN_INTERVAL_RESOLUTION = "1m"


class CandlesGraph:
def __init__(self, candles_df: pd.DataFrame, show_volume=True, extra_rows=1):
def __init__(self, candles_df: pd.DataFrame, line_mode=False, show_volume=True, extra_rows=1):
self.candles_df = candles_df
self.show_volume = show_volume
self.line_mode = line_mode
rows, heights = self.get_n_rows_and_heights(extra_rows)
self.rows = rows
specs = [[{"secondary_y": True}]] * rows
Expand All @@ -42,17 +44,27 @@ def figure(self):
return self.base_figure

def add_candles_graph(self):
self.base_figure.add_trace(
go.Candlestick(
x=self.candles_df.index,
open=self.candles_df['open'],
high=self.candles_df['high'],
low=self.candles_df['low'],
close=self.candles_df['close'],
name="OHLC"
),
row=1, col=1,
)
if self.line_mode:
self.base_figure.add_trace(
go.Scatter(x=self.candles_df.index,
y=self.candles_df['close'],
name="Close",
mode='lines',
line=dict(color='blue')),
row=1, col=1,
)
else:
self.base_figure.add_trace(
go.Candlestick(
x=self.candles_df.index,
open=self.candles_df['open'],
high=self.candles_df['high'],
low=self.candles_df['low'],
close=self.candles_df['close'],
name="OHLC"
),
row=1, col=1,
)

def add_buy_trades(self, orders_data: pd.DataFrame):
self.base_figure.add_trace(
Expand Down Expand Up @@ -531,8 +543,9 @@ def position_executor_summary_sunburst(self):
else:
return None

def candles_graph(self, candles: pd.DataFrame, show_volume=False, extra_rows=2):
cg = CandlesGraph(candles, show_volume=show_volume, extra_rows=extra_rows)
def candles_graph(self, candles: pd.DataFrame, interval="5m", show_volume=False, extra_rows=2):
line_mode = interval == MIN_INTERVAL_RESOLUTION
cg = CandlesGraph(candles, show_volume=show_volume, line_mode=line_mode, extra_rows=extra_rows)
cg.add_buy_trades(self.strategy_data.buys)
cg.add_sell_trades(self.strategy_data.sells)
cg.add_pnl(self.strategy_data, row=2)
Expand Down

0 comments on commit 52c7067

Please sign in to comment.