Spaces:
Runtime error
Runtime error
cyberosa
commited on
Commit
Β·
1ab360a
1
Parent(s):
9804ae2
adding new dataset with weekly metrics for traders and updated ROI
Browse files- app.py +74 -361
- scripts/utils.py +216 -0
- tabs/trader_plots.py +6 -21
app.py
CHANGED
|
@@ -1,10 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import gzip
|
| 4 |
-
import shutil
|
| 5 |
-
import os
|
| 6 |
import logging
|
| 7 |
-
from
|
| 8 |
|
| 9 |
|
| 10 |
from scripts.metrics import (
|
|
@@ -18,7 +15,7 @@ from scripts.retention_metrics import (
|
|
| 18 |
calculate_cohort_retention,
|
| 19 |
)
|
| 20 |
from tabs.trader_plots import (
|
| 21 |
-
|
| 22 |
default_trader_metric,
|
| 23 |
trader_metric_choices,
|
| 24 |
get_metrics_text,
|
|
@@ -66,209 +63,6 @@ def get_logger():
|
|
| 66 |
|
| 67 |
logger = get_logger()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
def load_all_data():
|
| 71 |
-
|
| 72 |
-
# all trades profitability
|
| 73 |
-
# Download the compressed file
|
| 74 |
-
gz_filepath_trades = hf_hub_download(
|
| 75 |
-
repo_id="valory/Olas-predict-dataset",
|
| 76 |
-
filename="all_trades_profitability.parquet.gz",
|
| 77 |
-
repo_type="dataset",
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
parquet_filepath_trades = gz_filepath_trades.replace(".gz", "")
|
| 81 |
-
parquet_filepath_trades = parquet_filepath_trades.replace("all", "")
|
| 82 |
-
|
| 83 |
-
with gzip.open(gz_filepath_trades, "rb") as f_in:
|
| 84 |
-
with open(parquet_filepath_trades, "wb") as f_out:
|
| 85 |
-
shutil.copyfileobj(f_in, f_out)
|
| 86 |
-
|
| 87 |
-
# Now read the decompressed parquet file
|
| 88 |
-
df1 = pd.read_parquet(parquet_filepath_trades)
|
| 89 |
-
|
| 90 |
-
# closed_markets_div
|
| 91 |
-
closed_markets_df = hf_hub_download(
|
| 92 |
-
repo_id="valory/Olas-predict-dataset",
|
| 93 |
-
filename="closed_markets_div.parquet",
|
| 94 |
-
repo_type="dataset",
|
| 95 |
-
)
|
| 96 |
-
df2 = pd.read_parquet(closed_markets_df)
|
| 97 |
-
|
| 98 |
-
# daily_info
|
| 99 |
-
daily_info_df = hf_hub_download(
|
| 100 |
-
repo_id="valory/Olas-predict-dataset",
|
| 101 |
-
filename="daily_info.parquet",
|
| 102 |
-
repo_type="dataset",
|
| 103 |
-
)
|
| 104 |
-
df3 = pd.read_parquet(daily_info_df)
|
| 105 |
-
|
| 106 |
-
# unknown traders
|
| 107 |
-
unknown_df = hf_hub_download(
|
| 108 |
-
repo_id="valory/Olas-predict-dataset",
|
| 109 |
-
filename="unknown_traders.parquet",
|
| 110 |
-
repo_type="dataset",
|
| 111 |
-
)
|
| 112 |
-
df4 = pd.read_parquet(unknown_df)
|
| 113 |
-
|
| 114 |
-
# retention activity
|
| 115 |
-
gz_file_path_ret = hf_hub_download(
|
| 116 |
-
repo_id="valory/Olas-predict-dataset",
|
| 117 |
-
filename="retention_activity.parquet.gz",
|
| 118 |
-
repo_type="dataset",
|
| 119 |
-
)
|
| 120 |
-
parquet_file_path_ret = gz_file_path_ret.replace(".gz", "")
|
| 121 |
-
|
| 122 |
-
with gzip.open(gz_file_path_ret, "rb") as f_in:
|
| 123 |
-
with open(parquet_file_path_ret, "wb") as f_out:
|
| 124 |
-
shutil.copyfileobj(f_in, f_out)
|
| 125 |
-
df5 = pd.read_parquet(parquet_file_path_ret)
|
| 126 |
-
# os.remove(parquet_file_path_ret)
|
| 127 |
-
|
| 128 |
-
# active_traders.parquet
|
| 129 |
-
active_traders_df = hf_hub_download(
|
| 130 |
-
repo_id="valory/Olas-predict-dataset",
|
| 131 |
-
filename="active_traders.parquet",
|
| 132 |
-
repo_type="dataset",
|
| 133 |
-
)
|
| 134 |
-
df6 = pd.read_parquet(active_traders_df)
|
| 135 |
-
|
| 136 |
-
# weekly_mech_calls.parquet
|
| 137 |
-
all_mech_calls_df = hf_hub_download(
|
| 138 |
-
repo_id="valory/Olas-predict-dataset",
|
| 139 |
-
filename="weekly_mech_calls.parquet",
|
| 140 |
-
repo_type="dataset",
|
| 141 |
-
)
|
| 142 |
-
df7 = pd.read_parquet(all_mech_calls_df)
|
| 143 |
-
|
| 144 |
-
# daa for quickstart and pearl
|
| 145 |
-
daa_qs_df = hf_hub_download(
|
| 146 |
-
repo_id="valory/Olas-predict-dataset",
|
| 147 |
-
filename="latest_result_DAA_QS.parquet",
|
| 148 |
-
repo_type="dataset",
|
| 149 |
-
)
|
| 150 |
-
df8 = pd.read_parquet(daa_qs_df)
|
| 151 |
-
|
| 152 |
-
daa_pearl_df = hf_hub_download(
|
| 153 |
-
repo_id="valory/Olas-predict-dataset",
|
| 154 |
-
filename="latest_result_DAA_Pearl.parquet",
|
| 155 |
-
repo_type="dataset",
|
| 156 |
-
)
|
| 157 |
-
df9 = pd.read_parquet(daa_pearl_df)
|
| 158 |
-
# Read weekly_avg_roi_pearl_agents.parquet
|
| 159 |
-
weekly_avg_roi_pearl_agents = hf_hub_download(
|
| 160 |
-
repo_id="valory/Olas-predict-dataset",
|
| 161 |
-
filename="weekly_avg_roi_pearl_agents.parquet",
|
| 162 |
-
repo_type="dataset",
|
| 163 |
-
)
|
| 164 |
-
df10 = pd.read_parquet(weekly_avg_roi_pearl_agents)
|
| 165 |
-
|
| 166 |
-
# two_weeks_avg_roi_pearl_agents.parquet
|
| 167 |
-
two_weeks_avg_roi_pearl_agents = hf_hub_download(
|
| 168 |
-
repo_id="valory/Olas-predict-dataset",
|
| 169 |
-
filename="two_weeks_avg_roi_pearl_agents.parquet",
|
| 170 |
-
repo_type="dataset",
|
| 171 |
-
)
|
| 172 |
-
df11 = pd.read_parquet(two_weeks_avg_roi_pearl_agents)
|
| 173 |
-
return df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def prepare_data():
|
| 177 |
-
|
| 178 |
-
(
|
| 179 |
-
all_trades,
|
| 180 |
-
closed_markets,
|
| 181 |
-
daily_info,
|
| 182 |
-
unknown_traders,
|
| 183 |
-
retention_df,
|
| 184 |
-
active_traders,
|
| 185 |
-
all_mech_calls,
|
| 186 |
-
daa_qs_df,
|
| 187 |
-
daa_pearl_df,
|
| 188 |
-
weekly_avg_roi_pearl_agents,
|
| 189 |
-
two_weeks_avg_roi_pearl_agents,
|
| 190 |
-
) = load_all_data()
|
| 191 |
-
all_trades["creation_timestamp"] = all_trades["creation_timestamp"].dt.tz_convert(
|
| 192 |
-
"UTC"
|
| 193 |
-
)
|
| 194 |
-
all_trades = all_trades.sort_values(by="creation_timestamp", ascending=True)
|
| 195 |
-
all_trades["creation_date"] = all_trades["creation_timestamp"].dt.date
|
| 196 |
-
|
| 197 |
-
# nr-trades variable
|
| 198 |
-
volume_trades_per_trader_and_market = (
|
| 199 |
-
all_trades.groupby(["trader_address", "title"])["roi"]
|
| 200 |
-
.count()
|
| 201 |
-
.reset_index(name="nr_trades_per_market")
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
traders_data = pd.merge(
|
| 205 |
-
all_trades, volume_trades_per_trader_and_market, on=["trader_address", "title"]
|
| 206 |
-
)
|
| 207 |
-
daily_info["creation_date"] = daily_info["creation_timestamp"].dt.date
|
| 208 |
-
unknown_traders["creation_date"] = unknown_traders["creation_timestamp"].dt.date
|
| 209 |
-
active_traders["creation_date"] = active_traders["creation_timestamp"].dt.date
|
| 210 |
-
# adding the trader family column
|
| 211 |
-
traders_data["trader_family"] = traders_data.apply(
|
| 212 |
-
lambda x: get_traders_family(x), axis=1
|
| 213 |
-
)
|
| 214 |
-
# print(traders_data.head())
|
| 215 |
-
|
| 216 |
-
traders_data = traders_data.sort_values(by="creation_timestamp", ascending=True)
|
| 217 |
-
unknown_traders = unknown_traders.sort_values(
|
| 218 |
-
by="creation_timestamp", ascending=True
|
| 219 |
-
)
|
| 220 |
-
traders_data["month_year_week"] = (
|
| 221 |
-
traders_data["creation_timestamp"]
|
| 222 |
-
.dt.to_period("W")
|
| 223 |
-
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 224 |
-
)
|
| 225 |
-
unknown_traders["month_year_week"] = (
|
| 226 |
-
unknown_traders["creation_timestamp"]
|
| 227 |
-
.dt.to_period("W")
|
| 228 |
-
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 229 |
-
)
|
| 230 |
-
closed_markets["month_year_week"] = (
|
| 231 |
-
closed_markets["opening_datetime"]
|
| 232 |
-
.dt.to_period("W")
|
| 233 |
-
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 234 |
-
)
|
| 235 |
-
|
| 236 |
-
# prepare the daa dataframes
|
| 237 |
-
daa_pearl_df["day"] = pd.to_datetime(
|
| 238 |
-
daa_pearl_df["day"], format="%Y-%m-%d 00:00:00.000 UTC"
|
| 239 |
-
)
|
| 240 |
-
daa_qs_df["day"] = pd.to_datetime(
|
| 241 |
-
daa_qs_df["day"], format="%Y-%m-%d 00:00:00.000 UTC"
|
| 242 |
-
)
|
| 243 |
-
daa_pearl_df["day"] = daa_pearl_df["day"].dt.tz_localize("UTC")
|
| 244 |
-
daa_qs_df["day"] = daa_qs_df["day"].dt.tz_localize("UTC")
|
| 245 |
-
daa_qs_df["tx_date"] = pd.to_datetime(daa_qs_df["day"]).dt.date
|
| 246 |
-
daa_pearl_df["tx_date"] = pd.to_datetime(daa_pearl_df["day"]).dt.date
|
| 247 |
-
daa_pearl_df["seven_day_trailing_avg"] = pd.to_numeric(
|
| 248 |
-
daa_pearl_df["seven_day_trailing_avg"], errors="coerce"
|
| 249 |
-
)
|
| 250 |
-
daa_pearl_df["seven_day_trailing_avg"] = daa_pearl_df[
|
| 251 |
-
"seven_day_trailing_avg"
|
| 252 |
-
].round(2)
|
| 253 |
-
daa_qs_df["seven_day_trailing_avg"] = pd.to_numeric(
|
| 254 |
-
daa_qs_df["seven_day_trailing_avg"], errors="coerce"
|
| 255 |
-
)
|
| 256 |
-
daa_qs_df["seven_day_trailing_avg"] = daa_qs_df["seven_day_trailing_avg"].round(2)
|
| 257 |
-
return (
|
| 258 |
-
traders_data,
|
| 259 |
-
closed_markets,
|
| 260 |
-
daily_info,
|
| 261 |
-
unknown_traders,
|
| 262 |
-
retention_df,
|
| 263 |
-
active_traders,
|
| 264 |
-
all_mech_calls,
|
| 265 |
-
daa_qs_df,
|
| 266 |
-
daa_pearl_df,
|
| 267 |
-
weekly_avg_roi_pearl_agents,
|
| 268 |
-
two_weeks_avg_roi_pearl_agents,
|
| 269 |
-
)
|
| 270 |
-
|
| 271 |
-
|
| 272 |
(
|
| 273 |
traders_data,
|
| 274 |
closed_markets,
|
|
@@ -281,6 +75,7 @@ def prepare_data():
|
|
| 281 |
daa_pearl_df,
|
| 282 |
weekly_avg_roi_pearl_agents,
|
| 283 |
two_weeks_avg_roi_pearl_agents,
|
|
|
|
| 284 |
) = prepare_data()
|
| 285 |
|
| 286 |
retention_df = prepare_retention_dataset(
|
|
@@ -290,20 +85,8 @@ print("max date of retention df")
|
|
| 290 |
print(max(retention_df.creation_timestamp))
|
| 291 |
|
| 292 |
demo = gr.Blocks()
|
| 293 |
-
#
|
| 294 |
-
|
| 295 |
-
traders_data=traders_data, all_mech_calls=all_mech_calls
|
| 296 |
-
)
|
| 297 |
-
weekly_o_metrics_by_market_creator = compute_weekly_metrics_by_market_creator(
|
| 298 |
-
traders_data=traders_data, all_mech_calls=all_mech_calls, trader_filter="Olas"
|
| 299 |
-
)
|
| 300 |
-
weekly_non_olas_metrics_by_market_creator = pd.DataFrame()
|
| 301 |
-
if len(traders_data.loc[traders_data["staking"] == "non_Olas"]) > 0:
|
| 302 |
-
weekly_non_olas_metrics_by_market_creator = (
|
| 303 |
-
compute_weekly_metrics_by_market_creator(
|
| 304 |
-
traders_data, all_mech_calls, trader_filter="non_Olas"
|
| 305 |
-
)
|
| 306 |
-
)
|
| 307 |
weekly_unknown_trader_metrics_by_market_creator = None
|
| 308 |
if len(unknown_traders) > 0:
|
| 309 |
weekly_unknown_trader_metrics_by_market_creator = (
|
|
@@ -335,9 +118,9 @@ with demo:
|
|
| 335 |
gr.Markdown("This app shows the weekly performance of the traders in Olas Predict.")
|
| 336 |
|
| 337 |
with gr.Tabs():
|
| 338 |
-
with gr.TabItem("π₯ Weekly metrics"):
|
| 339 |
with gr.Row():
|
| 340 |
-
gr.Markdown("# Weekly metrics
|
| 341 |
with gr.Row():
|
| 342 |
trader_details_selector = gr.Dropdown(
|
| 343 |
label="Select a weekly trader metric",
|
|
@@ -347,17 +130,17 @@ with demo:
|
|
| 347 |
|
| 348 |
with gr.Row():
|
| 349 |
with gr.Column(scale=3):
|
| 350 |
-
trader_markets_plot =
|
| 351 |
metric_name=default_trader_metric,
|
| 352 |
-
traders_df=
|
| 353 |
)
|
| 354 |
with gr.Column(scale=1):
|
| 355 |
trade_details_text = get_metrics_text(trader_type=None)
|
| 356 |
|
| 357 |
def update_trader_details(trader_detail):
|
| 358 |
-
return
|
| 359 |
metric_name=trader_detail,
|
| 360 |
-
traders_df=
|
| 361 |
)
|
| 362 |
|
| 363 |
trader_details_selector.change(
|
|
@@ -366,99 +149,70 @@ with demo:
|
|
| 366 |
outputs=trader_markets_plot,
|
| 367 |
)
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
with gr.Row():
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
with gr.Column(scale=3):
|
| 380 |
-
o_trader_markets_plot = plot_trader_metrics_by_market_creator(
|
| 381 |
-
metric_name=default_trader_metric,
|
| 382 |
-
traders_df=weekly_o_metrics_by_market_creator,
|
| 383 |
-
)
|
| 384 |
-
with gr.Column(scale=1):
|
| 385 |
-
trade_details_text = get_metrics_text(trader_type="Olas")
|
| 386 |
-
|
| 387 |
-
def update_a_trader_details(trader_detail):
|
| 388 |
-
return plot_trader_metrics_by_market_creator(
|
| 389 |
-
metric_name=trader_detail,
|
| 390 |
-
traders_df=weekly_o_metrics_by_market_creator,
|
| 391 |
-
)
|
| 392 |
-
|
| 393 |
-
trader_o_details_selector.change(
|
| 394 |
-
update_a_trader_details,
|
| 395 |
-
inputs=trader_o_details_selector,
|
| 396 |
-
outputs=o_trader_markets_plot,
|
| 397 |
-
)
|
| 398 |
-
if len(weekly_non_olas_metrics_by_market_creator) > 0:
|
| 399 |
-
# Non-Olas traders graph
|
| 400 |
-
with gr.Row():
|
| 401 |
-
gr.Markdown("# Weekly metrics of Non-Olas traders")
|
| 402 |
-
with gr.Row():
|
| 403 |
-
trader_no_details_selector = gr.Dropdown(
|
| 404 |
-
label="Select a weekly trader metric",
|
| 405 |
-
choices=trader_metric_choices,
|
| 406 |
-
value=default_trader_metric,
|
| 407 |
-
)
|
| 408 |
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
# Unknown traders graph
|
| 430 |
-
if weekly_unknown_trader_metrics_by_market_creator is not None:
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
with gr.Row():
|
| 441 |
-
with gr.Column(scale=3):
|
| 442 |
-
trader_u_markets_plot = plot_trader_metrics_by_market_creator(
|
| 443 |
-
metric_name=default_trader_metric,
|
| 444 |
-
traders_df=weekly_unknown_trader_metrics_by_market_creator,
|
| 445 |
-
)
|
| 446 |
-
with gr.Column(scale=1):
|
| 447 |
-
trade_details_text = get_metrics_text(
|
| 448 |
-
trader_type="unclassified"
|
| 449 |
-
)
|
| 450 |
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
|
| 463 |
with gr.TabItem("π
Daily metrics"):
|
| 464 |
live_trades_current_week = get_current_week_data(trades_df=daily_info)
|
|
@@ -664,16 +418,7 @@ with demo:
|
|
| 664 |
retention_matrix=cohort_retention_olas_qs,
|
| 665 |
cmap="Purples",
|
| 666 |
)
|
| 667 |
-
|
| 668 |
-
# cohort_retention_non_olas_pearl = calculate_cohort_retention(
|
| 669 |
-
# df=retention_df, market_creator="pearl", trader_type="non_Olas"
|
| 670 |
-
# )
|
| 671 |
-
# if len(cohort_retention_non_olas_pearl) > 0:
|
| 672 |
-
# gr.Markdown("## Cohort retention of Non-Olas traders")
|
| 673 |
-
# cohort_retention_plot2 = plot_cohort_retention_heatmap(
|
| 674 |
-
# retention_matrix=cohort_retention_non_olas_pearl,
|
| 675 |
-
# cmap=sns.color_palette("light:goldenrod", as_cmap=True),
|
| 676 |
-
# )
|
| 677 |
with gr.Row():
|
| 678 |
with gr.Column(scale=1):
|
| 679 |
gr.Markdown("## Cohort retention of pearl traders")
|
|
@@ -701,18 +446,6 @@ with demo:
|
|
| 701 |
retention_matrix=cohort_retention_unclassified_qs,
|
| 702 |
cmap="Greens",
|
| 703 |
)
|
| 704 |
-
# # non_Olas
|
| 705 |
-
# cohort_retention_non_olas_qs = calculate_cohort_retention(
|
| 706 |
-
# df=retention_df,
|
| 707 |
-
# market_creator="quickstart",
|
| 708 |
-
# trader_type="non_Olas",
|
| 709 |
-
# )
|
| 710 |
-
# if len(cohort_retention_non_olas_qs) > 0:
|
| 711 |
-
# gr.Markdown("## Cohort retention of Non-Olas traders")
|
| 712 |
-
# cohort_retention_plot5 = plot_cohort_retention_heatmap(
|
| 713 |
-
# retention_matrix=cohort_retention_non_olas_qs,
|
| 714 |
-
# cmap=sns.color_palette("light:goldenrod", as_cmap=True),
|
| 715 |
-
# )
|
| 716 |
with gr.TabItem("βοΈ Active traders"):
|
| 717 |
with gr.Row():
|
| 718 |
gr.Markdown("# Active Pearl traders by trader categories")
|
|
@@ -750,10 +483,10 @@ with demo:
|
|
| 750 |
with gr.Column(scale=1):
|
| 751 |
interpretation = get_interpretation_text()
|
| 752 |
|
| 753 |
-
with gr.TabItem("π° Money invested per
|
| 754 |
with gr.Row():
|
| 755 |
gr.Markdown("# Weekly total bet amount per trader type for all markets")
|
| 756 |
-
gr.Markdown("## Computed only for
|
| 757 |
with gr.Row():
|
| 758 |
total_bet_amount = plot_total_bet_amount(
|
| 759 |
traders_data, market_filter="all"
|
|
@@ -777,10 +510,10 @@ with demo:
|
|
| 777 |
traders_data, market_filter="quickstart"
|
| 778 |
)
|
| 779 |
|
| 780 |
-
with gr.TabItem("π° Money invested per market"):
|
| 781 |
with gr.Row():
|
| 782 |
gr.Markdown("# Weekly bet amounts per market for all traders")
|
| 783 |
-
gr.Markdown("## Computed only for
|
| 784 |
with gr.Row():
|
| 785 |
bet_amounts = plot_total_bet_amount_per_trader_per_market(traders_data)
|
| 786 |
|
|
@@ -791,13 +524,6 @@ with demo:
|
|
| 791 |
traders_data, trader_filter="Olas"
|
| 792 |
)
|
| 793 |
|
| 794 |
-
# with gr.Row():
|
| 795 |
-
# gr.Markdown("# Weekly bet amounts per market for Non-Olas traders")
|
| 796 |
-
# with gr.Row():
|
| 797 |
-
# no_trader_bet_amounts = plot_total_bet_amount_per_trader_per_market(
|
| 798 |
-
# traders_data, trader_filter="non_Olas"
|
| 799 |
-
# )
|
| 800 |
-
|
| 801 |
with gr.TabItem("ποΈWeekly winning trades % per trader"):
|
| 802 |
with gr.Row():
|
| 803 |
gr.Markdown("# Weekly winning trades percentage from all traders")
|
|
@@ -815,17 +541,4 @@ with demo:
|
|
| 815 |
weekly_winning_metrics_olas
|
| 816 |
)
|
| 817 |
|
| 818 |
-
# # non_Olas traders
|
| 819 |
-
# if len(weekly_non_olas_winning_metrics) > 0:
|
| 820 |
-
# with gr.Row():
|
| 821 |
-
# gr.Markdown(
|
| 822 |
-
# "# Weekly winning trades percentage from Non-Olas traders"
|
| 823 |
-
# )
|
| 824 |
-
# with gr.Row():
|
| 825 |
-
# metrics_text = get_metrics_text()
|
| 826 |
-
# with gr.Row():
|
| 827 |
-
# winning_metric = plot_winning_metric_per_trader(
|
| 828 |
-
# weekly_non_olas_winning_metrics
|
| 829 |
-
# )
|
| 830 |
-
|
| 831 |
demo.queue(default_concurrency_limit=40).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 3 |
import logging
|
| 4 |
+
from scripts.utils import prepare_data
|
| 5 |
|
| 6 |
|
| 7 |
from scripts.metrics import (
|
|
|
|
| 15 |
calculate_cohort_retention,
|
| 16 |
)
|
| 17 |
from tabs.trader_plots import (
|
| 18 |
+
plot_trader_metrics_by_agent_categories,
|
| 19 |
default_trader_metric,
|
| 20 |
trader_metric_choices,
|
| 21 |
get_metrics_text,
|
|
|
|
| 63 |
|
| 64 |
logger = get_logger()
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
(
|
| 67 |
traders_data,
|
| 68 |
closed_markets,
|
|
|
|
| 75 |
daa_pearl_df,
|
| 76 |
weekly_avg_roi_pearl_agents,
|
| 77 |
two_weeks_avg_roi_pearl_agents,
|
| 78 |
+
traders_weekly_metrics_df,
|
| 79 |
) = prepare_data()
|
| 80 |
|
| 81 |
retention_df = prepare_retention_dataset(
|
|
|
|
| 85 |
print(max(retention_df.creation_timestamp))
|
| 86 |
|
| 87 |
demo = gr.Blocks()
|
| 88 |
+
# TODO classify traders in the weekly metrics dataframe by agent type (Pearl, QS, and all)
|
| 89 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
weekly_unknown_trader_metrics_by_market_creator = None
|
| 91 |
if len(unknown_traders) > 0:
|
| 92 |
weekly_unknown_trader_metrics_by_market_creator = (
|
|
|
|
| 118 |
gr.Markdown("This app shows the weekly performance of the traders in Olas Predict.")
|
| 119 |
|
| 120 |
with gr.Tabs():
|
| 121 |
+
with gr.TabItem("π₯ Weekly metrics (WIP)"):
|
| 122 |
with gr.Row():
|
| 123 |
+
gr.Markdown("# Weekly metrics for all traders")
|
| 124 |
with gr.Row():
|
| 125 |
trader_details_selector = gr.Dropdown(
|
| 126 |
label="Select a weekly trader metric",
|
|
|
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
with gr.Column(scale=3):
|
| 133 |
+
trader_markets_plot = plot_trader_metrics_by_agent_categories(
|
| 134 |
metric_name=default_trader_metric,
|
| 135 |
+
traders_df=traders_weekly_metrics_df,
|
| 136 |
)
|
| 137 |
with gr.Column(scale=1):
|
| 138 |
trade_details_text = get_metrics_text(trader_type=None)
|
| 139 |
|
| 140 |
def update_trader_details(trader_detail):
|
| 141 |
+
return plot_trader_metrics_by_agent_categories(
|
| 142 |
metric_name=trader_detail,
|
| 143 |
+
traders_df=traders_weekly_metrics_df,
|
| 144 |
)
|
| 145 |
|
| 146 |
trader_details_selector.change(
|
|
|
|
| 149 |
outputs=trader_markets_plot,
|
| 150 |
)
|
| 151 |
|
| 152 |
+
# if len(weekly_non_olas_metrics_by_market_creator) > 0:
|
| 153 |
+
# # Non-Olas traders graph
|
| 154 |
+
# with gr.Row():
|
| 155 |
+
# gr.Markdown("# Weekly metrics of Non-Olas traders")
|
| 156 |
+
# with gr.Row():
|
| 157 |
+
# trader_no_details_selector = gr.Dropdown(
|
| 158 |
+
# label="Select a weekly trader metric",
|
| 159 |
+
# choices=trader_metric_choices,
|
| 160 |
+
# value=default_trader_metric,
|
| 161 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
# with gr.Row():
|
| 164 |
+
# with gr.Column(scale=3):
|
| 165 |
+
# trader_no_markets_plot = plot_trader_metrics_by_market_creator(
|
| 166 |
+
# metric_name=default_trader_metric,
|
| 167 |
+
# traders_df=weekly_non_olas_metrics_by_market_creator,
|
| 168 |
+
# )
|
| 169 |
+
# with gr.Column(scale=1):
|
| 170 |
+
# trade_details_text = get_metrics_text(trader_type="non_Olas")
|
| 171 |
|
| 172 |
+
# def update_no_trader_details(trader_detail):
|
| 173 |
+
# return plot_trader_metrics_by_market_creator(
|
| 174 |
+
# metric_name=trader_detail,
|
| 175 |
+
# traders_df=weekly_non_olas_metrics_by_market_creator,
|
| 176 |
+
# )
|
| 177 |
|
| 178 |
+
# trader_no_details_selector.change(
|
| 179 |
+
# update_no_trader_details,
|
| 180 |
+
# inputs=trader_no_details_selector,
|
| 181 |
+
# outputs=trader_no_markets_plot,
|
| 182 |
+
# )
|
| 183 |
# Unknown traders graph
|
| 184 |
+
# if weekly_unknown_trader_metrics_by_market_creator is not None:
|
| 185 |
+
# with gr.Row():
|
| 186 |
+
# gr.Markdown("# Weekly metrics of Unclassified traders")
|
| 187 |
+
# with gr.Row():
|
| 188 |
+
# trader_u_details_selector = gr.Dropdown(
|
| 189 |
+
# label="Select a weekly trader metric",
|
| 190 |
+
# choices=trader_metric_choices,
|
| 191 |
+
# value=default_trader_metric,
|
| 192 |
+
# )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
+
# with gr.Row():
|
| 195 |
+
# with gr.Column(scale=3):
|
| 196 |
+
# trader_u_markets_plot = plot_trader_metrics_by_agent_categories(
|
| 197 |
+
# metric_name=default_trader_metric,
|
| 198 |
+
# traders_df=weekly_unknown_trader_metrics_by_market_creator,
|
| 199 |
+
# )
|
| 200 |
+
# with gr.Column(scale=1):
|
| 201 |
+
# trade_details_text = get_metrics_text(
|
| 202 |
+
# trader_type="unclassified"
|
| 203 |
+
# )
|
| 204 |
+
|
| 205 |
+
# def update_u_trader_details(trader_detail):
|
| 206 |
+
# return plot_trader_metrics_by_agent_categories(
|
| 207 |
+
# metric_name=trader_detail,
|
| 208 |
+
# traders_df=weekly_unknown_trader_metrics_by_market_creator,
|
| 209 |
+
# )
|
| 210 |
|
| 211 |
+
# trader_u_details_selector.change(
|
| 212 |
+
# update_u_trader_details,
|
| 213 |
+
# inputs=trader_u_details_selector,
|
| 214 |
+
# outputs=trader_u_markets_plot,
|
| 215 |
+
# )
|
| 216 |
|
| 217 |
with gr.TabItem("π
Daily metrics"):
|
| 218 |
live_trades_current_week = get_current_week_data(trades_df=daily_info)
|
|
|
|
| 418 |
retention_matrix=cohort_retention_olas_qs,
|
| 419 |
cmap="Purples",
|
| 420 |
)
|
| 421 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 422 |
with gr.Row():
|
| 423 |
with gr.Column(scale=1):
|
| 424 |
gr.Markdown("## Cohort retention of pearl traders")
|
|
|
|
| 446 |
retention_matrix=cohort_retention_unclassified_qs,
|
| 447 |
cmap="Greens",
|
| 448 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 449 |
with gr.TabItem("βοΈ Active traders"):
|
| 450 |
with gr.Row():
|
| 451 |
gr.Markdown("# Active Pearl traders by trader categories")
|
|
|
|
| 483 |
with gr.Column(scale=1):
|
| 484 |
interpretation = get_interpretation_text()
|
| 485 |
|
| 486 |
+
with gr.TabItem("π° Money invested per market category"):
|
| 487 |
with gr.Row():
|
| 488 |
gr.Markdown("# Weekly total bet amount per trader type for all markets")
|
| 489 |
+
gr.Markdown("## Computed only for traders using the mech service")
|
| 490 |
with gr.Row():
|
| 491 |
total_bet_amount = plot_total_bet_amount(
|
| 492 |
traders_data, market_filter="all"
|
|
|
|
| 510 |
traders_data, market_filter="quickstart"
|
| 511 |
)
|
| 512 |
|
| 513 |
+
with gr.TabItem("π° Money invested per single market"):
|
| 514 |
with gr.Row():
|
| 515 |
gr.Markdown("# Weekly bet amounts per market for all traders")
|
| 516 |
+
gr.Markdown("## Computed only for traders using the mech service")
|
| 517 |
with gr.Row():
|
| 518 |
bet_amounts = plot_total_bet_amount_per_trader_per_market(traders_data)
|
| 519 |
|
|
|
|
| 524 |
traders_data, trader_filter="Olas"
|
| 525 |
)
|
| 526 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
with gr.TabItem("ποΈWeekly winning trades % per trader"):
|
| 528 |
with gr.Row():
|
| 529 |
gr.Markdown("# Weekly winning trades percentage from all traders")
|
|
|
|
| 541 |
weekly_winning_metrics_olas
|
| 542 |
)
|
| 543 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 544 |
demo.queue(default_concurrency_limit=40).launch()
|
scripts/utils.py
CHANGED
|
@@ -1,6 +1,10 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
from pathlib import Path
|
| 3 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
SCRIPTS_DIR = Path(__file__).parent
|
| 6 |
ROOT_DIR = SCRIPTS_DIR.parent
|
|
@@ -28,3 +32,215 @@ def get_next_week():
|
|
| 28 |
current_date = datetime.now()
|
| 29 |
next_week_start = current_date + timedelta(days=7 - (current_date.weekday() + 1))
|
| 30 |
return next_week_start.strftime("%b-%d-%Y")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
from pathlib import Path
|
| 3 |
from datetime import datetime, timedelta
|
| 4 |
+
import gzip
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
SCRIPTS_DIR = Path(__file__).parent
|
| 10 |
ROOT_DIR = SCRIPTS_DIR.parent
|
|
|
|
| 32 |
current_date = datetime.now()
|
| 33 |
next_week_start = current_date + timedelta(days=7 - (current_date.weekday() + 1))
|
| 34 |
return next_week_start.strftime("%b-%d-%Y")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def load_all_data():
|
| 38 |
+
|
| 39 |
+
# all trades profitability
|
| 40 |
+
# Download the compressed file
|
| 41 |
+
gz_filepath_trades = hf_hub_download(
|
| 42 |
+
repo_id="valory/Olas-predict-dataset",
|
| 43 |
+
filename="all_trades_profitability.parquet.gz",
|
| 44 |
+
repo_type="dataset",
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
parquet_filepath_trades = gz_filepath_trades.replace(".gz", "")
|
| 48 |
+
parquet_filepath_trades = parquet_filepath_trades.replace("all", "")
|
| 49 |
+
|
| 50 |
+
with gzip.open(gz_filepath_trades, "rb") as f_in:
|
| 51 |
+
with open(parquet_filepath_trades, "wb") as f_out:
|
| 52 |
+
shutil.copyfileobj(f_in, f_out)
|
| 53 |
+
|
| 54 |
+
# Now read the decompressed parquet file
|
| 55 |
+
df1 = pd.read_parquet(parquet_filepath_trades)
|
| 56 |
+
|
| 57 |
+
# closed_markets_div
|
| 58 |
+
closed_markets_df = hf_hub_download(
|
| 59 |
+
repo_id="valory/Olas-predict-dataset",
|
| 60 |
+
filename="closed_markets_div.parquet",
|
| 61 |
+
repo_type="dataset",
|
| 62 |
+
)
|
| 63 |
+
df2 = pd.read_parquet(closed_markets_df)
|
| 64 |
+
|
| 65 |
+
# daily_info
|
| 66 |
+
daily_info_df = hf_hub_download(
|
| 67 |
+
repo_id="valory/Olas-predict-dataset",
|
| 68 |
+
filename="daily_info.parquet",
|
| 69 |
+
repo_type="dataset",
|
| 70 |
+
)
|
| 71 |
+
df3 = pd.read_parquet(daily_info_df)
|
| 72 |
+
|
| 73 |
+
# unknown traders
|
| 74 |
+
unknown_df = hf_hub_download(
|
| 75 |
+
repo_id="valory/Olas-predict-dataset",
|
| 76 |
+
filename="unknown_traders.parquet",
|
| 77 |
+
repo_type="dataset",
|
| 78 |
+
)
|
| 79 |
+
df4 = pd.read_parquet(unknown_df)
|
| 80 |
+
|
| 81 |
+
# retention activity
|
| 82 |
+
gz_file_path_ret = hf_hub_download(
|
| 83 |
+
repo_id="valory/Olas-predict-dataset",
|
| 84 |
+
filename="retention_activity.parquet.gz",
|
| 85 |
+
repo_type="dataset",
|
| 86 |
+
)
|
| 87 |
+
parquet_file_path_ret = gz_file_path_ret.replace(".gz", "")
|
| 88 |
+
|
| 89 |
+
with gzip.open(gz_file_path_ret, "rb") as f_in:
|
| 90 |
+
with open(parquet_file_path_ret, "wb") as f_out:
|
| 91 |
+
shutil.copyfileobj(f_in, f_out)
|
| 92 |
+
df5 = pd.read_parquet(parquet_file_path_ret)
|
| 93 |
+
# os.remove(parquet_file_path_ret)
|
| 94 |
+
|
| 95 |
+
# active_traders.parquet
|
| 96 |
+
active_traders_df = hf_hub_download(
|
| 97 |
+
repo_id="valory/Olas-predict-dataset",
|
| 98 |
+
filename="active_traders.parquet",
|
| 99 |
+
repo_type="dataset",
|
| 100 |
+
)
|
| 101 |
+
df6 = pd.read_parquet(active_traders_df)
|
| 102 |
+
|
| 103 |
+
# weekly_mech_calls.parquet
|
| 104 |
+
all_mech_calls_df = hf_hub_download(
|
| 105 |
+
repo_id="valory/Olas-predict-dataset",
|
| 106 |
+
filename="weekly_mech_calls.parquet",
|
| 107 |
+
repo_type="dataset",
|
| 108 |
+
)
|
| 109 |
+
df7 = pd.read_parquet(all_mech_calls_df)
|
| 110 |
+
|
| 111 |
+
# daa for quickstart and pearl
|
| 112 |
+
daa_qs_df = hf_hub_download(
|
| 113 |
+
repo_id="valory/Olas-predict-dataset",
|
| 114 |
+
filename="latest_result_DAA_QS.parquet",
|
| 115 |
+
repo_type="dataset",
|
| 116 |
+
)
|
| 117 |
+
df8 = pd.read_parquet(daa_qs_df)
|
| 118 |
+
|
| 119 |
+
daa_pearl_df = hf_hub_download(
|
| 120 |
+
repo_id="valory/Olas-predict-dataset",
|
| 121 |
+
filename="latest_result_DAA_Pearl.parquet",
|
| 122 |
+
repo_type="dataset",
|
| 123 |
+
)
|
| 124 |
+
df9 = pd.read_parquet(daa_pearl_df)
|
| 125 |
+
# Read weekly_avg_roi_pearl_agents.parquet
|
| 126 |
+
weekly_avg_roi_pearl_agents = hf_hub_download(
|
| 127 |
+
repo_id="valory/Olas-predict-dataset",
|
| 128 |
+
filename="weekly_avg_roi_pearl_agents.parquet",
|
| 129 |
+
repo_type="dataset",
|
| 130 |
+
)
|
| 131 |
+
df10 = pd.read_parquet(weekly_avg_roi_pearl_agents)
|
| 132 |
+
|
| 133 |
+
# two_weeks_avg_roi_pearl_agents.parquet
|
| 134 |
+
two_weeks_avg_roi_pearl_agents = hf_hub_download(
|
| 135 |
+
repo_id="valory/Olas-predict-dataset",
|
| 136 |
+
filename="two_weeks_avg_roi_pearl_agents.parquet",
|
| 137 |
+
repo_type="dataset",
|
| 138 |
+
)
|
| 139 |
+
df11 = pd.read_parquet(two_weeks_avg_roi_pearl_agents)
|
| 140 |
+
|
| 141 |
+
# read traders_weekly_metrics.parquet file
|
| 142 |
+
traders_weekly_metrics_df = hf_hub_download(
|
| 143 |
+
repo_id="valory/Olas-predict-dataset",
|
| 144 |
+
filename="traders_weekly_metrics.parquet",
|
| 145 |
+
repo_type="dataset",
|
| 146 |
+
)
|
| 147 |
+
df12 = pd.read_parquet(traders_weekly_metrics_df)
|
| 148 |
+
return df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def prepare_data():
|
| 152 |
+
|
| 153 |
+
(
|
| 154 |
+
all_trades,
|
| 155 |
+
closed_markets,
|
| 156 |
+
daily_info,
|
| 157 |
+
unknown_traders,
|
| 158 |
+
retention_df,
|
| 159 |
+
active_traders,
|
| 160 |
+
all_mech_calls,
|
| 161 |
+
daa_qs_df,
|
| 162 |
+
daa_pearl_df,
|
| 163 |
+
weekly_avg_roi_pearl_agents,
|
| 164 |
+
two_weeks_avg_roi_pearl_agents,
|
| 165 |
+
traders_weekly_metrics_df,
|
| 166 |
+
) = load_all_data()
|
| 167 |
+
all_trades["creation_timestamp"] = all_trades["creation_timestamp"].dt.tz_convert(
|
| 168 |
+
"UTC"
|
| 169 |
+
)
|
| 170 |
+
all_trades = all_trades.sort_values(by="creation_timestamp", ascending=True)
|
| 171 |
+
all_trades["creation_date"] = all_trades["creation_timestamp"].dt.date
|
| 172 |
+
|
| 173 |
+
# nr-trades variable
|
| 174 |
+
volume_trades_per_trader_and_market = (
|
| 175 |
+
all_trades.groupby(["trader_address", "title"])["roi"]
|
| 176 |
+
.count()
|
| 177 |
+
.reset_index(name="nr_trades_per_market")
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
traders_data = pd.merge(
|
| 181 |
+
all_trades, volume_trades_per_trader_and_market, on=["trader_address", "title"]
|
| 182 |
+
)
|
| 183 |
+
daily_info["creation_date"] = daily_info["creation_timestamp"].dt.date
|
| 184 |
+
unknown_traders["creation_date"] = unknown_traders["creation_timestamp"].dt.date
|
| 185 |
+
active_traders["creation_date"] = active_traders["creation_timestamp"].dt.date
|
| 186 |
+
# adding the trader family column
|
| 187 |
+
traders_data["trader_family"] = traders_data.apply(
|
| 188 |
+
lambda x: get_traders_family(x), axis=1
|
| 189 |
+
)
|
| 190 |
+
# print(traders_data.head())
|
| 191 |
+
|
| 192 |
+
traders_data = traders_data.sort_values(by="creation_timestamp", ascending=True)
|
| 193 |
+
unknown_traders = unknown_traders.sort_values(
|
| 194 |
+
by="creation_timestamp", ascending=True
|
| 195 |
+
)
|
| 196 |
+
traders_data["month_year_week"] = (
|
| 197 |
+
traders_data["creation_timestamp"]
|
| 198 |
+
.dt.to_period("W")
|
| 199 |
+
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 200 |
+
)
|
| 201 |
+
unknown_traders["month_year_week"] = (
|
| 202 |
+
unknown_traders["creation_timestamp"]
|
| 203 |
+
.dt.to_period("W")
|
| 204 |
+
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 205 |
+
)
|
| 206 |
+
closed_markets["month_year_week"] = (
|
| 207 |
+
closed_markets["opening_datetime"]
|
| 208 |
+
.dt.to_period("W")
|
| 209 |
+
.dt.start_time.dt.strftime("%b-%d-%Y")
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
# prepare the daa dataframes
|
| 213 |
+
daa_pearl_df["day"] = pd.to_datetime(
|
| 214 |
+
daa_pearl_df["day"], format="%Y-%m-%d 00:00:00.000 UTC"
|
| 215 |
+
)
|
| 216 |
+
daa_qs_df["day"] = pd.to_datetime(
|
| 217 |
+
daa_qs_df["day"], format="%Y-%m-%d 00:00:00.000 UTC"
|
| 218 |
+
)
|
| 219 |
+
daa_pearl_df["day"] = daa_pearl_df["day"].dt.tz_localize("UTC")
|
| 220 |
+
daa_qs_df["day"] = daa_qs_df["day"].dt.tz_localize("UTC")
|
| 221 |
+
daa_qs_df["tx_date"] = pd.to_datetime(daa_qs_df["day"]).dt.date
|
| 222 |
+
daa_pearl_df["tx_date"] = pd.to_datetime(daa_pearl_df["day"]).dt.date
|
| 223 |
+
daa_pearl_df["seven_day_trailing_avg"] = pd.to_numeric(
|
| 224 |
+
daa_pearl_df["seven_day_trailing_avg"], errors="coerce"
|
| 225 |
+
)
|
| 226 |
+
daa_pearl_df["seven_day_trailing_avg"] = daa_pearl_df[
|
| 227 |
+
"seven_day_trailing_avg"
|
| 228 |
+
].round(2)
|
| 229 |
+
daa_qs_df["seven_day_trailing_avg"] = pd.to_numeric(
|
| 230 |
+
daa_qs_df["seven_day_trailing_avg"], errors="coerce"
|
| 231 |
+
)
|
| 232 |
+
daa_qs_df["seven_day_trailing_avg"] = daa_qs_df["seven_day_trailing_avg"].round(2)
|
| 233 |
+
return (
|
| 234 |
+
traders_data,
|
| 235 |
+
closed_markets,
|
| 236 |
+
daily_info,
|
| 237 |
+
unknown_traders,
|
| 238 |
+
retention_df,
|
| 239 |
+
active_traders,
|
| 240 |
+
all_mech_calls,
|
| 241 |
+
daa_qs_df,
|
| 242 |
+
daa_pearl_df,
|
| 243 |
+
weekly_avg_roi_pearl_agents,
|
| 244 |
+
two_weeks_avg_roi_pearl_agents,
|
| 245 |
+
traders_weekly_metrics_df,
|
| 246 |
+
)
|
tabs/trader_plots.py
CHANGED
|
@@ -84,25 +84,25 @@ def get_interpretation_text() -> gr.Markdown:
|
|
| 84 |
return gr.Markdown(interpretation_text)
|
| 85 |
|
| 86 |
|
| 87 |
-
def
|
| 88 |
metric_name: str, traders_df: pd.DataFrame
|
| 89 |
) -> gr.Plot:
|
| 90 |
"""Plots the weekly trader metrics."""
|
| 91 |
|
| 92 |
if metric_name == "mech calls":
|
| 93 |
metric_name = "mech_calls"
|
| 94 |
-
column_name = "
|
| 95 |
yaxis_title = "Total nr of mech calls per trader"
|
| 96 |
elif metric_name == "ROI":
|
| 97 |
column_name = "roi"
|
| 98 |
yaxis_title = "Total ROI (net profit/cost)"
|
| 99 |
elif metric_name == "bet amount":
|
| 100 |
metric_name = "bet_amount"
|
| 101 |
-
column_name =
|
| 102 |
yaxis_title = "Total bet amount per trader (xDAI)"
|
| 103 |
elif metric_name == "net earnings":
|
| 104 |
metric_name = "net_earnings"
|
| 105 |
-
column_name =
|
| 106 |
yaxis_title = "Total net profit per trader (xDAI)"
|
| 107 |
elif metric_name == "nr_trades":
|
| 108 |
column_name = metric_name
|
|
@@ -111,23 +111,10 @@ def plot_trader_metrics_by_market_creator(
|
|
| 111 |
column_name = metric_name
|
| 112 |
yaxis_title = "Total gross profit per trader (xDAI)"
|
| 113 |
|
| 114 |
-
traders_filtered = traders_df[["month_year_week", "market_creator", column_name]]
|
| 115 |
-
# Convert string dates to datetime and sort them
|
| 116 |
-
all_dates_dt = sorted(
|
| 117 |
-
[
|
| 118 |
-
datetime.strptime(date, "%b-%d-%Y")
|
| 119 |
-
for date in traders_filtered["month_year_week"].unique()
|
| 120 |
-
]
|
| 121 |
-
)
|
| 122 |
-
# Convert back to string format
|
| 123 |
-
all_dates = [date.strftime("%b-%d-%Y") for date in all_dates_dt]
|
| 124 |
fig = px.box(
|
| 125 |
-
|
| 126 |
-
x="
|
| 127 |
y=column_name,
|
| 128 |
-
color="market_creator",
|
| 129 |
-
color_discrete_sequence=["purple", "goldenrod", "darkgreen"],
|
| 130 |
-
category_orders={"market_creator": ["pearl", "quickstart", "all"]},
|
| 131 |
)
|
| 132 |
fig.update_traces(boxmean=True)
|
| 133 |
fig.update_layout(
|
|
@@ -136,8 +123,6 @@ def plot_trader_metrics_by_market_creator(
|
|
| 136 |
legend=dict(yanchor="top", y=0.5),
|
| 137 |
)
|
| 138 |
fig.update_xaxes(tickformat="%b %d\n%Y")
|
| 139 |
-
# Update layout to force x-axis category order (hotfix for a sorting issue)
|
| 140 |
-
fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": all_dates})
|
| 141 |
|
| 142 |
return gr.Plot(
|
| 143 |
value=fig,
|
|
|
|
| 84 |
return gr.Markdown(interpretation_text)
|
| 85 |
|
| 86 |
|
| 87 |
+
def plot_trader_metrics_by_agent_categories(
|
| 88 |
metric_name: str, traders_df: pd.DataFrame
|
| 89 |
) -> gr.Plot:
|
| 90 |
"""Plots the weekly trader metrics."""
|
| 91 |
|
| 92 |
if metric_name == "mech calls":
|
| 93 |
metric_name = "mech_calls"
|
| 94 |
+
column_name = "total_mech_calls"
|
| 95 |
yaxis_title = "Total nr of mech calls per trader"
|
| 96 |
elif metric_name == "ROI":
|
| 97 |
column_name = "roi"
|
| 98 |
yaxis_title = "Total ROI (net profit/cost)"
|
| 99 |
elif metric_name == "bet amount":
|
| 100 |
metric_name = "bet_amount"
|
| 101 |
+
column_name = "total_bet_amount"
|
| 102 |
yaxis_title = "Total bet amount per trader (xDAI)"
|
| 103 |
elif metric_name == "net earnings":
|
| 104 |
metric_name = "net_earnings"
|
| 105 |
+
column_name = "net_earnings"
|
| 106 |
yaxis_title = "Total net profit per trader (xDAI)"
|
| 107 |
elif metric_name == "nr_trades":
|
| 108 |
column_name = metric_name
|
|
|
|
| 111 |
column_name = metric_name
|
| 112 |
yaxis_title = "Total gross profit per trader (xDAI)"
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
fig = px.box(
|
| 115 |
+
traders_df,
|
| 116 |
+
x="week_start",
|
| 117 |
y=column_name,
|
|
|
|
|
|
|
|
|
|
| 118 |
)
|
| 119 |
fig.update_traces(boxmean=True)
|
| 120 |
fig.update_layout(
|
|
|
|
| 123 |
legend=dict(yanchor="top", y=0.5),
|
| 124 |
)
|
| 125 |
fig.update_xaxes(tickformat="%b %d\n%Y")
|
|
|
|
|
|
|
| 126 |
|
| 127 |
return gr.Plot(
|
| 128 |
value=fig,
|