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_daily_metrics_by_market_creator, | |
| compute_winning_metrics_by_trader, | |
| ) | |
| from tabs.trader_plots import ( | |
| plot_trader_metrics_by_market_creator, | |
| plot_trader_daily_metrics_by_market_creator, | |
| default_trader_metric, | |
| trader_metric_choices, | |
| get_metrics_text, | |
| plot_winning_metric_per_trader, | |
| get_interpretation_text, | |
| ) | |
| from scripts.utils import get_traders_family | |
| from scripts.trades_volume_per_market import plot_weekly_trades_volume_by_trader_family | |
| 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 | |
| # nr-trades variable | |
| 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"] | |
| ) | |
| # adding the trader family column | |
| trader_agents_data["trader_family"] = trader_agents_data.apply( | |
| lambda x: get_traders_family(x), axis=1 | |
| ) | |
| print(trader_agents_data.trader_family.value_counts()) | |
| 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 | |
| ) | |
| daily_metrics_by_market_creator = compute_daily_metrics_by_market_creator( | |
| trader_agents_data | |
| ) | |
| weekly_agent_metrics_by_market_creator = compute_weekly_metrics_by_market_creator( | |
| trader_agents_data, trader_filter="agent" | |
| ) | |
| weekly_non_agent_metrics_by_market_creator = compute_weekly_metrics_by_market_creator( | |
| trader_agents_data, trader_filter="non_agent" | |
| ) | |
| # print("weekly metrics by market creator") | |
| # print(weekly_metrics_by_market_creator.head()) | |
| weekly_winning_metrics = compute_winning_metrics_by_trader( | |
| trader_agents_data=trader_agents_data | |
| ) | |
| weekly_agent_winning_metrics = compute_winning_metrics_by_trader( | |
| trader_agents_data=trader_agents_data, trader_filter="agent" | |
| ) | |
| weekly_non_agent_winning_metrics = compute_winning_metrics_by_trader( | |
| trader_agents_data=trader_agents_data, trader_filter="non_agent" | |
| ) | |
| 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("π₯ Weekly metrics"): | |
| with gr.Row(): | |
| gr.Markdown("# Weekly metrics of 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_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, | |
| ) | |
| # Agentic traders graph | |
| with gr.Row(): | |
| gr.Markdown("# Weekly metrics of trader Agents") | |
| with gr.Row(): | |
| trader_a_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): | |
| a_trader_markets_plot = plot_trader_metrics_by_market_creator( | |
| metric_name=default_trader_metric, | |
| traders_df=weekly_agent_metrics_by_market_creator, | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text() | |
| def update_a_trader_details(trader_detail): | |
| return plot_trader_metrics_by_market_creator( | |
| metric_name=trader_detail, | |
| traders_df=weekly_agent_metrics_by_market_creator, | |
| ) | |
| trader_a_details_selector.change( | |
| update_a_trader_details, | |
| inputs=trader_a_details_selector, | |
| outputs=a_trader_markets_plot, | |
| ) | |
| # Non-agentic traders graph | |
| with gr.Row(): | |
| gr.Markdown("# Weekly metrics of Non-agent traders") | |
| with gr.Row(): | |
| trader_na_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): | |
| na_trader_markets_plot = plot_trader_metrics_by_market_creator( | |
| metric_name=default_trader_metric, | |
| traders_df=weekly_non_agent_metrics_by_market_creator, | |
| ) | |
| with gr.Column(scale=1): | |
| trade_details_text = get_metrics_text() | |
| def update_na_trader_details(trader_detail): | |
| return plot_trader_metrics_by_market_creator( | |
| metric_name=trader_detail, | |
| traders_df=weekly_non_agent_metrics_by_market_creator, | |
| ) | |
| trader_na_details_selector.change( | |
| update_na_trader_details, | |
| inputs=trader_na_details_selector, | |
| outputs=na_trader_markets_plot, | |
| ) | |
| # with gr.TabItem("π₯ Daily metrics (WIP)"): | |
| # with gr.Row(): | |
| # gr.Markdown("# Daily metrics of last week of all traders") | |
| # with gr.Row(): | |
| # trader_daily_details_selector = gr.Dropdown( | |
| # label="Select a daily trader metric", | |
| # choices=trader_metric_choices, | |
| # value=default_trader_metric, | |
| # ) | |
| # with gr.Row(): | |
| # with gr.Column(scale=3): | |
| # trader_daily_markets_plot = ( | |
| # plot_trader_daily_metrics_by_market_creator( | |
| # metric_name=default_trader_metric, | |
| # traders_df=daily_metrics_by_market_creator, | |
| # ) | |
| # ) | |
| # with gr.Column(scale=1): | |
| # trade_details_text = get_metrics_text() | |
| # def update_trader_daily_details(trader_detail): | |
| # return plot_trader_daily_metrics_by_market_creator( | |
| # metric_name=trader_detail, | |
| # traders_df=daily_metrics_by_market_creator, | |
| # ) | |
| # trader_daily_details_selector.change( | |
| # update_trader_daily_details, | |
| # inputs=trader_daily_details_selector, | |
| # outputs=trader_daily_markets_plot, | |
| # ) | |
| with gr.TabItem("πClosed 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.Row(): | |
| gr.Markdown( | |
| "# Weekly volume of trades at each market per trader family" | |
| ) | |
| with gr.Row(): | |
| trades_volume_plot = plot_weekly_trades_volume_by_trader_family( | |
| trader_agents_data | |
| ) | |
| 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) | |
| # Agentic traders | |
| with gr.Row(): | |
| gr.Markdown("# Weekly winning trades percentage from traders Agents") | |
| with gr.Row(): | |
| metrics_text = get_metrics_text() | |
| with gr.Row(): | |
| winning_metric = plot_winning_metric_per_trader( | |
| weekly_agent_winning_metrics | |
| ) | |
| # Non_agentic traders | |
| with gr.Row(): | |
| gr.Markdown("# Weekly winning trades percentage from Non-agent traders") | |
| with gr.Row(): | |
| metrics_text = get_metrics_text() | |
| with gr.Row(): | |
| winning_metric = plot_winning_metric_per_trader( | |
| weekly_non_agent_winning_metrics | |
| ) | |
| demo.queue(default_concurrency_limit=40).launch() | |