File size: 9,128 Bytes
d8cf478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52d1750
 
 
d8cf478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d41146f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d8cf478
 
 
3ed8c7a
 
 
 
 
 
 
 
 
6154c13
3ed8c7a
 
 
 
 
6154c13
 
d8cf478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d41146f
d8cf478
 
6154c13
 
 
 
 
 
3ed8c7a
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from tqdm import tqdm

DEFAULT_MECH_FEE = 0.01  # xDAI


def compute_metrics(trader_address: str, trader_data: pd.DataFrame) -> dict:

    if len(trader_data) == 0:
        print("No data to compute metrics")
        return {}

    weekly_metrics = {}
    weekly_metrics["trader_address"] = trader_address
    total_net_earnings = trader_data.net_earnings.sum()
    total_bet_amounts = trader_data.collateral_amount.sum()
    total_num_mech_calls = trader_data.num_mech_calls.sum()
    weekly_metrics["net_earnings"] = total_net_earnings
    weekly_metrics["earnings"] = trader_data.earnings.sum()
    weekly_metrics["bet_amount"] = total_bet_amounts
    weekly_metrics["nr_mech_calls"] = total_num_mech_calls
    total_fee_amounts = trader_data.mech_fee_amount.sum()
    total_costs = (
        total_bet_amounts
        + total_fee_amounts
        + (total_num_mech_calls * DEFAULT_MECH_FEE)
    )
    weekly_metrics["roi"] = total_net_earnings / total_costs
    return weekly_metrics


def compute_trader_metrics_by_trader_type(
    trader_address: str, week_traders_data: pd.DataFrame, trader_type: str = "all"
) -> pd.DataFrame:
    """This function computes for a specific week the different metrics: roi, net_earnings, earnings, bet_amount, nr_mech_calls.
    The global roi of the trader agent by computing the individual net profit and the indivicual costs values
    achieved per market and dividing both.
    It is possible to filter by trader type: multibet, singlebet, all"""
    assert "trader_type" in week_traders_data.columns
    filtered_traders_data = week_traders_data.loc[
        week_traders_data["trader_address"] == trader_address
    ]

    if trader_type != "all":  # compute only for the specific type
        filtered_traders_data = filtered_traders_data.loc[
            filtered_traders_data["trader_type"] == trader_type
        ]
        if len(filtered_traders_data) == 0:
            return pd.DataFrame()  # No Data

    return compute_metrics(trader_address, filtered_traders_data)


def compute_trader_metrics_by_market_creator(
    trader_address: str, week_traders_data: pd.DataFrame, market_creator: str = "all"
) -> dict:
    """This function computes for a specific week the different metrics: roi, net_earnings, earnings, bet_amount, nr_mech_calls.
    The global roi of the trader agent by computing the individual net profit and the indivicual costs values
    achieved per market and dividing both.
    It is possible to filter by market creator: quickstart, pearl, all"""
    assert "market_creator" in week_traders_data.columns
    filtered_traders_data = week_traders_data.loc[
        week_traders_data["trader_address"] == trader_address
    ]
    if market_creator != "all":  # compute only for the specific market creator
        filtered_traders_data = filtered_traders_data.loc[
            filtered_traders_data["market_creator"] == market_creator
        ]
        if len(filtered_traders_data) == 0:
            tqdm.write(f"No data. Skipping market creator {market_creator}")
            return {}  # No Data
    # tqdm.write(
    #     f"Volume of data for trader {trader_address} and market creator {market_creator} = {len(filtered_traders_data)}"
    # )
    metrics = compute_metrics(trader_address, filtered_traders_data)
    return metrics


def merge_trader_metrics(
    trader: str, weekly_data: pd.DataFrame, week: str
) -> pd.DataFrame:
    trader_metrics = []
    # computation as specification 1 for all types of markets
    weekly_metrics_all = compute_trader_metrics_by_market_creator(
        trader, weekly_data, market_creator="all"
    )
    weekly_metrics_all["month_year_week"] = week
    weekly_metrics_all["market_creator"] = "all"
    trader_metrics.append(weekly_metrics_all)

    # computation as specification 1 for quickstart markets
    weekly_metrics_qs = compute_trader_metrics_by_market_creator(
        trader, weekly_data, market_creator="quickstart"
    )
    if len(weekly_metrics_qs) > 0:
        weekly_metrics_qs["month_year_week"] = week
        weekly_metrics_qs["market_creator"] = "quickstart"
        trader_metrics.append(weekly_metrics_qs)
    # computation as specification 1 for pearl markets
    weekly_metrics_pearl = compute_trader_metrics_by_market_creator(
        trader, weekly_data, market_creator="pearl"
    )
    if len(weekly_metrics_pearl) > 0:
        weekly_metrics_pearl["month_year_week"] = week
        weekly_metrics_pearl["market_creator"] = "pearl"
        trader_metrics.append(weekly_metrics_pearl)
    result = pd.DataFrame.from_dict(trader_metrics, orient="columns")
    # tqdm.write(f"Total length of all trader metrics for this week = {len(result)}")
    return result


