Spaces:
Runtime error
Runtime error
| from datetime import datetime, timedelta | |
| import gradio as gr | |
| import pandas as pd | |
| import duckdb | |
| import logging | |
| from scripts.metrics import ( | |
| compute_weekly_metrics_by_market_creator, | |
| compute_weekly_metrics_by_trader_type, | |
| ) | |
| from tabs.trader_plots import ( | |
| plot_trader_metrics_by_market_creator, | |
| plot_trader_metrics_by_trader_type, | |
| default_trader_metric, | |
| trader_metric_choices, | |
| get_metrics_text, | |
| ) | |
| from tabs.market_plots import plot_kl_div_per_market | |
| 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() | |
| def get_all_data(): | |
| """ | |
| Get parquet files from weekly stats and new generated | |
| """ | |
| logger.info("Getting traders data") | |
| con = duckdb.connect(":memory:") | |
| # Query to fetch data from all_trades_profitability.parquet | |
| query1 = f""" | |
| SELECT * | |
| FROM read_parquet('./data/all_trades_profitability.parquet') | |
| """ | |
| df1 = con.execute(query1).fetchdf() | |
| logger.info("Got all data from all_trades_profitability.parquet") | |
| # Query to fetch data from closed_markets_div.parquet | |
| query2 = f""" | |
| SELECT * | |
| FROM read_parquet('./data/closed_markets_div.parquet') | |
| """ | |
| df2 = con.execute(query2).fetchdf() | |
| logger.info("Got all data from closed_markets_div.parquet") | |
| con.close() | |
| return df1, df2 | |
| def prepare_data(): | |
| all_trades, closed_markets = get_all_data() | |
| all_trades["creation_date"] = all_trades["creation_timestamp"].dt.date | |
| # adding multi-bet variables | |
| volume_trades_per_trader_and_market = ( | |
| all_trades.groupby(["trader_address", "title"])["roi"].count().reset_index() | |
| ) | |
| volume_trades_per_trader_and_market.rename( | |
| columns={"roi": "nr_trades_per_market"}, inplace=True | |
| ) | |
| trader_agents_data = pd.merge( | |
| all_trades, volume_trades_per_trader_and_market, on=["trader_address", "title"] | |
| ) | |
| # right now all traders are of the same type: singlebet | |
| trader_agents_data["trader_type"] = "singlebet" | |
| trader_agents_data = trader_agents_data.sort_values( | |
| by="creation_timestamp", ascending=True | |
| ) | |
| trader_agents_data["month_year_week"] = ( | |
| trader_agents_data["creation_timestamp"].dt.to_period("W").dt.strftime("%b-%d") | |
| ) | |
| closed_markets["month_year_week"] = ( | |
| closed_markets["opening_datetime"].dt.to_period("W").dt.strftime("%b-%d") | |
| ) | |
| return trader_agents_data, closed_markets | |
| trader_agents_data, closed_markets = prepare_data() | |
| print("trader agents data before computing metrics") | |
| print(trader_agents_data.head()) | |
| demo = gr.Blocks() | |
| # get weekly metrics by market creator: qs, pearl or all. | |
| weekly_metrics_by_market_creator = compute_weekly_metrics_by_market_creator( | |
| trader_agents_data | |
| ) | |
| print("weekly metrics by market creator") | |
| print(weekly_metrics_by_market_creator.head()) | |
| # get weekly metrics by trader type: multibet, singlebet or all. | |
| weekly_metrics_by_trader_type = compute_weekly_metrics_by_trader_type( | |
| trader_agents_data | |
| ) | |
| with demo: | |
| gr.HTML("<h1>Trader agents monitoring dashboard </h1>") | |
| gr.Markdown( | |
| "This app shows the weekly performance of the trader agents in Olas Predict." | |
| ) | |
| with gr.Tabs(): | |
| with gr.TabItem("🔥Trader Agents Dashboard"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly metrics of trader agents by market creator") | |
| with gr.Row(): | |
| trader_details_selector = gr.Dropdown( | |
| label="Select a 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_market_creator( | |
| metric_name=default_trader_metric, | |
| traders_df=weekly_metrics_by_market_creator, | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text() | |
| def update_trader_details(trader_detail): | |
| return plot_trader_metrics_by_market_creator( | |
| metric_name=trader_detail, | |
| traders_df=weekly_metrics_by_market_creator, | |
| ) | |
| trader_details_selector.change( | |
| update_trader_details, | |
| inputs=trader_details_selector, | |
| outputs=trader_markets_plot, | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "# Weekly metrics for trader agents by trader type (multibet or singlebet)" | |
| ) | |
| with gr.Row(): | |
| trader_metric_selector = gr.Dropdown( | |
| label="Select a trader metric", | |
| choices=trader_metric_choices, | |
| value=default_trader_metric, | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| trader_type_plot = plot_trader_metrics_by_trader_type( | |
| metric_name=default_trader_metric, | |
| traders_df=weekly_metrics_by_trader_type, | |
| ) | |
| with gr.Column(scale=1): | |
| trader_metrics_text = get_metrics_text() | |
| def update_trader_metric(trader_metric): | |
| return plot_trader_metrics_by_trader_type( | |
| metric_name=trader_metric, | |
| traders_df=weekly_metrics_by_trader_type, | |
| ) | |
| trader_metric_selector.change( | |
| update_trader_metric, | |
| inputs=trader_metric_selector, | |
| outputs=trader_type_plot, | |
| ) | |
| with gr.TabItem("📉Closed Markets Kullback–Leibler divergence"): | |
| with gr.Row(): | |
| gr.Markdown( | |
| "# Weekly Kullback–Leibler divergence computed for the closed markets" | |
| ) | |
| with gr.Row(): | |
| gr.Markdown( | |
| "This divergence is a type of statistical distance between two probability distributions P and Q. In our case P is the probability defined by the final liquidity distribution of the market. While Q is the distribution of the final outcome." | |
| ) | |
| 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): | |
| metrics_text = get_metrics_text() | |
| demo.queue(default_concurrency_limit=40).launch() | |