File size: 7,737 Bytes
2f8587a
 
2309882
2f8587a
6012e5c
2f8587a
 
 
 
 
 
 
6012e5c
 
 
 
2f8587a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f865e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ca7c50
a8f865e
 
 
6ca7c50
a8f865e
 
6ca7c50
 
 
 
 
 
 
 
 
 
 
 
 
a8f865e
 
 
 
2309882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8f865e
 
 
2309882
a8f865e
 
 
 
 
 
 
 
2309882
 
a8f865e
773af00
 
 
a8f865e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ca7c50
a8f865e
 
 
 
 
6ca7c50
2309882
a8f865e
2309882
 
 
 
 
 
773af00
 
2309882
 
 
 
 
2f8587a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6012e5c
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import pandas as pd
import gradio as gr
import plotly.express as px

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

tool_metric_choices = ["losses", "wins", "total_request", "win_perc"]

default_trade_metric = "ROI"
default_tool_metric = "win_perc"

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 get_metrics(
    metric_name: str, column_name: str, market_creator: str, trades_df: pd.DataFrame
) -> pd.DataFrame:
    # this is to filter out the data before 2023-09-01
    trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
    if market_creator != "all":
        trades_filtered = trades_filtered.loc[
            trades_filtered["market_creator"] == market_creator
        ]

    trades_filtered = (
        trades_filtered.groupby("month_year_week", sort=False)[column_name]
        .quantile([0.25, 0.5, 0.75])
        .unstack()
    )
    # reformat the data as percentile, date, value
    trades_filtered = trades_filtered.melt(
        id_vars=["month_year_week"], var_name="percentile", value_name=metric_name
    )
    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 trades_filtered


def get_boxplot_metrics(column_name: str, trades_df: pd.DataFrame) -> pd.DataFrame:
    # 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[
        ["creation_timestamp", "month_year_week", "market_creator", column_name]
    ]

    # adding the total
    trades_filtered_all = trades_filtered.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
    )
    return all_filtered_trades


def plot2_trade_details(
    metric_name: str, market_creator: str, trades_df: pd.DataFrame
) -> gr.Plot:
    """Plots the trade details for the given trade detail."""

    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)"

    trades_filtered = get_metrics(metric_name, column_name, market_creator, trades_df)
    fig = px.line(
        trades_filtered, x="month_year_week", y=metric_name, color="percentile"
    )
    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")
    return gr.Plot(
        value=fig,
    )


def plot_trade_metrics(
    metric_name: str, trades_df: pd.DataFrame, height: int = None, width: int = 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)"

    trades_filtered = get_boxplot_metrics(column_name, trades_df)
    fig = px.box(
        trades_filtered,
        x="month_year_week",
        y=column_name,
        color="market_creator",
        color_discrete_sequence=["goldenrod", "darkgreen", "purple"],
    )
    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")
    if height is not None:
        fig.update_layout(width=WIDTH, height=HEIGHT)
    return gr.Plot(
        value=fig,
    )


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,
    )


def plot_tool_metrics(wins_df: pd.DataFrame, winning_selector: str) -> gr.Plot:
    print("under construction")
    if winning_selector == "losses":
        yaxis_title = "Nr of mech calls per trade"
    elif winning_selector == "win_perc":
        column_name = "roi"
        yaxis_title = "ROI (net profit/cost)"
    elif winning_selector == "wins":
        yaxis_title = "Collateral amount per trade (xDAI)"
    else:  # "total_request"
        column_name = metric_name
        yaxis_title = "Gross profit per trade (xDAI)"