cyberosa commited on
Commit
0e538d2
·
1 Parent(s): 60949bd

nr trades as live metric

Browse files
app.py CHANGED
@@ -14,18 +14,19 @@ from tabs.trader_plots import (
14
  plot_trader_daily_metrics_by_market_creator,
15
  default_trader_metric,
16
  trader_metric_choices,
17
- trade_daily_metric_choices,
18
- default_daily_metric,
19
  get_metrics_text,
20
  plot_winning_metric_per_trader,
21
  get_interpretation_text,
22
  )
23
- from tabs.daily_graphs import get_current_week_data, plot_daily_metrics
 
 
 
 
 
24
  from scripts.utils import get_traders_family
25
  from scripts.trades_volume_per_market import plot_weekly_trades_volume_by_trader_family
26
- from tabs.market_plots import (
27
- plot_kl_div_per_market,
28
- )
29
 
30
 
31
  def get_logger():
@@ -86,10 +87,9 @@ def prepare_data():
86
 
87
  # nr-trades variable
88
  volume_trades_per_trader_and_market = (
89
- all_trades.groupby(["trader_address", "title"])["roi"].count().reset_index()
90
- )
91
- volume_trades_per_trader_and_market.rename(
92
- columns={"roi": "nr_trades_per_market"}, inplace=True
93
  )
94
 
