diff --git a/src/nailab/ui/equitygraph.py b/src/nailab/ui/equitygraph.py new file mode 100644 index 0000000..d306b51 --- /dev/null +++ b/src/nailab/ui/equitygraph.py @@ -0,0 +1,39 @@ + +from gi.repository import Gtk + +class EquityGraph(Gtk.Misc): + + def __init__(self): + super().__init__() + self.trades = [] + self.cumulative_pnl = [] + + def do_draw(self, cr): + bg_color = self.get_style_context().get_background_color(Gtk.StateFlags.NORMAL) + cr.set_source_rgba(*list(bg_color)) + cr.paint() + + allocation = self.get_allocation() + if len(self.cumulative_pnl) > 0: + fg_color = self.get_style_context().get_color(Gtk.StateFlags.NORMAL) + cr.set_source_rgba(*list(fg_color)); + cr.set_line_width(2) + + min_equity = min(self.cumulative_pnl) + max_equity = max(self.cumulative_pnl) + + kx = allocation.width / len(self.cumulative_pnl) + ky = 0.8 * allocation.height / (max_equity - min_equity) + + cr.move_to(0, allocation.height * 0.9 - (self.cumulative_pnl[0] - min_equity) * ky) + for i, x in enumerate(self.cumulative_pnl[1:]): + cr.line_to((i + 1) * kx, allocation.height * 0.9 - (x - min_equity) * ky) + cr.stroke() + + def set_trades(self, trades): + self.trades = trades + s = 0 + self.cumulative_pnl = [] + for trade in trades: + s += trade['pnl'] + self.cumulative_pnl.append(s) diff --git a/src/nailab/ui/resultstable.py b/src/nailab/ui/resultstable.py index 41a8a2a..b5b705e 100644 --- a/src/nailab/ui/resultstable.py +++ b/src/nailab/ui/resultstable.py @@ -3,6 +3,8 @@ from gi.repository import Gtk from prettytable import PrettyTable +from .equitygraph import EquityGraph + def render_float(a): return "{:.3f}".format(a) @@ -24,6 +26,9 @@ class ResultsTableWidget(Gtk.Notebook): self.text_result.set_buffer(self.buffer) self.append_page(self.text_result, Gtk.Label('Statistics')) + self.equity_graph = EquityGraph() + self.append_page(self.equity_graph, Gtk.Label('Equity')) + style_ctx = self.text_result.get_style_context() self.provider = Gtk.CssProvider() self.provider.load_from_data(b'GtkTextView { font-family: "Monospace"; }') @@ -31,7 +36,7 @@ class ResultsTableWidget(Gtk.Notebook): def set_results(self, results, trades): self.buffer.set_text(self.generate_plain_text(results)) - print(trades) + self.equity_graph.set_trades(trades) def generate_plain_text(self, stats):