Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import pandas as pd | |
| import logging | |
| from scripts.utils import prepare_data | |
| from scripts.metrics import ( | |
| compute_weekly_metrics_by_market_creator, | |
| compute_daily_metrics_by_market_creator, | |
| compute_winning_metrics_by_trader, | |
| ) | |
| from scripts.retention_metrics import ( | |
| prepare_retention_dataset, | |
| calculate_wow_retention_by_type, | |
| calculate_cohort_retention, | |
| ) | |
| from tabs.trader_plots import ( | |
| plot_trader_metrics_by_agent_categories, | |
| default_trader_metric, | |
| trader_metric_choices, | |
| get_metrics_text, | |
| plot_winning_metric_per_trader, | |
| get_interpretation_text, | |
| plot_total_bet_amount, | |
| plot_active_traders, | |
| ) | |
| from tabs.agent_graphs import ( | |
| plot_rolling_average_dune, | |
| plot_rolling_average_roi, | |
| plot_weekly_average_roi, | |
| ) | |
| from tabs.daily_graphs import ( | |
| get_current_week_data, | |
| plot_daily_metrics, | |
| trader_daily_metric_choices, | |
| default_daily_metric, | |
| ) | |
| from scripts.utils import get_traders_family | |
| from tabs.market_plots import ( | |
| plot_kl_div_per_market, | |
| plot_total_bet_amount_per_trader_per_market, | |
| ) | |
| from tabs.retention_plots import ( | |
| plot_wow_retention_by_type, | |
| plot_cohort_retention_heatmap, | |
| ) | |
| def get_logger(): | |
| logger = logging.getLogger(__name__) | |
| logger.setLevel(logging.DEBUG) | |
| # stream handler and formatter | |
| stream_handler = logging.StreamHandler() | |
| stream_handler.setLevel(logging.DEBUG) | |
| formatter = logging.Formatter( | |
| "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
| ) | |
| stream_handler.setFormatter(formatter) | |
| logger.addHandler(stream_handler) | |
| return logger | |
| logger = get_logger() | |
| ( | |
| traders_data, | |
| closed_markets, | |
| daily_info, | |
| unknown_traders, | |
| raw_retention_df, | |
| active_traders, | |
| all_mech_calls, | |
| daa_qs_df, | |
| daa_pearl_df, | |
| weekly_avg_roi_pearl_agents, | |
| two_weeks_avg_roi_pearl_agents, | |
| traders_weekly_metrics_df, | |
| ) = prepare_data() | |
| retention_df = prepare_retention_dataset( | |
| retention_df=raw_retention_df, unknown_df=unknown_traders | |
| ) | |
| print("max date of retention df") | |
| print(max(retention_df.creation_timestamp)) | |
| demo = gr.Blocks() | |
| # TODO classify traders in the weekly metrics dataframe by agent type (Pearl, QS, and all) | |
| weekly_unknown_trader_metrics_by_market_creator = None | |
| if len(unknown_traders) > 0: | |
| weekly_unknown_trader_metrics_by_market_creator = ( | |
| compute_weekly_metrics_by_market_creator( | |
| traders_data=unknown_traders, | |
| all_mech_calls=None, | |
| trader_filter=None, | |
| unknown_trader=True, | |
| ) | |
| ) | |
| # just for all traders | |
| weekly_winning_metrics = compute_winning_metrics_by_trader( | |
| traders_data=traders_data, unknown_info=unknown_traders | |
| ) | |
| weekly_winning_metrics_olas = compute_winning_metrics_by_trader( | |
| traders_data=traders_data, unknown_info=unknown_traders, trader_filter="Olas" | |
| ) | |
| weekly_non_olas_winning_metrics = pd.DataFrame() | |
| if len(traders_data.loc[traders_data["staking"] == "non_Olas"]) > 0: | |
| weekly_non_olas_winning_metrics = compute_winning_metrics_by_trader( | |
| traders_data=traders_data, | |
| unknown_info=unknown_traders, | |
| trader_filter="non_Olas", | |
| ) | |
| with demo: | |
| gr.HTML("<h1>Traders monitoring dashboard </h1>") | |
| gr.Markdown("This app shows the weekly performance of the traders in Olas Predict.") | |
| with gr.Tabs(): | |
| with gr.TabItem("π₯ Weekly metrics (WIP)"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly metrics for all traders") | |
| with gr.Row(): | |
| trader_details_selector = gr.Dropdown( | |
| label="Select a weekly trader metric", | |
| choices=trader_metric_choices, | |
| value=default_trader_metric, | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| trader_markets_plot = plot_trader_metrics_by_agent_categories( | |
| metric_name=default_trader_metric, | |
| traders_df=traders_weekly_metrics_df, | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text(trader_type=None) | |
| def update_trader_details(trader_detail): | |
| return plot_trader_metrics_by_agent_categories( | |
| metric_name=trader_detail, | |
| traders_df=traders_weekly_metrics_df, | |
| ) | |
| trader_details_selector.change( | |
| update_trader_details, | |
| inputs=trader_details_selector, | |
| outputs=trader_markets_plot, | |
| ) | |
| # if len(weekly_non_olas_metrics_by_market_creator) > 0: | |
| # # Non-Olas traders graph | |
| # with gr.Row(): | |
| # gr.Markdown("# Weekly metrics of Non-Olas traders") | |
| # with gr.Row(): | |
| # trader_no_details_selector = gr.Dropdown( | |
| # label="Select a weekly trader metric", | |
| # choices=trader_metric_choices, | |
| # value=default_trader_metric, | |
| # ) | |
| # with gr.Row(): | |
| # with gr.Column(scale=3): | |
| # trader_no_markets_plot = plot_trader_metrics_by_market_creator( | |
| # metric_name=default_trader_metric, | |
| # traders_df=weekly_non_olas_metrics_by_market_creator, | |
| # ) | |
| # with gr.Column(scale=1): | |
| # trade_details_text = get_metrics_text(trader_type="non_Olas") | |
| # def update_no_trader_details(trader_detail): | |
| # return plot_trader_metrics_by_market_creator( | |
| # metric_name=trader_detail, | |
| # traders_df=weekly_non_olas_metrics_by_market_creator, | |
| # ) | |
| # trader_no_details_selector.change( | |
| # update_no_trader_details, | |
| # inputs=trader_no_details_selector, | |
| # outputs=trader_no_markets_plot, | |
| # ) | |
| # Unknown traders graph | |
| # if weekly_unknown_trader_metrics_by_market_creator is not None: | |
| # with gr.Row(): | |
| # gr.Markdown("# Weekly metrics of Unclassified traders") | |
| # with gr.Row(): | |
| # trader_u_details_selector = gr.Dropdown( | |
| # label="Select a weekly trader metric", | |
| # choices=trader_metric_choices, | |
| # value=default_trader_metric, | |
| # ) | |
| # with gr.Row(): | |
| # with gr.Column(scale=3): | |
| # trader_u_markets_plot = plot_trader_metrics_by_agent_categories( | |
| # metric_name=default_trader_metric, | |
| # traders_df=weekly_unknown_trader_metrics_by_market_creator, | |
| # ) | |
| # with gr.Column(scale=1): | |
| # trade_details_text = get_metrics_text( | |
| # trader_type="unclassified" | |
| # ) | |
| # def update_u_trader_details(trader_detail): | |
| # return plot_trader_metrics_by_agent_categories( | |
| # metric_name=trader_detail, | |
| # traders_df=weekly_unknown_trader_metrics_by_market_creator, | |
| # ) | |
| # trader_u_details_selector.change( | |
| # update_u_trader_details, | |
| # inputs=trader_u_details_selector, | |
| # outputs=trader_u_markets_plot, | |
| # ) | |
| with gr.TabItem("π Daily metrics (WIP)"): | |
| live_trades_current_week = get_current_week_data(trades_df=daily_info) | |
| if len(live_trades_current_week) > 0: | |
| live_metrics_by_market_creator = ( | |
| compute_daily_metrics_by_market_creator( | |
| live_trades_current_week, trader_filter=None, live_metrics=True | |
| ) | |
| ) | |
| else: | |
| live_metrics_by_market_creator = pd.DataFrame() | |
| with gr.Row(): | |
| gr.Markdown("# Daily live metrics for all trades") | |
| with gr.Row(): | |
| trade_live_details_selector = gr.Dropdown( | |
| label="Select a daily live metric", | |
| choices=trader_daily_metric_choices, | |
| value=default_daily_metric, | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| trade_live_details_plot = plot_daily_metrics( | |
| metric_name=default_daily_metric, | |
| trades_df=live_metrics_by_market_creator, | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text(daily=True) | |
| def update_trade_live_details(trade_detail, trade_live_details_plot): | |
| new_a_plot = plot_daily_metrics( | |
| metric_name=trade_detail, trades_df=live_metrics_by_market_creator | |
| ) | |
| return new_a_plot | |
| trade_live_details_selector.change( | |
| update_trade_live_details, | |
| inputs=[trade_live_details_selector, trade_live_details_plot], | |
| outputs=[trade_live_details_plot], | |
| ) | |
| # Olas traders | |
| with gr.Row(): | |
| gr.Markdown("# Daily live metrics for π Olas traders") | |
| with gr.Row(): | |
| o_trader_live_details_selector = gr.Dropdown( | |
| label="Select a daily live metric", | |
| choices=trader_daily_metric_choices, | |
| value=default_daily_metric, | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| o_trader_live_details_plot = plot_daily_metrics( | |
| metric_name=default_daily_metric, | |
| trades_df=live_metrics_by_market_creator, | |
| trader_filter="Olas", | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text(daily=True) | |
| def update_a_trader_live_details(trade_detail, a_trader_live_details_plot): | |
| o_trader_plot = plot_daily_metrics( | |
| metric_name=trade_detail, | |
| trades_df=live_metrics_by_market_creator, | |
| trader_filter="Olas", | |
| ) | |
| return o_trader_plot | |
| o_trader_live_details_selector.change( | |
| update_a_trader_live_details, | |
| inputs=[o_trader_live_details_selector, o_trader_live_details_plot], | |
| outputs=[o_trader_live_details_plot], | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("# Daily live metrics for Non-Olas traders") | |
| with gr.Row(): | |
| no_trader_live_details_selector = gr.Dropdown( | |
| label="Select a daily live metric", | |
| choices=trader_daily_metric_choices, | |
| value=default_daily_metric, | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| no_trader_live_details_plot = plot_daily_metrics( | |
| metric_name=default_daily_metric, | |
| trades_df=live_metrics_by_market_creator, | |
| trader_filter="non_Olas", | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text(daily=True) | |
| def update_na_trader_live_details( | |
| trade_detail, no_trader_live_details_plot | |
| ): | |
| no_trader_plot = plot_daily_metrics( | |
| metric_name=trade_detail, | |
| trades_df=live_metrics_by_market_creator, | |
| trader_filter="non_Olas", | |
| ) | |
| return no_trader_plot | |
| no_trader_live_details_selector.change( | |
| update_na_trader_live_details, | |
| inputs=[no_trader_live_details_selector, no_trader_live_details_plot], | |
| outputs=[no_trader_live_details_plot], | |
| ) | |
| with gr.TabItem(" Agent metrics"): | |
| with gr.Row(): | |
| gr.Markdown(" # Daily active Pearl agents") | |
| with gr.Row(): | |
| rolling_avg_plot = plot_rolling_average_dune( | |
| daa_pearl_df, | |
| ) | |
| with gr.Row(): | |
| gr.Markdown(" # Daily active Quickstart agents") | |
| with gr.Row(): | |
| rolling_avg_plot = plot_rolling_average_dune( | |
| daa_qs_df, | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("# 2-weeks rolling average ROI for Pearl agents") | |
| with gr.Row(): | |
| pearl_rolling_avg_plot = plot_rolling_average_roi( | |
| two_weeks_avg_roi_pearl_agents | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("# Average weekly ROI for Pearl agents") | |
| with gr.Row(): | |
| gr.Markdown( | |
| "This graph shows the average weekly ROI for Pearl agents. The data is based on the latest DAA results." | |
| ) | |
| with gr.Row(): | |
| weekly_avg_roi_plot = plot_weekly_average_roi( | |
| weekly_avg_roi_df=weekly_avg_roi_pearl_agents, | |
| ) | |
| with gr.TabItem("πͺ Retention metrics"): | |
| with gr.Row(): | |
| gr.Markdown("# Wow retention by trader type") | |
| with gr.Row(): | |
| gr.Markdown( | |
| """ | |
| Activity based on mech interactions for Olas and non_Olas traders and based on trading acitivity for the unclassified ones. | |
| - Olas trader: agent using Mech, with a service ID and the corresponding safe in the registry | |
| - Non-Olas trader: agent using Mech, with no service ID | |
| - Unclassified trader: agent (safe/EOAs) not using Mechs | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Wow retention in Pearl markets") | |
| wow_retention = calculate_wow_retention_by_type( | |
| retention_df, market_creator="pearl" | |
| ) | |
| wow_retention_plot = plot_wow_retention_by_type( | |
| wow_retention=wow_retention | |
| ) | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Wow retention in Quickstart markets") | |
| wow_retention = calculate_wow_retention_by_type( | |
| retention_df, market_creator="quickstart" | |
| ) | |
| wow_retention_plot = plot_wow_retention_by_type( | |
| wow_retention=wow_retention | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("# Cohort retention graphs") | |
| with gr.Row(): | |
| gr.Markdown( | |
| "The Cohort groups are organized by cohort weeks. A trader is part of a cohort group/week where it was detected the FIRST activity ever of that trader." | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| """ | |
| Week 0 for a cohort group is the same cohort week of the FIRST detected activity ever of that trader. | |
| Only two values are possible for this Week 0: | |
| 1. 100% if the cohort size is > 0, meaning all traders active that first cohort week | |
| 2. 0% if the cohort size = 0, meaning no totally new traders started activity that cohort week. | |
| """ | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Cohort retention of pearl traders") | |
| gr.Markdown("### Cohort retention of π Olas traders") | |
| cohort_retention_olas_pearl = calculate_cohort_retention( | |
| df=retention_df, market_creator="pearl", trader_type="Olas" | |
| ) | |
| cohort_retention_plot1 = plot_cohort_retention_heatmap( | |
| retention_matrix=cohort_retention_olas_pearl, cmap="Purples" | |
| ) | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Cohort retention of quickstart traders") | |
| gr.Markdown("### Cohort retention of π Olas traders") | |
| cohort_retention_olas_qs = calculate_cohort_retention( | |
| df=retention_df, market_creator="quickstart", trader_type="Olas" | |
| ) | |
| cohort_retention_plot4 = plot_cohort_retention_heatmap( | |
| retention_matrix=cohort_retention_olas_qs, | |
| cmap="Purples", | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Cohort retention of pearl traders") | |
| cohort_retention_unclassified_pearl = calculate_cohort_retention( | |
| df=retention_df, | |
| market_creator="pearl", | |
| trader_type="unclassified", | |
| ) | |
| if len(cohort_retention_unclassified_pearl) > 0: | |
| gr.Markdown("### Cohort retention of unclassified traders") | |
| cohort_retention_plot3 = plot_cohort_retention_heatmap( | |
| retention_matrix=cohort_retention_unclassified_pearl, | |
| cmap="Greens", | |
| ) | |
| with gr.Column(scale=1): | |
| gr.Markdown("## Cohort retention in quickstart traders") | |
| cohort_retention_unclassified_qs = calculate_cohort_retention( | |
| df=retention_df, | |
| market_creator="quickstart", | |
| trader_type="unclassified", | |
| ) | |
| if len(cohort_retention_unclassified_qs) > 0: | |
| gr.Markdown("### Cohort retention of unclassified traders") | |
| cohort_retention_plot6 = plot_cohort_retention_heatmap( | |
| retention_matrix=cohort_retention_unclassified_qs, | |
| cmap="Greens", | |
| ) | |
| with gr.TabItem("βοΈ Active traders"): | |
| with gr.Row(): | |
| gr.Markdown("# Active Pearl traders by trader categories") | |
| with gr.Row(): | |
| active_traders_plot = plot_active_traders(active_traders) | |
| with gr.Row(): | |
| gr.Markdown("# Active traders for Pearl markets by trader categories") | |
| with gr.Row(): | |
| active_traders_plot_pearl = plot_active_traders( | |
| active_traders, market_creator="pearl" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown("# Active Quickstart traders by trader categories") | |
| with gr.Row(): | |
| active_traders_plot_qs = plot_active_traders( | |
| active_traders, market_creator="quickstart" | |
| ) | |
| with gr.TabItem("π Markets KullbackβLeibler divergence"): | |
| with gr.Row(): | |
| gr.Markdown( | |
| "# Weekly Market Prediction Accuracy for Closed Markets (Kullback-Leibler Divergence)" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "Aka, how much off is the market predictionβs accuracy from the real outcome of the event. Values capped at 20 for market outcomes completely opposite to the real outcome." | |
| ) | |
| with gr.Row(): | |
| trade_details_text = get_metrics_text() | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| kl_div_plot = plot_kl_div_per_market(closed_markets=closed_markets) | |
| with gr.Column(scale=1): | |
| interpretation = get_interpretation_text() | |
| with gr.TabItem("π° Money invested per market category"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly total bet amount per trader type for all markets") | |
| gr.Markdown("## Computed only for traders using the mech service") | |
| with gr.Row(): | |
| total_bet_amount = plot_total_bet_amount( | |
| traders_data, market_filter="all" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "# Weekly total bet amount per trader type for Pearl markets" | |
| ) | |
| with gr.Row(): | |
| o_trader_total_bet_amount = plot_total_bet_amount( | |
| traders_data, market_filter="pearl" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "# Weekly total bet amount per trader type for Quickstart markets" | |
| ) | |
| with gr.Row(): | |
| no_trader_total_bet_amount = plot_total_bet_amount( | |
| traders_data, market_filter="quickstart" | |
| ) | |
| with gr.TabItem("π° Money invested per single market"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly bet amounts per market for all traders") | |
| gr.Markdown("## Computed only for traders using the mech service") | |
| with gr.Row(): | |
| bet_amounts = plot_total_bet_amount_per_trader_per_market(traders_data) | |
| with gr.Row(): | |
| gr.Markdown("# Weekly bet amounts per market for π Olas traders") | |
| with gr.Row(): | |
| o_trader_bet_amounts = plot_total_bet_amount_per_trader_per_market( | |
| traders_data, trader_filter="Olas" | |
| ) | |
| with gr.TabItem("ποΈWeekly winning trades % per trader"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly winning trades percentage from all traders") | |
| with gr.Row(): | |
| metrics_text = get_metrics_text() | |
| with gr.Row(): | |
| winning_metric = plot_winning_metric_per_trader(weekly_winning_metrics) | |
| with gr.Row(): | |
| gr.Markdown("# Weekly winning trades percentage from π Olas traders") | |
| with gr.Row(): | |
| metrics_text = get_metrics_text() | |
| with gr.Row(): | |
| winning_metric_olas = plot_winning_metric_per_trader( | |
| weekly_winning_metrics_olas | |
| ) | |
| demo.queue(default_concurrency_limit=40).launch() | |