95
  trader_agents_data = pd.merge(
@@ -100,7 +100,7 @@ def prepare_data():
100
  trader_agents_data["trader_family"] = trader_agents_data.apply(
101
  lambda x: get_traders_family(x), axis=1
102
  )
103
- print(trader_agents_data.trader_family.value_counts())
104
 
105
  trader_agents_data = trader_agents_data.sort_values(
106
  by="creation_timestamp", ascending=True
@@ -336,6 +336,13 @@ with demo:
336
  with gr.Column(scale=1):
337
  interpretation = get_interpretation_text()
338
 
 
 
 
 
 
 
 
339
  with gr.Row():
340
  gr.Markdown(
341
  "# Weekly volume of trades at each market per trader family"
 
14
  plot_trader_daily_metrics_by_market_creator,
15
  default_trader_metric,
16
  trader_metric_choices,
 
 
17
  get_metrics_text,
18
  plot_winning_metric_per_trader,
19
  get_interpretation_text,
20
  )
21
+ from tabs.daily_graphs import (
22
+ get_current_week_data,
23
+ plot_daily_metrics,
24
+ trade_daily_metric_choices,
25
+ default_daily_metric,
26
+ )
27
  from scripts.utils import get_traders_family
28
  from scripts.trades_volume_per_market import plot_weekly_trades_volume_by_trader_family
29
+ from tabs.market_plots import plot_kl_div_per_market, plot_total_bet_amount
 
 
30
 
31
 
32
  def get_logger():
 
87
 
88
  # nr-trades variable
89
  volume_trades_per_trader_and_market = (
90
+ all_trades.groupby(["trader_address", "title"])["roi"]
91
+ .count()
92
+ .reset_index(name="nr_trades_per_market")
 
93
  )
94
 
95
  trader_agents_data = pd.merge(
 
100
  trader_agents_data["trader_family"] = trader_agents_data.apply(
101
  lambda x: get_traders_family(x), axis=1
102
  )
103
+ print(trader_agents_data.head())
104
 
105
  trader_agents_data = trader_agents_data.sort_values(
106
  by="creation_timestamp", ascending=True
 
336
  with gr.Column(scale=1):
337
  interpretation = get_interpretation_text()
338
 
339
+ with gr.Row():
340
+ gr.Markdown(
341
+ "# Weekly total bet amount by market creator and trader type"
342
+ )
343
+ with gr.Row():
344
+ total_bet_amount = plot_total_bet_amount(trader_agents_data)
345
+
346
  with gr.Row():
347
  gr.Markdown(
348
  "# Weekly volume of trades at each market per trader family"
scripts/metrics.py CHANGED
@@ -19,12 +19,12 @@ def compute_metrics(
19
  agg_metrics["bet_amount"] = total_bet_amounts
20
  agg_metrics["nr_mech_calls"] = total_num_mech_calls
21
  agg_metrics["staking"] = trader_data.iloc[0].staking
 
22
  if live_metrics:
23
  return agg_metrics
24
  total_net_earnings = trader_data.net_earnings.sum()
25
  agg_metrics["net_earnings"] = total_net_earnings
26
  agg_metrics["earnings"] = trader_data.earnings.sum()
27
- agg_metrics["nr_trades"] = len(trader_data)
28
  total_fee_amounts = trader_data.mech_fee_amount.sum()
29
  total_costs = (
30
  total_bet_amounts
 
19
  agg_metrics["bet_amount"] = total_bet_amounts
20
  agg_metrics["nr_mech_calls"] = total_num_mech_calls
21
  agg_metrics["staking"] = trader_data.iloc[0].staking
22
+ agg_metrics["nr_trades"] = len(trader_data)
23
  if live_metrics:
24
  return agg_metrics
25
  total_net_earnings = trader_data.net_earnings.sum()
26
  agg_metrics["net_earnings"] = total_net_earnings
27
  agg_metrics["earnings"] = trader_data.earnings.sum()
 
28
  total_fee_amounts = trader_data.mech_fee_amount.sum()
29
  total_costs = (
30
  total_bet_amounts
tabs/daily_graphs.py CHANGED
@@ -7,6 +7,9 @@ import plotly.graph_objects as go
7
  from datetime import datetime, timedelta
8
  from tqdm import tqdm
9
 
 
 
 
10
 
11
  def plot_daily_trades(trades_df: pd.DataFrame) -> gr.Plot:
12
  # get daily trades
@@ -75,21 +78,24 @@ def plot_daily_metrics(
75
  if metric_name == "mech calls":
76
  metric_name = "nr_mech_calls"
77
  column_name = "nr_mech_calls"
78
- yaxis_title = "Nr of mech calls per trade"
 
 
 
79
  elif metric_name == "ROI":
80
  column_name = "roi"
81
- yaxis_title = "ROI (net profit/cost)"
82
  elif metric_name == "collateral amount":
83
  metric_name = "bet_amount"
84
  column_name = metric_name
85
- yaxis_title = "Collateral amount per trade (xDAI)"
86
  elif metric_name == "net earnings":
87
  metric_name = "net_earnings"
88
  column_name = metric_name
89
- yaxis_title = "Net profit per trade (xDAI)"
90
  else: # earnings
91
  column_name = metric_name
92
- yaxis_title = "Gross profit per trade (xDAI)"
93
 
94
  if trader_filter == "agent":
95
  trades_filtered = trades_df.loc[trades_df["staking"] != "non_agent"]
 
7
  from datetime import datetime, timedelta
8
  from tqdm import tqdm
9
 
10
+ trade_daily_metric_choices = ["mech calls", "collateral amount", "nr_trades"]
11
+ default_daily_metric = "collateral amount"
12
+
13
 
14
  def plot_daily_trades(trades_df: pd.DataFrame) -> gr.Plot:
15
  # get daily trades
 
78
  if metric_name == "mech calls":
79
  metric_name = "nr_mech_calls"
80
  column_name = "nr_mech_calls"
81
+ yaxis_title = "Total nr of mech calls per trader"
82
+ elif metric_name == "nr_trades":
83
+ column_name = metric_name
84
+ yaxis_title = "Total nr of trades per trader"
85
  elif metric_name == "ROI":
86
  column_name = "roi"
87
+ yaxis_title = "Total ROI (net profit/cost)"
88
  elif metric_name == "collateral amount":
89
  metric_name = "bet_amount"
90
  column_name = metric_name
91
+ yaxis_title = "Total bet amount per trader (xDAI)"
92
  elif metric_name == "net earnings":
93
  metric_name = "net_earnings"
94
  column_name = metric_name
95
+ yaxis_title = "Total net profit per trader (xDAI)"
96
  else: # earnings
97
  column_name = metric_name
98
+ yaxis_title = "Total gross profit per trader (xDAI)"
99
 
100
  if trader_filter == "agent":
101
  trades_filtered = trades_df.loc[trades_df["staking"] != "non_agent"]
tabs/market_plots.py CHANGED
@@ -85,3 +85,64 @@ def plot_kl_div_with_off_by(closed_markets: pd.DataFrame) -> gr.Plot:
85
  return gr.Plot(
86
  value=fig,
87
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  return gr.Plot(
86
  value=fig,
87
  )
88
+
89
+
90
+ def plot_total_bet_amount(trades_df: pd.DataFrame) -> gr.Plot:
91
+ """Plots the trade metrics."""
92
+ # Create binary staking category
93
+ trades_df["trader_type"] = trades_df["staking"].apply(
94
+ lambda x: "non_agent" if x == "non_agent" else "agent"
95
+ )
96
+
97
+ total_bet_amount = (
98
+ trades_df.groupby(
99
+ ["month_year_week", "market_creator", "trader_type"], sort=False
100
+ )["collateral_amount"]
101
+ .sum()
102
+ .reset_index(name="total_bet_amount")
103
+ )
104
+ color_mapping = [
105
+ "darkviolet",
106
+ "purple",
107
+ "goldenrod",
108
+ "darkgoldenrod",
109
+ "green",
110
+ "darkgreen",
111
+ ]
112
+ total_bet_amount["trader_market"] = total_bet_amount.apply(
113
+ lambda x: (x["trader_type"], x["market_creator"]), axis=1
114
+ )
115
+
116
+ fig = px.bar(
117
+ total_bet_amount,
118
+ x="month_year_week",
119
+ y="total_bet_amount",
120
+ color="trader_market",
121
+ color_discrete_sequence=color_mapping,
122
+ category_orders={
123
+ "market_creator": ["pearl", "quickstart", "all"],
124
+ "trader_market": [
125
+ ("agent", "pearl"),
126
+ ("non_agent", "pearl"),
127
+ ("agent", "quickstart"),
128
+ ("non_agent", "quickstart"),
129
+ ("agent", "all"),
130
+ ("non_agent", "all"),
131
+ ],
132
+ },
133
+ barmode="group",
134
+ facet_col="market_creator",
135
+ )
136
+
137
+ fig.update_layout(
138
+ xaxis_title="Week",
139
+ yaxis_title="Weekly total bet amount",
140
+ legend=dict(yanchor="top", y=0.5),
141
+ )
142
+ for axis in fig.layout:
143
+ if axis.startswith("xaxis"):
144
+ fig.layout[axis].update(title="Week")
145
+ fig.update_xaxes(tickformat="%b %d")
146
+ return gr.Plot(
147
+ value=fig,
148
+ )
tabs/trader_plots.py CHANGED
@@ -12,12 +12,6 @@ trader_metric_choices = [
12
  ]
13
  default_trader_metric = "ROI"
14
 
15
- trade_daily_metric_choices = [
16
- "mech calls",
17
- "collateral amount",
18
- ]
19
- default_daily_metric = "collateral amount"
20
-
21
 
22
  def get_metrics_text() -> gr.Markdown:
23
  metric_text = """
 
12
  ]
13
  default_trader_metric = "ROI"
14
 
 
 
 
 
 
 
15
 
16
  def get_metrics_text() -> gr.Markdown:
17
  metric_text = """