File size: 5,687 Bytes
16d3c27
 
c50dc76
f4470aa
da55889
16d3c27
0b04461
16d3c27
 
 
 
 
 
 
681e0a4
c255cf4
 
 
 
 
 
0b04461
 
ed33e36
c255cf4
16d3c27
 
 
 
 
29746ae
f4470aa
29746ae
984590a
29746ae
f4470aa
29746ae
 
 
 
 
 
 
 
 
f4470aa
29746ae
 
984590a
e51ae04
 
 
984590a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681e0a4
ac8ae1f
e51ae04
ac8ae1f
e51ae04
681e0a4
ac8ae1f
e51ae04
ac8ae1f
e51ae04
 
 
da55889
 
 
6992ec1
da55889
 
 
 
6992ec1
984590a
 
 
 
 
681e0a4
43fad47
c50dc76
984590a
c50dc76
 
 
 
 
 
da55889
 
c50dc76
 
 
 
 
f9ef62b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c523075
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pandas as pd
import gradio as gr
import plotly.express as px
import gc
from datetime import datetime

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


tool_metric_choices = {
    "Weekly Mean Mech Tool Accuracy as (Accurate Responses/All) %": "win_perc",
    "Total Weekly Inaccurate Nr of Mech Tool Responses": "losses",
    "Total Weekly Accurate Nr of Mech Tool Responses": "wins",
    "Total Weekly Nr of Mech Tool Requests": "total_request",
}

default_trade_metric = "ROI"

default_tool_metric = "Weekly Mean Mech Tool Accuracy as (Accurate Responses/All) %"

HEIGHT = 600
WIDTH = 1000


def get_boxplot_metrics(column_name: str, trades_df: pd.DataFrame) -> pd.DataFrame:
    trades_filtered = trades_df[
        ["creation_timestamp", "month_year_week", "market_creator", column_name]
    ]
    # adding the total
    trades_filtered_all = trades_df.copy(deep=True)
    trades_filtered_all["market_creator"] = "all"

    # merging both dataframes
    all_filtered_trades = pd.concat(
        [trades_filtered, trades_filtered_all], ignore_index=True
    )
    all_filtered_trades = all_filtered_trades.sort_values(
        by="creation_timestamp", ascending=True
    )
    gc.collect()
    return all_filtered_trades


def plot_trade_metrics(
    metric_name: str, trades_df: pd.DataFrame, trader_filter: str = None
) -> gr.Plot:
    """Plots the trade metrics."""

    if metric_name == "mech calls":
        metric_name = "mech_calls"
        column_name = "num_mech_calls"
        yaxis_title = "Nr of mech calls per trade"
    elif metric_name == "ROI":
        column_name = "roi"
        yaxis_title = "ROI (net profit/cost)"
    elif metric_name == "collateral amount":
        metric_name = "collateral_amount"
        column_name = metric_name
        yaxis_title = "Collateral amount per trade (xDAI)"
    elif metric_name == "net earnings":
        metric_name = "net_earnings"
        column_name = metric_name
        yaxis_title = "Net profit per trade (xDAI)"
    else:  # earnings
        column_name = metric_name
        yaxis_title = "Gross profit per trade (xDAI)"

    color_discrete = ["purple", "darkgoldenrod", "darkgreen"]
    if trader_filter == "Olas":
        trades_filtered = get_boxplot_metrics(
            column_name, trades_df.loc[trades_df["staking"] != "non_Olas"]
        )
        color_discrete = ["darkviolet", "goldenrod", "green"]
    elif trader_filter == "non_Olas":
        trades_filtered = get_boxplot_metrics(
            column_name, trades_df.loc[trades_df["staking"] == "non_Olas"]
        )
    else:
        trades_filtered = get_boxplot_metrics(column_name, trades_df)
    # Convert string dates to datetime and sort them
    all_dates_dt = sorted(
        [
            datetime.strptime(date, "%b-%d-%Y")
            for date in trades_filtered["month_year_week"].unique()
        ]
    )
    # Convert back to string format
    all_dates = [date.strftime("%b-%d-%Y") for date in all_dates_dt]
    fig = px.box(
        trades_filtered,
        x="month_year_week",
        y=column_name,
        color="market_creator",
        color_discrete_sequence=color_discrete,
        category_orders={"market_creator": ["pearl", "quickstart", "all"]},
    )
    fig.update_traces(boxmean=True)
    fig.update_layout(
        xaxis_title="Week",
        yaxis_title=yaxis_title,
        legend=dict(yanchor="top", y=0.5),
    )
    fig.update_xaxes(tickformat="%b %d\n%Y")
    # Update layout to force x-axis category order (hotfix for a sorting issue)
    fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": all_dates})
    return gr.Plot(
        value=fig,
    )


def get_trade_metrics_text(trader_type: str = None) -> gr.Markdown:
    if trader_type is None:
        metric_text = """ 
            ## Description of the graph
            These metrics are computed weekly. The statistical measures are:
            * min, max, 25th(q1), 50th(median) and 75th(q2) percentiles
            * the upper and lower fences to delimit possible outliers
            * the average values as the dotted lines
            """
    elif trader_type == "Olas":
        metric_text = """ 
            ## Definition of Olas trader
            Agents using Mech, with a service ID and the corresponding safe in the registry
            ## Description of the graph
            These metrics are computed weekly. The statistical measures are:
            * min, max, 25th(q1), 50th(median) and 75th(q2) percentiles
            * the upper and lower fences to delimit possible outliers
            * the average values as the dotted lines
            """
    elif trader_type == "non_Olas":
        metric_text = """ 
            ## Definition of non-Olas trader
            Agents using Mech, with no service ID
            ## Description of the graph
            These metrics are computed weekly. The statistical measures are:
            * min, max, 25th(q1), 50th(median) and 75th(q2) percentiles
            * the upper and lower fences to delimit possible outliers
            * the average values as the dotted lines
            """
    else:  # Unclassified
        metric_text = """ 
            ## Definition of unclassified trader
            Agents (safe/EOAs) not using Mechs
            ## Description of the graph
            These metrics are computed weekly. The statistical measures are:
            * min, max, 25th(q1), 50th(median) and 75th(q2) percentiles
            * the upper and lower fences to delimit possible outliers
            * the average values as the dotted lines
            """
    return gr.Markdown(metric_text)