Olas-predict-dataset / scripts /mech_calls_file_generator.py
cyberosa
updating scripts and files. New daily mech requests
23d3748
raw
history blame
3.46 kB
import pandas as pd
try:
from utils import ROOT_DIR, TMP_DIR
except ImportError:
from scripts.utils import ROOT_DIR, TMP_DIR
from datetime import datetime, timezone
from tqdm import tqdm
def transform_to_datetime(x):
return datetime.fromtimestamp(int(x), tz=timezone.utc)
def compute_weekly_total_mech_calls(
trader: str, week: str, weekly_trades: pd.DataFrame, weekly_tools: pd.DataFrame
) -> dict:
weekly_total_mech_calls_dict = {}
weekly_total_mech_calls_dict["trader_address"] = trader
weekly_total_mech_calls_dict["month_year_week"] = week
weekly_total_mech_calls_dict["total_trades"] = len(weekly_trades)
weekly_total_mech_calls_dict["total_mech_calls"] = len(weekly_tools)
return weekly_total_mech_calls_dict
def compute_total_mech_calls():
"""Function to compute the total number of mech calls for all traders and all markets
at a weekly level"""
try:
print("Reading tools file")
tools = pd.read_parquet(TMP_DIR / "tools.parquet")
tools["request_time"] = pd.to_datetime(tools["request_time"], utc=True)
tools["request_date"] = tools["request_time"].dt.date
tools = tools.sort_values(by="request_time", ascending=True)
tools["month_year_week"] = (
tools["request_time"]
.dt.to_period("W")
.dt.start_time.dt.strftime("%b-%d-%Y")
)
except Exception as e:
print(f"Error updating the invalid trades parquet {e}")
print("Reading trades weekly info file")
fpmmTrades = pd.read_parquet(TMP_DIR / "fpmmTrades.parquet")
try:
fpmmTrades["creationTimestamp"] = fpmmTrades["creationTimestamp"].apply(
lambda x: transform_to_datetime(x)
)
except Exception as e:
print(f"Transformation not needed")
fpmmTrades["creation_timestamp"] = pd.to_datetime(fpmmTrades["creationTimestamp"])
fpmmTrades["creation_date"] = fpmmTrades["creation_timestamp"].dt.date
fpmmTrades = fpmmTrades.sort_values(by="creation_timestamp", ascending=True)
fpmmTrades["month_year_week"] = (
fpmmTrades["creation_timestamp"]
.dt.to_period("W")
.dt.start_time.dt.strftime("%b-%d-%Y")
)
nr_traders = len(fpmmTrades["trader_address"].unique())
all_mech_calls = []
for trader in tqdm(
fpmmTrades["trader_address"].unique(),
total=nr_traders,
desc="creating weekly mech calls dataframe",
):
# compute the mech calls estimations for each trader
all_trades = fpmmTrades[fpmmTrades["trader_address"] == trader]
all_tools = tools[tools["trader_address"] == trader]
weeks = fpmmTrades.month_year_week.unique()
for week in weeks:
weekly_trades = all_trades.loc[all_trades["month_year_week"] == week]
weekly_tools = all_tools.loc[all_tools["month_year_week"] == week]
weekly_mech_calls_dict = compute_weekly_total_mech_calls(
trader, week, weekly_trades, weekly_tools
)
all_mech_calls.append(weekly_mech_calls_dict)
all_mech_calls_df: pd.DataFrame = pd.DataFrame.from_dict(
all_mech_calls, orient="columns"
)
print("Saving weekly_mech_calls.parquet file")
print(all_mech_calls_df.total_mech_calls.describe())
all_mech_calls_df.to_parquet(ROOT_DIR / "weekly_mech_calls.parquet", index=False)
if __name__ == "__main__":
compute_total_mech_calls()