Skip to content

Commit

Permalink
Allow selection of bots for dashboard cumulative plot chart
Browse files Browse the repository at this point in the history
  • Loading branch information
froggleston committed Oct 2, 2024
1 parent 5859126 commit 6db01bb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 35 deletions.
20 changes: 17 additions & 3 deletions ftui/freqtext.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ DataTable {
/* margin: 2 2; */
}

#dsh-chart-bot-list {
dock: left;
width: 20;
height: 32;
overflow: auto;
height: 1fr;
border: darkgray;
/* scrollbar-gutter: stable; */
/* margin: 2 2; */
}

ListView Label {
padding: 1 2;
}
Expand Down Expand Up @@ -96,7 +107,6 @@ ListView Label {

#dash-collapsibles Collapsible Container {
padding-top: 1;
margin-left: -1;
margin-right: -1;
}

Expand All @@ -110,11 +120,15 @@ ListView Label {
scrollbar-gutter: stable;
}

#dsh-chart-container {
height: 32;
padding-left: 2;
}

#dash-cumprof-profit {
height: 32;
width: 100%;
padding-left: 1;
padding-right: 1;
padding-right: 2;
overflow: auto;
scrollbar-gutter: stable;
}
Expand Down
9 changes: 5 additions & 4 deletions ftui/ftui_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,11 @@ def dash_closed_trades_table(row_data, colours=FtuiColours()) -> Table:
return table


def dash_cumulative_profit_plot_data(trades, bot=None, pair=None):
if trades.shape[0] > 0 and bot is not None:
# Filter trades to one bot
trades = trades.loc[trades["Bot"] == bot].copy()
def dash_cumulative_profit_plot_data(trades, bot_list=[], pair=None):
if trades.shape[0] > 0 and bot_list:
# Filter trades to what's in the bot_list
# trades = trades.loc[trades["Bot"] == bot].copy()
trades = trades.loc[trades['Bot'].apply(lambda x: x in bot_list)].copy()

# Filter trades to one pair
if pair is not None:
Expand Down
87 changes: 59 additions & 28 deletions ftui/screens/dashboard_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@
Footer,
Header,
Label,
ListView,

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.12)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.12)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.9)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.10)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.9)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.10)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.11)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused

Check failure on line 15 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.11)

Ruff (F401)

ftui/screens/dashboard_screen.py:15:5: F401 `textual.widgets.ListView` imported but unused
SelectionList,
Static,
)
from textual.widgets.selection_list import Selection
from textual.worker import get_current_worker
from textual_plotext import PlotextPlot

import ftui.ftui_helpers as fth
from ftui.widgets.label_item import LabelItem

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.12)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.12)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.9)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.10)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.9)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.10)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, 3.11)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused

Check failure on line 24 in ftui/screens/dashboard_screen.py

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, 3.11)

Ruff (F401)

ftui/screens/dashboard_screen.py:24:37: F401 `ftui.widgets.label_item.LabelItem` imported but unused
from ftui.widgets.timed_screen import TimedScreen


Expand Down Expand Up @@ -61,14 +65,16 @@ def compose(self) -> ComposeResult:

with Container(id="dash-collapsibles"):
with Collapsible(title="All Open Trades", id="dsh-op-collap", collapsed=False):
yield Static(id="all-open-trades-table", classes="bg-static-default")
yield Static(id="all-open-trades-table", classes="bg-static-default collap-update")

with Collapsible(title="All Closed Trades", id="dsh-cl-collap", collapsed=True):
with Container(id="dash-closed-profit-container"):
yield Static(id="dash-closed-profit", classes="bg-static-default")
yield Static(id="dash-closed-profit", classes="bg-static-default collap-update")

with Collapsible(title="Cumulative Profit", id="dsh-cp-collap", collapsed=True):
yield PlotextPlot(id="dash-cumprof-profit", classes="bg-static-default")
with Container(id="dsh-chart-container"):
yield SelectionList(id="dsh-chart-bot-list", classes="bg-static-default")
yield PlotextPlot(id="dash-cumprof-profit", classes="bg-static-default collap-update")

