rosacastillo commited on
Commit
c50dc76
·
1 Parent(s): 16d3c27

Fixing metrics graph

Browse files
Files changed (2) hide show
  1. app.py +11 -9
  2. tabs/metrics.py +57 -0
app.py CHANGED
@@ -21,6 +21,7 @@ from tabs.metrics import (
21
  metric_choices,
22
  default_metric,
23
  plot_trade_details,
 
24
  )
25
 
26
  from tabs.tool_win import (
@@ -191,15 +192,15 @@ with demo:
191
  with gr.Row():
192
  gr.Markdown("# Trend of weekly trades")
193
  with gr.Row():
194
- with gr.Column(min_width=350):
195
  qs_trades_by_week = plot_trades_per_market_by_week(
196
  trades_df=trades_by_market, market_type="quickstart"
197
  )
198
- with gr.Column(min_width=350):
199
  pearl_trades_by_week = plot_trades_per_market_by_week(
200
  trades_df=trades_by_market, market_type="pearl"
201
  )
202
- with gr.Column(min_width=350):
203
  all_trades_by_week = plot_trades_per_market_by_week(
204
  trades_df=trades_by_market, market_type="all"
205
  )
@@ -207,16 +208,15 @@ with demo:
207
  with gr.Row():
208
  gr.Markdown("# Percentage of winning trades per week")
209
  with gr.Row():
210
- with gr.Column(min_width=350):
211
  qs_wtrades_by_week = plot_winning_trades_per_market_by_week(
212
  trades_df=winning_trades_by_market, market_type="quickstart"
213
  )
214
- with gr.Column(min_width=350):
215
- # gr.Markdown("# From Pearl market creator")
216
  pearl_wtrades_by_week = plot_winning_trades_per_market_by_week(
217
  trades_df=winning_trades_by_market, market_type="pearl"
218
  )
219
- with gr.Column(min_width=350):
220
  all_wtrades_by_week = plot_winning_trades_per_market_by_week(
221
  trades_df=winning_trades_by_market, market_type="all"
222
  )
@@ -230,12 +230,14 @@ with demo:
230
  value=default_metric,
231
  )
232
  with gr.Row():
233
- trade_details_plot = plot_trade_details(
234
  metric_name=default_metric, trades_df=trades_df
235
  )
236
 
237
  def update_trade_details(trade_detail):
238
- return plot_trade_details(metric_name=trade_detail, trades_df=trades_df)
 
 
239
 
240
  trade_details_selector.change(
241
  update_trade_details,
 
21
  metric_choices,
22
  default_metric,
23
  plot_trade_details,
24
+ plot2_trade_details,
25
  )
26
 
27
  from tabs.tool_win import (
 
192
  with gr.Row():
193
  gr.Markdown("# Trend of weekly trades")
194
  with gr.Row():
195
+ with gr.Column():
196
  qs_trades_by_week = plot_trades_per_market_by_week(
197
  trades_df=trades_by_market, market_type="quickstart"
198
  )
199
+ with gr.Column():
200
  pearl_trades_by_week = plot_trades_per_market_by_week(
201
  trades_df=trades_by_market, market_type="pearl"
202
  )
203
+ with gr.Column():
204
  all_trades_by_week = plot_trades_per_market_by_week(
205
  trades_df=trades_by_market, market_type="all"
206
  )
 
208
  with gr.Row():
209
  gr.Markdown("# Percentage of winning trades per week")
210
  with gr.Row():
211
+ with gr.Column():
212
  qs_wtrades_by_week = plot_winning_trades_per_market_by_week(
213
  trades_df=winning_trades_by_market, market_type="quickstart"
214
  )
215
+ with gr.Column():
 
216
  pearl_wtrades_by_week = plot_winning_trades_per_market_by_week(
217
  trades_df=winning_trades_by_market, market_type="pearl"
218
  )
219
+ with gr.Column():
220
  all_wtrades_by_week = plot_winning_trades_per_market_by_week(
221
  trades_df=winning_trades_by_market, market_type="all"
222
  )
 
230
  value=default_metric,
231
  )
232
  with gr.Row():
233
+ trade_details_plot = plot2_trade_details(
234
  metric_name=default_metric, trades_df=trades_df
235
  )
236
 
237
  def update_trade_details(trade_detail):
238
+ return plot2_trade_details(
239
+ metric_name=trade_detail, trades_df=trades_df
240
+ )
241
 
242
  trade_details_selector.change(
243
  update_trade_details,
tabs/metrics.py CHANGED
@@ -1,5 +1,6 @@
1
  import pandas as pd
2
  import gradio as gr
 
3
 
4
  metric_choices = [
5
  "mech calls",
@@ -58,6 +59,62 @@ def plot_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.LinePlot
58
  )
59
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  def plot_average_roi_per_market_by_week(trades_df: pd.DataFrame) -> gr.LinePlot:
62
 
63
  mean_roi_per_market_by_week = (
 
1
  import pandas as pd
2
  import gradio as gr
3
+ import plotly.express as px
4
 
5
  metric_choices = [
6
  "mech calls",
 
59
  )
60
 
61
 
62
+ def plot2_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.Plot:
63
+ """Plots the trade details for the given trade detail."""
64
+
65
+ if metric_name == "mech calls":
66
+ metric_name = "mech_calls"
67
+ column_name = "num_mech_calls"
68
+ yaxis_title = "Nr of mech calls per trade"
69
+ elif metric_name == "ROI":
70
+ column_name = "roi"
71
+ yaxis_title = "ROI (net profit/cost)"
72
+ elif metric_name == "collateral amount":
73
+ metric_name = "collateral_amount"
74
+ column_name = metric_name
75
+ yaxis_title = "Collateral amount per trade (xDAI)"
76
+ elif metric_name == "net earnings":
77
+ metric_name = "net_earnings"
78
+ column_name = metric_name
79
+ yaxis_title = "Net profit per trade (xDAI)"
80
+ else: # earnings
81
+ column_name = metric_name
82
+ yaxis_title = "Gross profit per trade (xDAI)"
83
+
84
+ # this is to filter out the data before 2023-09-01
85
+ trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
86
+ trades_filtered = (
87
+ trades_filtered.groupby("month_year_week", sort=False)[column_name]
88
+ .quantile([0.25, 0.5, 0.75])
89
+ .unstack()
90
+ )
91
+ trades_filtered.columns = trades_filtered.columns.astype(str)
92
+ trades_filtered.reset_index(inplace=True)
93
+ trades_filtered.columns = [
94
+ "month_year_week",
95
+ "25th_percentile",
96
+ "50th_percentile",
97
+ "75th_percentile",
98
+ ]
99
+ # reformat the data as percentile, date, value
100
+ trades_filtered = trades_filtered.melt(
101
+ id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
102
+ )
103
+
104
+ fig = px.line(
105
+ trades_filtered, x="month_year_week", y=metric_name, color="percentile"
106
+ )
107
+ fig.update_layout(
108
+ xaxis_title="Week",
109
+ yaxis_title=yaxis_title,
110
+ legend=dict(yanchor="top", y=0.5),
111
+ )
112
+ fig.update_xaxes(tickformat="%b %d\n%Y")
113
+ return gr.Plot(
114
+ value=fig,
115
+ )
116
+
117
+
118
  def plot_average_roi_per_market_by_week(trades_df: pd.DataFrame) -> gr.LinePlot:
119
 
120
  mean_roi_per_market_by_week = (