Olas-predict-dataset / scripts /market_metrics.py
cyberosa
adding update scripts for the dataset
cd451ea
raw
history blame
2.18 kB
import numpy as np
import pandas as pd
import time
from utils import convert_hex_to_int, ROOT_DIR, TMP_DIR
def determine_market_status(row):
current_answer = row["currentAnswer"]
"""Determine the market status of a trade."""
if (current_answer is np.nan or current_answer is None) and time.time() >= int(
row["openingTimestamp"]
):
return "pending"
if current_answer is np.nan or current_answer is None:
return "open"
if row["fpmm.isPendingArbitration"]:
return "arbitrating"
if row["fpmm.answerFinalizedTimestamp"] and time.time() < int(
row["fpmm.answerFinalizedTimestamp"]
):
return "finalizing"
return "closed"
def compute_market_metrics(all_trades: pd.DataFrame):
print("Preparing dataset")
all_trades.rename(
columns={
"fpmm.currentAnswer": "currentAnswer",
"fpmm.openingTimestamp": "openingTimestamp",
"fpmm.id": "market_id",
},
inplace=True,
)
all_trades["currentAnswer"] = all_trades["currentAnswer"].apply(
lambda x: convert_hex_to_int(x)
)
all_trades["market_status"] = all_trades.apply(
lambda x: determine_market_status(x), axis=1
)
closed_trades = all_trades.loc[all_trades["market_status"] == "closed"]
print("Computing metrics")
nr_trades = (
closed_trades.groupby("market_id")["id"].count().reset_index(name="nr_trades")
)
total_traders = (
closed_trades.groupby("market_id")["trader_address"]
.nunique()
.reset_index(name="total_traders")
)
final_dataset = nr_trades.merge(total_traders, on="market_id")
markets = closed_trades[
["market_id", "title", "market_creator", "openingTimestamp"]
]
markets.drop_duplicates("market_id", inplace=True)
market_metrics = markets.merge(final_dataset, on="market_id")
print("Saving dataset")
market_metrics.to_parquet(ROOT_DIR / "closed_market_metrics.parquet", index=False)
print(market_metrics.head())
if __name__ == "__main__":
all_trades = pd.read_parquet(TMP_DIR / "fpmmTrades.parquet")
compute_market_metrics(all_trades)