cyberosa
commited on
Commit
·
0d76eae
1
Parent(s):
9cc7b98
adding DAA for all traders
Browse files- app.py +1 -1
- tabs/trader_plots.py +60 -43
app.py
CHANGED
@@ -636,7 +636,7 @@ with demo:
|
|
636 |
gr.Markdown("## Daily active agents by market categories")
|
637 |
with gr.Row():
|
638 |
rolling_avg_plot = plot_rolling_average(
|
639 |
-
active_traders,
|
640 |
)
|
641 |
|
642 |
with gr.TabItem("📉 Markets Kullback–Leibler divergence"):
|
|
|
636 |
gr.Markdown("## Daily active agents by market categories")
|
637 |
with gr.Row():
|
638 |
rolling_avg_plot = plot_rolling_average(
|
639 |
+
active_traders,
|
640 |
)
|
641 |
|
642 |
with gr.TabItem("📉 Markets Kullback–Leibler divergence"):
|
tabs/trader_plots.py
CHANGED
@@ -301,7 +301,9 @@ def plot_total_bet_amount(
|
|
301 |
)
|
302 |
|
303 |
|
304 |
-
def
|
|
|
|
|
305 |
"""Function to get the 7-day rolling average of the number of unique trader_address by market_creator and total"""
|
306 |
# Create a local copy of the dataframe
|
307 |
local_df = active_traders_df.copy()
|
@@ -313,43 +315,66 @@ def get_sevenday_rolling_average(active_traders_df: pd.DataFrame) -> pd.DataFram
|
|
313 |
# Sort the dataframe by date
|
314 |
local_df = local_df.sort_values(by="creation_date")
|
315 |
|
316 |
-
# Group by market_creator and
|
317 |
-
|
318 |
-
local_df.groupby("market_creator")
|
319 |
-
.
|
320 |
-
lambda group: group.set_index("creation_date")
|
321 |
-
.resample("D")["trader_address"]
|
322 |
-
.nunique()
|
323 |
-
.rolling(window=7)
|
324 |
-
.mean()
|
325 |
-
)
|
326 |
.reset_index()
|
327 |
)
|
328 |
-
|
|
|
|
|
|
|
|
|
|
|
329 |
|
330 |
# Calculate the total rolling average across all market_creators
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
.
|
335 |
-
|
336 |
-
.resample("D")["trader_address"]
|
337 |
-
.nunique()
|
338 |
-
.rolling(window=7)
|
339 |
-
.mean()
|
340 |
-
)
|
341 |
.reset_index()
|
342 |
)
|
343 |
-
|
|
|
|
|
|
|
344 |
|
345 |
# Combine both results
|
346 |
combined_rolling_avg = pd.concat(
|
347 |
-
[rolling_avg_by_market,
|
348 |
)
|
349 |
|
350 |
return combined_rolling_avg
|
351 |
|
352 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
353 |
def plot_rolling_average(
|
354 |
active_traders_df: pd.DataFrame,
|
355 |
market_creator: str = None,
|
@@ -357,30 +382,25 @@ def plot_rolling_average(
|
|
357 |
"""Function to plot the rolling average of active traders for the different categories and markets"""
|
358 |
|
359 |
rolling_avg_df = get_sevenday_rolling_average(active_traders_df)
|
360 |
-
|
361 |
if market_creator is not None:
|
362 |
rolling_avg_df = rolling_avg_df.loc[
|
363 |
rolling_avg_df["market_creator"] == market_creator
|
364 |
]
|
365 |
|
366 |
-
#
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
]
|
372 |
-
)
|
373 |
-
# Convert back to string format
|
374 |
-
all_dates = [date.strftime("%b-%d-%Y") for date in all_dates_dt]
|
375 |
-
fig = px.line(
|
376 |
rolling_avg_df,
|
377 |
x="creation_date",
|
378 |
y="rolling_avg_traders",
|
379 |
-
color="market_creator",
|
380 |
-
color_discrete_sequence=color_mapping,
|
381 |
-
category_orders={
|
382 |
-
|
383 |
-
},
|
384 |
)
|
385 |
fig.update_layout(
|
386 |
xaxis_title="Date",
|
@@ -388,9 +408,6 @@ def plot_rolling_average(
|
|
388 |
legend=dict(yanchor="top", y=0.5),
|
389 |
)
|
390 |
|
391 |
-
fig.update_xaxes(tickformat="%b %d")
|
392 |
-
# Update layout to force x-axis category order (hotfix for a sorting issue)
|
393 |
-
fig.update_layout(xaxis={"categoryorder": "array", "categoryarray": all_dates})
|
394 |
return gr.Plot(
|
395 |
value=fig,
|
396 |
)
|
|
|
301 |
)
|
302 |
|
303 |
|
304 |
+
def get_sevenday_rolling_average_by_market_creator(
|
305 |
+
active_traders_df: pd.DataFrame,
|
306 |
+
) -> pd.DataFrame:
|
307 |
"""Function to get the 7-day rolling average of the number of unique trader_address by market_creator and total"""
|
308 |
# Create a local copy of the dataframe
|
309 |
local_df = active_traders_df.copy()
|
|
|
315 |
# Sort the dataframe by date
|
316 |
local_df = local_df.sort_values(by="creation_date")
|
317 |
|
318 |
+
# Group by market_creator and creation_date, count unique traders
|
319 |
+
daily_traders = (
|
320 |
+
local_df.groupby(["market_creator", "creation_date"])["trader_address"]
|
321 |
+
.nunique()
|
|
|
|
|
|
|
|
|
|
|
|
|
322 |
.reset_index()
|
323 |
)
|
324 |
+
|
325 |
+
# Calculate rolling average for each market_creator
|
326 |
+
rolling_avg_by_market = daily_traders.copy()
|
327 |
+
rolling_avg_by_market["rolling_avg_traders"] = rolling_avg_by_market.groupby(
|
328 |
+
"market_creator"
|
329 |
+
)["trader_address"].transform(lambda x: x.rolling(window=7).mean())
|
330 |
|
331 |
# Calculate the total rolling average across all market_creators
|
332 |
+
all_markets = daily_traders.copy()
|
333 |
+
all_markets["market_creator"] = "all"
|
334 |
+
all_markets = (
|
335 |
+
all_markets.groupby(["market_creator", "creation_date"])["trader_address"]
|
336 |
+
.sum()
|
|
|
|
|
|
|
|
|
|
|
337 |
.reset_index()
|
338 |
)
|
339 |
+
|
340 |
+
all_markets["rolling_avg_traders"] = (
|
341 |
+
all_markets["trader_address"].rolling(window=7).mean()
|
342 |
+
)
|
343 |
|
344 |
# Combine both results
|
345 |
combined_rolling_avg = pd.concat(
|
346 |
+
[rolling_avg_by_market, all_markets], ignore_index=True
|
347 |
)
|
348 |
|
349 |
return combined_rolling_avg
|
350 |
|
351 |
|
352 |
+
def get_sevenday_rolling_average(active_traders_df: pd.DataFrame) -> pd.DataFrame:
|
353 |
+
"""Function to get the 7-day rolling average of the number of unique trader_address by market_creator and total"""
|
354 |
+
# Create a local copy of the dataframe
|
355 |
+
local_df = active_traders_df.copy()
|
356 |
+
|
357 |
+
# Convert string dates to datetime
|
358 |
+
local_df["creation_date"] = pd.to_datetime(
|
359 |
+
local_df["creation_date"], format="%b-%d-%Y"
|
360 |
+
)
|
361 |
+
# Sort the dataframe by date
|
362 |
+
local_df = local_df.sort_values(by="creation_date").set_index("creation_date")
|
363 |
+
|
364 |
+
# Group by market_creator and calculate rolling average of unique trader_address
|
365 |
+
rolling_avg_by_market = (
|
366 |
+
local_df.resample("D")["trader_address"]
|
367 |
+
.nunique()
|
368 |
+
.rolling(window=7)
|
369 |
+
.mean()
|
370 |
+
.reset_index()
|
371 |
+
)
|
372 |
+
rolling_avg_by_market.rename(
|
373 |
+
columns={"trader_address": "rolling_avg_traders"}, inplace=True
|
374 |
+
)
|
375 |
+
return rolling_avg_by_market
|
376 |
+
|
377 |
+
|
378 |
def plot_rolling_average(
|
379 |
active_traders_df: pd.DataFrame,
|
380 |
market_creator: str = None,
|
|
|
382 |
"""Function to plot the rolling average of active traders for the different categories and markets"""
|
383 |
|
384 |
rolling_avg_df = get_sevenday_rolling_average(active_traders_df)
|
385 |
+
print(rolling_avg_df.head())
|
386 |
if market_creator is not None:
|
387 |
rolling_avg_df = rolling_avg_df.loc[
|
388 |
rolling_avg_df["market_creator"] == market_creator
|
389 |
]
|
390 |
|
391 |
+
# Ensure 'creation_date' is a column, not an index
|
392 |
+
if "creation_date" not in rolling_avg_df.columns:
|
393 |
+
rolling_avg_df = rolling_avg_df.reset_index()
|
394 |
+
|
395 |
+
fig = px.bar(
|
|
|
|
|
|
|
|
|
|
|
396 |
rolling_avg_df,
|
397 |
x="creation_date",
|
398 |
y="rolling_avg_traders",
|
399 |
+
# color="market_creator",
|
400 |
+
# color_discrete_sequence=color_mapping,
|
401 |
+
# category_orders={
|
402 |
+
# "market_creator": ["pearl", "quickstart", "all"],
|
403 |
+
# },
|
404 |
)
|
405 |
fig.update_layout(
|
406 |
xaxis_title="Date",
|
|
|
408 |
legend=dict(yanchor="top", y=0.5),
|
409 |
)
|
410 |
|
|
|
|
|
|
|
411 |
return gr.Plot(
|
412 |
value=fig,
|
413 |
)
|