File size: 2,277 Bytes
929ee61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import pandas as pd
import gradio as gr

metric_choices = [
    "mech calls",
    "collateral amount",
    "earnings",
    "net earnings",
    "ROI",
]

default_metric = "ROI"

HEIGHT = 600
WIDTH = 1000


def plot_trade_details(metric_name: str, trades_df: pd.DataFrame) -> gr.LinePlot:
    """Plots the trade details for the given trade detail."""
    column_name = metric_name
    if metric_name == "mech calls":
        metric_name = "mech_calls"
        column_name = "num_mech_calls"
    elif metric_name == "ROI":
        column_name = "roi"

    # this is to filter out the data before 2023-09-01
    trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
    trades_filtered = (
        trades_filtered.groupby("month_year_week")[column_name]
        .quantile([0.25, 0.5, 0.75])
        .unstack()
    )
    trades_filtered.columns = trades_filtered.columns.astype(str)
    trades_filtered.reset_index(inplace=True)
    trades_filtered.columns = [
        "month_year_week",
        "25th_percentile",
        "50th_percentile",
        "75th_percentile",
    ]
    # reformat the data as percentile, date, value
    trades_filtered = trades_filtered.melt(
        id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
    )

    return gr.LinePlot(
        value=trades_filtered,
        x="month_year_week",
        y=metric_name,
        color="percentile",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "percentile", metric_name],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_average_roi_per_market_by_week(trades_df: pd.DataFrame) -> gr.LinePlot:

    mean_roi_per_market_by_week = (
        trades_df.groupby(["market_creator", "month_year_week"])["roi"]
        .mean()
        .reset_index()
    )
    mean_roi_per_market_by_week.rename(columns={"roi": "mean_roi"}, inplace=True)
    return gr.LinePlot(
        value=mean_roi_per_market_by_week,
        x="month_year_week",
        y="ROI",
        color="market_creator",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "market_creator", "mean_roi"],
        height=HEIGHT,
        width=WIDTH,
    )