File size: 11,609 Bytes
e2483e1
 
 
 
7a45667
 
 
e2483e1
 
 
7a45667
 
 
 
 
 
 
 
 
 
 
e2483e1
 
 
 
 
7a45667
 
 
 
 
 
 
 
 
 
 
e2483e1
7a45667
e2483e1
 
7a45667
e2483e1
 
7a45667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
 
7a45667
e2483e1
 
7a45667
e2483e1
 
 
 
7a45667
 
 
 
 
 
e2483e1
 
 
 
 
 
7a45667
e2483e1
 
7a45667
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
e2483e1
7a45667
e2483e1
7a45667
 
 
 
 
 
e2483e1
 
 
 
 
 
7a45667
e2483e1
 
7a45667
 
 
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
e2483e1
 
 
7a45667
 
 
 
 
 
e2483e1
 
 
 
 
 
7a45667
e2483e1
 
7a45667
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
e2483e1
7a45667
e2483e1
7a45667
 
 
 
 
 
e2483e1
 
 
 
 
 
7a45667
e2483e1
 
7a45667
 
 
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
 
 
e2483e1
7a45667
 
 
 
 
 
e2483e1
 
 
 
 
 
7a45667
e2483e1
 
7a45667
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2483e1
 
7a45667
e2483e1
 
 
 
 
 
 
 
 
 
 
7a45667
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import gradio as gr
import pandas as pd


HEIGHT = 600
WIDTH = 1000


def prepare_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
    """Prepares the trades data for analysis."""
    trades_df["creation_timestamp"] = pd.to_datetime(trades_df["creation_timestamp"])
    trades_df["creation_timestamp"] = trades_df["creation_timestamp"].dt.tz_convert(
        "UTC"
    )
    trades_df["month_year"] = (
        trades_df["creation_timestamp"].dt.to_period("M").astype(str)
    )
    trades_df["month_year_week"] = (
        trades_df["creation_timestamp"].dt.to_period("W").astype(str)
    )
    trades_df["winning_trade"] = trades_df["winning_trade"].astype(int)
    return trades_df


def get_overall_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the overall trades data for the given tools and calculates the winning percentage."""
    trades_count = trades_df.groupby("month_year_week").size().reset_index()
    trades_count.columns = trades_count.columns.astype(str)
    trades_count.rename(columns={"0": "trades"}, inplace=True)
    return trades_count


def get_overall_by_market_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the overall trades data for the given tools and calculates the winning percentage."""
    trades_count = (
        trades_df.groupby(["market_creator", "month_year_week"]).size().reset_index()
    )
    trades_count.columns = trades_count.columns.astype(str)
    trades_count.rename(columns={"0": "trades"}, inplace=True)
    return trades_count


def get_overall_winning_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the overall winning trades data for the given tools and calculates the winning percentage."""
    winning_trades = (
        trades_df.groupby(["month_year_week"])["winning_trade"].sum()
        / trades_df.groupby(["month_year_week"])["winning_trade"].count()
        * 100
    )
    # winning_trades is a series, give it a dataframe
    winning_trades = winning_trades.reset_index()
    winning_trades.columns = winning_trades.columns.astype(str)
    winning_trades.columns = ["month_year_week", "winning_trade"]
    return winning_trades


def get_overall_winning_by_market_trades(trades_df: pd.DataFrame) -> pd.DataFrame:
    """Gets the overall winning trades data for the given tools and calculates the winning percentage."""
    winning_trades = (
        trades_df.groupby(["market_creator", "month_year_week"])["winning_trade"].sum()
        / trades_df.groupby(["market_creator", "month_year_week"])[
            "winning_trade"
        ].count()
        * 100
    )
    # winning_trades is a series, give it a dataframe
    winning_trades = winning_trades.reset_index()
    winning_trades.columns = winning_trades.columns.astype(str)
    winning_trades.columns = ["market_creator", "month_year_week", "winning_trade"]
    return winning_trades


def plot_trade_details(trade_detail: str, trades_df: pd.DataFrame) -> gr.LinePlot:
    """Plots the trade details for the given trade detail."""
    if trade_detail == "mech calls":
        # 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")["num_mech_calls"]
            .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="mech_calls"
        )

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

    if trade_detail == "collateral amount":
        trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
        trades_filtered = (
            trades_filtered.groupby("month_year_week")["collateral_amount"]
            .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="collateral_amount",
        )

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

    if trade_detail == "earnings":
        trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
        trades_filtered = (
            trades_filtered.groupby("month_year_week")["earnings"]
            .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="earnings"
        )

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

    if trade_detail == "net earnings":
        trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
        trades_filtered = (
            trades_filtered.groupby("month_year_week")["net_earnings"]
            .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="net_earnings",
        )

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

    if trade_detail == "ROI":
        trades_filtered = trades_df[trades_df["creation_timestamp"] > "2023-09-01"]
        trades_filtered = (
            trades_filtered.groupby("month_year_week")["roi"]
            .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="ROI"
        )

        return gr.LinePlot(
            value=trades_filtered,
            x="month_year_week",
            y="ROI",
            color="percentile",
            show_label=True,
            interactive=True,
            show_actions_button=True,
            tooltip=["month_year_week", "percentile", "ROI"],
            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,
    )


def plot_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
    """Plots the trades data for the given tools and calculates the winning percentage."""
    return gr.BarPlot(
        value=trades_df,
        x="month_year_week",
        y="trades",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "trades"],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_trades_per_market_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
    """Plots the trades data for the given tools and calculates the winning percentage."""
    assert "market_creator" in trades_df.columns
    return gr.BarPlot(
        value=trades_df,
        x="month_year_week",
        y="trades",
        color="market_creator",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "trades"],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_winning_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
    """Plots the winning trades data for the given tools and calculates the winning percentage."""
    return gr.BarPlot(
        value=trades_df,
        x="month_year_week",
        y="winning_trade",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "winning_trade"],
        height=HEIGHT,
        width=WIDTH,
    )


def plot_winning_trades_per_market_by_week(trades_df: pd.DataFrame) -> gr.BarPlot:
    """Plots the winning trades data for the given tools and calculates the winning percentage."""
    assert "market_creator" in trades_df.columns
    return gr.BarPlot(
        value=trades_df,
        x="month_year_week",
        y="winning_trade",
        color="market_creator",
        show_label=True,
        interactive=True,
        show_actions_button=True,
        tooltip=["month_year_week", "winning_trade"],
        height=HEIGHT,
        width=WIDTH,
    )