def merge_trader_metrics_by_type(
    trader: str, weekly_data: pd.DataFrame, week: str
) -> pd.DataFrame:
    trader_metrics = []
    # computation as specification 1 for all types of traders
    weekly_metrics_all = compute_trader_metrics_by_trader_type(
        trader, weekly_data, trader_type="all"
    )
    weekly_metrics_all["month_year_week"] = week
    weekly_metrics_all["trader_type"] = "all"
    trader_metrics.append(weekly_metrics_all)

    # computation as specification 1 for multibet traders
    weekly_metrics_mb = compute_trader_metrics_by_trader_type(
        trader, weekly_data, trader_type="multibet"
    )
    if len(weekly_metrics_mb) > 0:
        weekly_metrics_mb["month_year_week"] = week
        weekly_metrics_mb["trader_type"] = "multibet"
        trader_metrics.append(weekly_metrics_mb)

    # computation as specification 1 for singlebet traders
    weekly_metrics_sb = compute_trader_metrics_by_trader_type(
        trader, weekly_data, trader_type="singlebet"
    )
    if len(weekly_metrics_sb) > 0:
        weekly_metrics_sb["month_year_week"] = week
        weekly_metrics_sb["trader_type"] = "singlebet"
        trader_metrics.append(weekly_metrics_sb)
    result = pd.DataFrame.from_dict(trader_metrics, orient="columns")
    # tqdm.write(f"Total length of all trader metrics for this week = {len(result)}")
    return result


def win_metrics_trader_level(weekly_data):
    winning_trades = (
        weekly_data.groupby(
            ["month_year_week", "market_creator", "trader_address"], sort=False
        )["winning_trade"].sum()
        / weekly_data.groupby(
            ["month_year_week", "market_creator", "trader_address"], sort=False
        )["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.rename(columns={"winning_trade": "winning_perc"}, inplace=True)
    return winning_trades


def compute_weekly_metrics_by_market_creator(
    trader_agents_data: pd.DataFrame,
) -> pd.DataFrame:
    """Function to compute the metrics at the trader level per week and with different categories by market creator"""
    contents = []
    all_weeks = list(trader_agents_data.month_year_week.unique())
    for week in all_weeks:
        weekly_data = trader_agents_data.loc[
            trader_agents_data["month_year_week"] == week
        ]
        print(f"Computing weekly metrics for week ={week} by market creator")
        # traverse each trader agent
        traders = list(weekly_data.trader_address.unique())
        for trader in tqdm(traders, desc=f"Trader' metrics", unit="metrics"):
            contents.append(merge_trader_metrics(trader, weekly_data, week))
    print("End computing all weekly metrics by market creator")
    return pd.concat(contents, ignore_index=True)


def compute_weekly_metrics_by_trader_type(
    trader_agents_data: pd.DataFrame,
) -> pd.DataFrame:
    """Function to compute the metrics at the trader level per week and with different types of traders"""
    contents = []
    all_weeks = list(trader_agents_data.month_year_week.unique())
    for week in all_weeks:
        weekly_data = trader_agents_data.loc[
            trader_agents_data["month_year_week"] == week
        ]
        print(f"Computing weekly metrics for week ={week} by trader type")
        # traverse each trader agent
        traders = list(weekly_data.trader_address.unique())
        for trader in tqdm(traders, desc=f"Trader' metrics", unit="metrics"):
            contents.append(merge_trader_metrics_by_type(trader, weekly_data, week))
    print("End computing all weekly metrics by trader types")
    return pd.concat(contents, ignore_index=True)


def compute_winning_metrics_by_trader(
    trader_agents_data: pd.DataFrame,
) -> pd.DataFrame:
    """Function to compute the winning metrics at the trader level per week and with different market creators"""
    market_all = trader_agents_data.copy(deep=True)
    market_all["market_creator"] = "all"

    # merging both dataframes
    final_traders = pd.concat([market_all, trader_agents_data], ignore_index=True)
    final_traders = final_traders.sort_values(by="creation_timestamp", ascending=True)

    winning_df = win_metrics_trader_level(final_traders)
    winning_df.head()
    return winning_df