Commit
·
1f895f9
1
Parent(s):
6145542
updating the metrics graph
Browse files- app.py +11 -11
- tabs/metrics.py +92 -20
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from tabs.metrics import (
|
|
| 22 |
default_metric,
|
| 23 |
plot_trade_details,
|
| 24 |
plot2_trade_details,
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
from tabs.tool_win import (
|
|
@@ -192,15 +193,15 @@ with demo:
|
|
| 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,15 +209,15 @@ with demo:
|
|
| 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,14 +231,13 @@ with demo:
|
|
| 230 |
value=default_metric,
|
| 231 |
)
|
| 232 |
with gr.Row():
|
| 233 |
-
trade_details_plot =
|
| 234 |
-
metric_name=default_metric,
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
def update_trade_details(trade_detail):
|
| 238 |
-
return
|
| 239 |
-
metric_name=trade_detail, trades_df=trades_df
|
| 240 |
-
)
|
| 241 |
|
| 242 |
trade_details_selector.change(
|
| 243 |
update_trade_details,
|
|
|
|
| 22 |
default_metric,
|
| 23 |
plot_trade_details,
|
| 24 |
plot2_trade_details,
|
| 25 |
+
plot_trade_metrics,
|
| 26 |
)
|
| 27 |
|
| 28 |
from tabs.tool_win import (
|
|
|
|
| 193 |
with gr.Row():
|
| 194 |
gr.Markdown("# Trend of weekly trades")
|
| 195 |
with gr.Row():
|
| 196 |
+
with gr.Column(min_width=400):
|
| 197 |
qs_trades_by_week = plot_trades_per_market_by_week(
|
| 198 |
trades_df=trades_by_market, market_type="quickstart"
|
| 199 |
)
|
| 200 |
+
with gr.Column(min_width=400):
|
| 201 |
pearl_trades_by_week = plot_trades_per_market_by_week(
|
| 202 |
trades_df=trades_by_market, market_type="pearl"
|
| 203 |
)
|
| 204 |
+
with gr.Column(min_width=400):
|
| 205 |
all_trades_by_week = plot_trades_per_market_by_week(
|
| 206 |
trades_df=trades_by_market, market_type="all"
|
| 207 |
)
|
|
|
|
| 209 |
with gr.Row():
|
| 210 |
gr.Markdown("# Percentage of winning trades per week")
|
| 211 |
with gr.Row():
|
| 212 |
+
with gr.Column(min_width=400):
|
| 213 |
qs_wtrades_by_week = plot_winning_trades_per_market_by_week(
|
| 214 |
trades_df=winning_trades_by_market, market_type="quickstart"
|
| 215 |
)
|
| 216 |
+
with gr.Column(min_width=400):
|
| 217 |
pearl_wtrades_by_week = plot_winning_trades_per_market_by_week(
|
| 218 |
trades_df=winning_trades_by_market, market_type="pearl"
|
| 219 |
)
|
| 220 |
+
with gr.Column(min_width=400):
|
| 221 |
all_wtrades_by_week = plot_winning_trades_per_market_by_week(
|
| 222 |
trades_df=winning_trades_by_market, market_type="all"
|
| 223 |
)
|
|
|
|
| 231 |
value=default_metric,
|
| 232 |
)
|
| 233 |
with gr.Row():
|
| 234 |
+
trade_details_plot = plot_trade_metrics(
|
| 235 |
+
metric_name=default_metric,
|
| 236 |
+
trades_df=trades_df,
|
| 237 |
)
|
| 238 |
|
| 239 |
def update_trade_details(trade_detail):
|
| 240 |
+
return plot_trade_metrics(metric_name=trade_detail, trades_df=trades_df)
|
|
|
|
|
|
|
| 241 |
|
| 242 |
trade_details_selector.change(
|
| 243 |
update_trade_details,
|
tabs/metrics.py
CHANGED
|
@@ -59,7 +59,53 @@ def plot_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.LinePlot
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|
| 62 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
"""Plots the trade details for the given trade detail."""
|
| 64 |
|
| 65 |
if metric_name == "mech calls":
|
|
@@ -81,29 +127,55 @@ def plot2_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.Plot:
|
|
| 81 |
column_name = metric_name
|
| 82 |
yaxis_title = "Gross profit per trade (xDAI)"
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
trades_filtered.groupby("month_year_week", sort=False)[column_name]
|
| 88 |
-
.quantile([0.25, 0.5, 0.75])
|
| 89 |
-
.unstack()
|
| 90 |
)
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 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 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
|
|
|
| 107 |
fig.update_layout(
|
| 108 |
xaxis_title="Week",
|
| 109 |
yaxis_title=yaxis_title,
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|
| 62 |
+
def get_metrics(
|
| 63 |
+
metric_name: str, column_name: str, market_creator: str, trades_df: pd.DataFrame
|
| 64 |
+
) -> pd.DataFrame:
|
| 65 |
+
# this is to filter out the data before 2023-09-01
|
| 66 |
+
trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
|
| 67 |
+
if market_creator != "all":
|
| 68 |
+
trades_filtered = trades_filtered.loc[
|
| 69 |
+
trades_filtered["market_creator"] == market_creator
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
trades_filtered = (
|
| 73 |
+
trades_filtered.groupby("month_year_week", sort=False)[column_name]
|
| 74 |
+
.quantile([0.25, 0.5, 0.75])
|
| 75 |
+
.unstack()
|
| 76 |
+
)
|
| 77 |
+
# reformat the data as percentile, date, value
|
| 78 |
+
trades_filtered = trades_filtered.melt(
|
| 79 |
+
id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
|
| 80 |
+
)
|
| 81 |
+
trades_filtered.columns = trades_filtered.columns.astype(str)
|
| 82 |
+
trades_filtered.reset_index(inplace=True)
|
| 83 |
+
trades_filtered.columns = [
|
| 84 |
+
"month_year_week",
|
| 85 |
+
"25th_percentile",
|
| 86 |
+
"50th_percentile",
|
| 87 |
+
"75th_percentile",
|
| 88 |
+
]
|
| 89 |
+
# reformat the data as percentile, date, value
|
| 90 |
+
trades_filtered = trades_filtered.melt(
|
| 91 |
+
id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
|
| 92 |
+
)
|
| 93 |
+
return trades_filtered
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def get_boxplot_metrics(
|
| 97 |
+
metric_name: str, column_name: str, trades_df: pd.DataFrame
|
| 98 |
+
) -> pd.DataFrame:
|
| 99 |
+
# this is to filter out the data before 2023-09-01
|
| 100 |
+
trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
|
| 101 |
+
trades_filtered = trades_filtered[
|
| 102 |
+
["month_year_week", "market_creator", column_name]
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def plot2_trade_details(
|
| 107 |
+
metric_name: str, market_creator: str, trades_df: pd.DataFrame
|
| 108 |
+
) -> gr.Plot:
|
| 109 |
"""Plots the trade details for the given trade detail."""
|
| 110 |
|
| 111 |
if metric_name == "mech calls":
|
|
|
|
| 127 |
column_name = metric_name
|
| 128 |
yaxis_title = "Gross profit per trade (xDAI)"
|
| 129 |
|
| 130 |
+
trades_filtered = get_metrics(metric_name, column_name, market_creator, trades_df)
|
| 131 |
+
fig = px.line(
|
| 132 |
+
trades_filtered, x="month_year_week", y=metric_name, color="percentile"
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
+
fig.update_layout(
|
| 135 |
+
xaxis_title="Week",
|
| 136 |
+
yaxis_title=yaxis_title,
|
| 137 |
+
legend=dict(yanchor="top", y=0.5),
|
| 138 |
+
)
|
| 139 |
+
fig.update_xaxes(tickformat="%b %d\n%Y")
|
| 140 |
+
return gr.Plot(
|
| 141 |
+
value=fig,
|
|
|
|
|
|
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
+
|
| 145 |
+
def plot_trade_metrics(metric_name: str, trades_df: pd.DataFrame) -> gr.Plot:
|
| 146 |
+
"""Plots the trade metrics."""
|
| 147 |
+
|
| 148 |
+
if metric_name == "mech calls":
|
| 149 |
+
metric_name = "mech_calls"
|
| 150 |
+
column_name = "num_mech_calls"
|
| 151 |
+
yaxis_title = "Nr of mech calls per trade"
|
| 152 |
+
elif metric_name == "ROI":
|
| 153 |
+
column_name = "roi"
|
| 154 |
+
yaxis_title = "ROI (net profit/cost)"
|
| 155 |
+
elif metric_name == "collateral amount":
|
| 156 |
+
metric_name = "collateral_amount"
|
| 157 |
+
column_name = metric_name
|
| 158 |
+
yaxis_title = "Collateral amount per trade (xDAI)"
|
| 159 |
+
elif metric_name == "net earnings":
|
| 160 |
+
metric_name = "net_earnings"
|
| 161 |
+
column_name = metric_name
|
| 162 |
+
yaxis_title = "Net profit per trade (xDAI)"
|
| 163 |
+
else: # earnings
|
| 164 |
+
column_name = metric_name
|
| 165 |
+
yaxis_title = "Gross profit per trade (xDAI)"
|
| 166 |
+
|
| 167 |
+
trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
|
| 168 |
+
trades_filtered = trades_filtered[
|
| 169 |
+
["month_year_week", "market_creator", column_name]
|
| 170 |
+
]
|
| 171 |
+
fig = px.box(
|
| 172 |
+
trades_filtered,
|
| 173 |
+
x="month_year_week",
|
| 174 |
+
y=column_name,
|
| 175 |
+
color="market_creator",
|
| 176 |
+
color_discrete_sequence=["goldenrod", "purple"],
|
| 177 |
)
|
| 178 |
+
fig.update_traces(boxmean=True)
|
| 179 |
fig.update_layout(
|
| 180 |
xaxis_title="Week",
|
| 181 |
yaxis_title=yaxis_title,
|