# with Collapsible(title="Daily Trade Summary",
# id="dsh-dt-collap",
Expand All @@ -87,6 +93,12 @@ def on_mount(self) -> None:
for sd in summary_digits:
sd.loading = True

dsh_chart_bot_list = self.query_one("#dsh-chart-bot-list")
for n, cl in self.app.client_dict.items():
dsh_chart_bot_list.add_option(
Selection(n, n, True)
)

update_one_sec_render = self.set_interval(1, self.update_per_sec)
self.register_timer(f"{self.__class__.__name__}_1sec", update_one_sec_render)

Expand All @@ -109,7 +121,8 @@ async def update_per_five_sec(self):

dsh_cp_collap = self.query_one("#dsh-cp-collap")
if dsh_cp_collap.collapsed is False:
self.update_cumulative_profit_plot()
selected_bots = self.query_one("#dsh-chart-bot-list").selected
self.update_cumulative_profit_plot(bot_list = selected_bots)

def _render_open_trade_data(self, data, trading_mode="spot"):
row_data = []
Expand Down Expand Up @@ -410,46 +423,64 @@ def update_dashboard_all_trade_summary(self):
ats.loading = False

@work(group="dash_chart_worker", exclusive=True, thread=True)
def update_cumulative_profit_plot(self):
def update_cumulative_profit_plot(self, bot_list=None):
client_dfs = self.app.client_dfs

all_cum_data = pd.DataFrame()
if "all_closed" in client_dfs:
all_cum_data = fth.dash_cumulative_profit_plot_data(client_dfs["all_closed"])
if bot_list is None or not bot_list:
all_cum_data = fth.dash_cumulative_profit_plot_data(
client_dfs["all_closed"]
)
else:
all_cum_data = fth.dash_cumulative_profit_plot_data(
client_dfs["all_closed"],
bot_list=bot_list
)

if "plot_cumprof" in all_cum_data.columns:
chart_container = self.query_one("#dash-cumprof-profit")
cplt = chart_container.plt
dfmt = "Y-m-d"
cplt.date_form(dfmt)
if "plot_cumprof" in all_cum_data.columns:
chart_container = self.query_one("#dash-cumprof-profit")
cplt = chart_container.plt
cplt.clear_data()

all_cum_data.index = all_cum_data.index.tz_localize(None)
dfmt = "Y-m-d"
cplt.date_form(dfmt)

dates = cplt.datetimes_to_string(all_cum_data.index)
all_cum_data.index = all_cum_data.index.tz_localize(None)

cplt.plot(
dates,
all_cum_data["plot_cumprof"].values,
color=self.app.COLOURS.profit_chart_col,
)
dates = cplt.datetimes_to_string(all_cum_data.index)

cplt.ylim(
all_cum_data["plot_cumprof"].min() * 0.99,
all_cum_data["plot_cumprof"].max() * 1.01,
)
cplt.ylabel("Profit")
cplt.plot(
dates,
all_cum_data["plot_cumprof"].values,
color=self.app.COLOURS.profit_chart_col,
)

cplt.ylim(
all_cum_data["plot_cumprof"].min() * 0.99,
all_cum_data["plot_cumprof"].max() * 1.01,
)
cplt.ylabel("Profit")

worker = get_current_worker()
if not worker.is_cancelled:
self.app.call_from_thread(chart_container.refresh)
chart_container.loading = False

@on(SelectionList.SelectedChanged)
def update_cum_plot_from_list(self) -> None:
chart_container = self.query_one("#dash-cumprof-profit")
chart_container.loading = True

worker = get_current_worker()
if not worker.is_cancelled:
self.app.call_from_thread(chart_container.refresh)
chart_container.loading = False
selected_bots = self.query_one("#dsh-chart-bot-list").selected
self.update_cumulative_profit_plot(bot_list = selected_bots)

@on(Collapsible.Toggled)
def toggle_collapsible(self, event: Collapsible.Toggled) -> None:
event.stop()
collap = event.collapsible

collap_children = collap.query().filter(".bg-static-default")
collap_children = collap.query().filter(".collap-update")

if collap.collapsed is False:
for child in collap_children:
Expand Down

0 comments on commit 6db01bb

Please sign in to comment.