cyberosa
commited on
Commit
·
5aa6873
1
Parent(s):
12193ab
new dataset for 2-weeks rolling avg
Browse files- scripts/predict_kpis.py +122 -26
- two_weeks_avg_roi_pearl_agents.parquet +3 -0
scripts/predict_kpis.py
CHANGED
|
@@ -302,11 +302,12 @@ def get_trades_on_closed_markets_by_pearl_agents() -> pd.DataFrame:
|
|
| 302 |
raise ValueError("No trades found for the pearl agents")
|
| 303 |
|
| 304 |
|
| 305 |
-
def
|
| 306 |
agent_trades: pd.DataFrame,
|
| 307 |
mech_calls: pd.DataFrame,
|
| 308 |
agent: str,
|
| 309 |
-
|
|
|
|
| 310 |
) -> dict:
|
| 311 |
# ROI formula net_earnings/total_costs
|
| 312 |
earnings = agent_trades.earnings.sum()
|
|
@@ -316,24 +317,38 @@ def compute_market_agent_roi(
|
|
| 316 |
total_costs = total_bet_amount + total_market_fees + total_mech_fees
|
| 317 |
net_earnings = earnings - total_costs
|
| 318 |
if total_costs == 0:
|
| 319 |
-
raise ValueError(f"Total costs for agent {agent}
|
| 320 |
roi = net_earnings / total_costs
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
|
| 331 |
-
def
|
|
|
|
|
|
|
| 332 |
agent_trades = get_trades_on_closed_markets_by_pearl_agents()
|
| 333 |
-
|
| 334 |
-
agent_trades
|
| 335 |
)
|
| 336 |
-
|
| 337 |
agent_trades["creation_timestamp"] = pd.to_datetime(
|
| 338 |
agent_trades["creation_timestamp"]
|
| 339 |
)
|
|
@@ -342,21 +357,40 @@ def compute_weekly_avg_roi_pearl_agents() -> pd.DataFrame:
|
|
| 342 |
].dt.tz_convert("UTC")
|
| 343 |
agent_trades["creation_date"] = agent_trades["creation_timestamp"].dt.date
|
| 344 |
agent_trades = agent_trades.sort_values(by="creation_timestamp", ascending=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
agent_trades["week_start"] = (
|
| 347 |
agent_trades["creation_timestamp"].dt.to_period("W").dt.start_time
|
| 348 |
)
|
| 349 |
|
| 350 |
grouped_trades = agent_trades.groupby("week_start")
|
| 351 |
contents = []
|
| 352 |
-
|
| 353 |
# Iterate through the groups (each group represents a week)
|
| 354 |
for week, week_data in grouped_trades:
|
| 355 |
print(f"Week: {week}") # Print the week identifier
|
| 356 |
|
| 357 |
# for all closed markets
|
| 358 |
closed_markets = week_data.title.unique()
|
| 359 |
-
agents = week_data.trader_address.unique()
|
| 360 |
for agent in agents:
|
| 361 |
# compute all trades done by the agent on those markets, no matter from which week
|
| 362 |
agent_markets_data = agent_trades.loc[
|
|
@@ -371,11 +405,12 @@ def compute_weekly_avg_roi_pearl_agents() -> pd.DataFrame:
|
|
| 371 |
(agent_mech_requests["trader_address"] == agent)
|
| 372 |
& (agent_mech_requests["title"].isin(closed_markets))
|
| 373 |
]
|
|
|
|
| 374 |
# compute the ROI for that market, that trader and that week
|
| 375 |
try:
|
| 376 |
# Convert the dictionary to DataFrame before appending
|
| 377 |
-
roi_dict =
|
| 378 |
-
agent_markets_data, agent_mech_calls, agent, week
|
| 379 |
)
|
| 380 |
contents.append(pd.DataFrame([roi_dict]))
|
| 381 |
except ValueError as e:
|
|
@@ -392,9 +427,59 @@ def compute_weekly_avg_roi_pearl_agents() -> pd.DataFrame:
|
|
| 392 |
return weekly_avg_roi_data
|
| 393 |
|
| 394 |
|
| 395 |
-
def
|
| 396 |
-
|
| 397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
|
| 399 |
|
| 400 |
if __name__ == "__main__":
|
|
@@ -402,7 +487,18 @@ if __name__ == "__main__":
|
|
| 402 |
prepare_predict_services_dataset()
|
| 403 |
# dune = setup_dune_python_client()
|
| 404 |
# load_predict_services_file(dune_client=dune)
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
raise ValueError("No trades found for the pearl agents")
|
| 303 |
|
| 304 |
|
| 305 |
+
def compute_markets_agent_roi(
|
| 306 |
agent_trades: pd.DataFrame,
|
| 307 |
mech_calls: pd.DataFrame,
|
| 308 |
agent: str,
|
| 309 |
+
period: str,
|
| 310 |
+
period_value: datetime,
|
| 311 |
) -> dict:
|
| 312 |
# ROI formula net_earnings/total_costs
|
| 313 |
earnings = agent_trades.earnings.sum()
|
|
|
|
| 317 |
total_costs = total_bet_amount + total_market_fees + total_mech_fees
|
| 318 |
net_earnings = earnings - total_costs
|
| 319 |
if total_costs == 0:
|
| 320 |
+
raise ValueError(f"Total costs for agent {agent} are zero")
|
| 321 |
roi = net_earnings / total_costs
|
| 322 |
+
if period == "week":
|
| 323 |
+
return {
|
| 324 |
+
"trader_address": agent,
|
| 325 |
+
"week_start": period_value,
|
| 326 |
+
"roi": roi,
|
| 327 |
+
"net_earnings": net_earnings,
|
| 328 |
+
"total_bet_amount": total_bet_amount,
|
| 329 |
+
"total_mech_calls": len(mech_calls),
|
| 330 |
+
}
|
| 331 |
+
if period == "day":
|
| 332 |
+
return {
|
| 333 |
+
"trader_address": agent,
|
| 334 |
+
"creation_date": period_value,
|
| 335 |
+
"roi": roi,
|
| 336 |
+
"net_earnings": net_earnings,
|
| 337 |
+
"total_bet_amount": total_bet_amount,
|
| 338 |
+
"total_mech_calls": len(mech_calls),
|
| 339 |
+
}
|
| 340 |
+
raise ValueError(
|
| 341 |
+
f"Invalid period {period} for agent {agent}. Expected 'week' or 'day'."
|
| 342 |
+
)
|
| 343 |
|
| 344 |
|
| 345 |
+
def prepare_agents_data() -> Tuple[pd.DataFrame, pd.DataFrame]:
|
| 346 |
+
"""Function to prepare the agents data for the predict ROI KPIs computation"""
|
| 347 |
+
# Get the trades done by pearl agents on closed markets
|
| 348 |
agent_trades = get_trades_on_closed_markets_by_pearl_agents()
|
| 349 |
+
print(
|
| 350 |
+
f"Number of trades done by pearl agents on closed markets: {len(agent_trades)}"
|
| 351 |
)
|
|
|
|
| 352 |
agent_trades["creation_timestamp"] = pd.to_datetime(
|
| 353 |
agent_trades["creation_timestamp"]
|
| 354 |
)
|
|
|
|
| 357 |
].dt.tz_convert("UTC")
|
| 358 |
agent_trades["creation_date"] = agent_trades["creation_timestamp"].dt.date
|
| 359 |
agent_trades = agent_trades.sort_values(by="creation_timestamp", ascending=True)
|
| 360 |
+
# Get the mech requests done by pearl agents on closed markets
|
| 361 |
+
agent_mech_requests = get_mech_requests_on_closed_markets_by_pearl_agents(
|
| 362 |
+
agent_trades
|
| 363 |
+
)
|
| 364 |
+
agent_mech_requests["request_time"] = pd.to_datetime(
|
| 365 |
+
agent_mech_requests["request_time"], utc=True
|
| 366 |
+
)
|
| 367 |
+
agent_mech_requests = agent_mech_requests.sort_values(
|
| 368 |
+
by="request_time", ascending=True
|
| 369 |
+
)
|
| 370 |
+
agent_mech_requests["request_date"] = agent_mech_requests["request_time"].dt.date
|
| 371 |
+
print(
|
| 372 |
+
f"Number of mech requests done by pearl agents on closed markets: {len(agent_mech_requests)}"
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
return agent_trades, agent_mech_requests
|
| 376 |
|
| 377 |
+
|
| 378 |
+
def compute_weekly_avg_roi_pearl_agents(
|
| 379 |
+
agent_trades, agent_mech_requests
|
| 380 |
+
) -> pd.DataFrame:
|
| 381 |
agent_trades["week_start"] = (
|
| 382 |
agent_trades["creation_timestamp"].dt.to_period("W").dt.start_time
|
| 383 |
)
|
| 384 |
|
| 385 |
grouped_trades = agent_trades.groupby("week_start")
|
| 386 |
contents = []
|
| 387 |
+
agents = agent_trades.trader_address.unique()
|
| 388 |
# Iterate through the groups (each group represents a week)
|
| 389 |
for week, week_data in grouped_trades:
|
| 390 |
print(f"Week: {week}") # Print the week identifier
|
| 391 |
|
| 392 |
# for all closed markets
|
| 393 |
closed_markets = week_data.title.unique()
|
|
|
|
| 394 |
for agent in agents:
|
| 395 |
# compute all trades done by the agent on those markets, no matter from which week
|
| 396 |
agent_markets_data = agent_trades.loc[
|
|
|
|
| 405 |
(agent_mech_requests["trader_address"] == agent)
|
| 406 |
& (agent_mech_requests["title"].isin(closed_markets))
|
| 407 |
]
|
| 408 |
+
|
| 409 |
# compute the ROI for that market, that trader and that week
|
| 410 |
try:
|
| 411 |
# Convert the dictionary to DataFrame before appending
|
| 412 |
+
roi_dict = compute_markets_agent_roi(
|
| 413 |
+
agent_markets_data, agent_mech_calls, agent, "week", week
|
| 414 |
)
|
| 415 |
contents.append(pd.DataFrame([roi_dict]))
|
| 416 |
except ValueError as e:
|
|
|
|
| 427 |
return weekly_avg_roi_data
|
| 428 |
|
| 429 |
|
| 430 |
+
def compute_two_weeks_rolling_avg_roi_pearl_agents(
|
| 431 |
+
agents_trades: pd.DataFrame, agents_mech_requests: pd.DataFrame
|
| 432 |
+
) -> pd.DataFrame:
|
| 433 |
+
grouped_trades = agents_trades.groupby("creation_date")
|
| 434 |
+
contents = []
|
| 435 |
+
agents = agents_trades.trader_address.unique()
|
| 436 |
+
# Iterate through the groups (each group represents a day)
|
| 437 |
+
for day, day_data in grouped_trades:
|
| 438 |
+
# take all closed markets in two weeks before that day
|
| 439 |
+
print(f"Day: {day}") # Print the day identifier
|
| 440 |
+
two_weeks_ago = day - timedelta(days=14)
|
| 441 |
+
two_weeks_data = agents_trades.loc[
|
| 442 |
+
(agents_trades["creation_date"] >= two_weeks_ago)
|
| 443 |
+
& (agents_trades["creation_date"] <= day)
|
| 444 |
+
]
|
| 445 |
+
if len(two_weeks_data) == 0:
|
| 446 |
+
# not betting activity
|
| 447 |
+
continue
|
| 448 |
+
# for all closed markets
|
| 449 |
+
closed_markets = two_weeks_data.title.unique()
|
| 450 |
+
for agent in agents:
|
| 451 |
+
# take trades done by the agent two weeks before that day using creation_date and delta
|
| 452 |
+
agent_markets_data = agents_trades.loc[
|
| 453 |
+
(agents_trades["trader_address"] == agent)
|
| 454 |
+
& (agents_trades["title"].isin(closed_markets))
|
| 455 |
+
]
|
| 456 |
+
if len(agent_markets_data) == 0:
|
| 457 |
+
# not betting activity
|
| 458 |
+
continue
|
| 459 |
+
|
| 460 |
+
# filter mech requests done by the agent on that market
|
| 461 |
+
agent_mech_calls = agents_mech_requests.loc[
|
| 462 |
+
(agents_mech_requests["trader_address"] == agent)
|
| 463 |
+
& (agents_mech_requests["title"].isin(closed_markets))
|
| 464 |
+
]
|
| 465 |
+
# compute the ROI for these markets, that trader and for this period
|
| 466 |
+
try:
|
| 467 |
+
# Convert the dictionary to DataFrame before appending
|
| 468 |
+
roi_dict = compute_markets_agent_roi(
|
| 469 |
+
agent_markets_data, agent_mech_calls, agent, "day", day
|
| 470 |
+
)
|
| 471 |
+
contents.append(pd.DataFrame([roi_dict]))
|
| 472 |
+
except ValueError as e:
|
| 473 |
+
print(f"Skipping ROI calculation: {e}")
|
| 474 |
+
continue
|
| 475 |
+
two_weeks_avg_data = pd.concat(contents, ignore_index=True)
|
| 476 |
+
|
| 477 |
+
two_weeks_rolling_avg_roi = (
|
| 478 |
+
two_weeks_avg_data.groupby("creation_date")["roi"]
|
| 479 |
+
.mean()
|
| 480 |
+
.reset_index(name="two_weeks_avg_roi")
|
| 481 |
+
)
|
| 482 |
+
return two_weeks_rolling_avg_roi
|
| 483 |
|
| 484 |
|
| 485 |
if __name__ == "__main__":
|
|
|
|
| 487 |
prepare_predict_services_dataset()
|
| 488 |
# dune = setup_dune_python_client()
|
| 489 |
# load_predict_services_file(dune_client=dune)
|
| 490 |
+
agents_trades, agents_mech_requests = prepare_agents_data()
|
| 491 |
+
|
| 492 |
+
weekly_avg = compute_weekly_avg_roi_pearl_agents(
|
| 493 |
+
agents_trades, agents_mech_requests
|
| 494 |
+
)
|
| 495 |
+
print(weekly_avg.head())
|
| 496 |
+
# save in a file
|
| 497 |
+
weekly_avg.to_parquet(ROOT_DIR / "weekly_avg_roi_pearl_agents.parquet")
|
| 498 |
+
|
| 499 |
+
two_weeks_avg = compute_two_weeks_rolling_avg_roi_pearl_agents(
|
| 500 |
+
agents_trades, agents_mech_requests
|
| 501 |
+
)
|
| 502 |
+
print(two_weeks_avg.head())
|
| 503 |
+
# save in a file
|
| 504 |
+
two_weeks_avg.to_parquet(ROOT_DIR / "two_weeks_avg_roi_pearl_agents.parquet")
|
two_weeks_avg_roi_pearl_agents.parquet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f7dcb86cafacfcf4f1fd0c8ff9dbe2f5fba45efe6725b7900dcefd7d497019f0
|
| 3 |
+
size 3045
|