Spaces:
Runtime error
Runtime error
cyberosa
commited on
Commit
Β·
dc98cf9
1
Parent(s):
ba84b53
updating weekly data
Browse files- app.py +7 -1
- tabs/trader_plots.py +192 -0
app.py
CHANGED
|
@@ -12,12 +12,12 @@ from scripts.metrics import (
|
|
| 12 |
)
|
| 13 |
from tabs.trader_plots import (
|
| 14 |
plot_trader_metrics_by_market_creator,
|
| 15 |
-
plot_trader_metrics_by_trader_type,
|
| 16 |
default_trader_metric,
|
| 17 |
trader_metric_choices,
|
| 18 |
get_metrics_text,
|
| 19 |
plot_winning_metric_per_trader,
|
| 20 |
get_interpretation_text,
|
|
|
|
| 21 |
)
|
| 22 |
|
| 23 |
from tabs.market_plots import (
|
|
@@ -157,6 +157,12 @@ with demo:
|
|
| 157 |
inputs=trader_details_selector,
|
| 158 |
outputs=trader_markets_plot,
|
| 159 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
with gr.TabItem("πClosed Markets KullbackβLeibler divergence"):
|
| 162 |
with gr.Row():
|
|
|
|
| 12 |
)
|
| 13 |
from tabs.trader_plots import (
|
| 14 |
plot_trader_metrics_by_market_creator,
|
|
|
|
| 15 |
default_trader_metric,
|
| 16 |
trader_metric_choices,
|
| 17 |
get_metrics_text,
|
| 18 |
plot_winning_metric_per_trader,
|
| 19 |
get_interpretation_text,
|
| 20 |
+
plot_median_roi_by_creation_date,
|
| 21 |
)
|
| 22 |
|
| 23 |
from tabs.market_plots import (
|
|
|
|
| 157 |
inputs=trader_details_selector,
|
| 158 |
outputs=trader_markets_plot,
|
| 159 |
)
|
| 160 |
+
# with gr.Row():
|
| 161 |
+
# gr.Markdown("# Daily Median ROI by market creator")
|
| 162 |
+
# with gr.Row():
|
| 163 |
+
# daily_median = plot_median_roi_by_creation_date(
|
| 164 |
+
# traders_df=trader_agents_data
|
| 165 |
+
# )
|
| 166 |
|
| 167 |
with gr.TabItem("πClosed Markets KullbackβLeibler divergence"):
|
| 168 |
with gr.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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
def plot_trader_metrics_by_trader_type(metric_name: str, traders_df: pd.DataFrame):
|
| 94 |
"""Plots the weekly trader metrics."""
|
| 95 |
|
|
|
|
| 85 |
)
|
| 86 |
fig.update_xaxes(tickformat="%b %d\n%Y")
|
| 87 |
|
| 88 |
+
# # Add a vertical dashed line for the "multibet release"
|
| 89 |
+
# release_week = "Sep-29"
|
| 90 |
+
# # Extract unique categories to determine the position
|
| 91 |
+
# categories = traders_filtered["month_year_week"].unique()
|
| 92 |
+
|
| 93 |
+
# if release_week in categories:
|
| 94 |
+
# # Determine the numerical position of the release_week
|
| 95 |
+
# x_position = list(categories).index(release_week)
|
| 96 |
+
|
| 97 |
+
# # Add a vertical dashed line using add_shape
|
| 98 |
+
# fig.add_shape(
|
| 99 |
+
# type="line",
|
| 100 |
+
# x0=x_position,
|
| 101 |
+
# x1=x_position,
|
| 102 |
+
# y0=0,
|
| 103 |
+
# y1=1,
|
| 104 |
+
# xref="x",
|
| 105 |
+
# yref="paper",
|
| 106 |
+
# line=dict(color="darkred", dash="dash"),
|
| 107 |
+
# )
|
| 108 |
+
|
| 109 |
+
# # Add an annotation for the vertical line
|
| 110 |
+
# fig.add_annotation(
|
| 111 |
+
# x=x_position,
|
| 112 |
+
# y=0.8,
|
| 113 |
+
# xref="x",
|
| 114 |
+
# yref="paper",
|
| 115 |
+
# text="multibet release",
|
| 116 |
+
# showarrow=True,
|
| 117 |
+
# arrowhead=2,
|
| 118 |
+
# ax=0,
|
| 119 |
+
# ay=-40,
|
| 120 |
+
# font=dict(color="darkred"),
|
| 121 |
+
# bgcolor="gray",
|
| 122 |
+
# bordercolor="darkred",
|
| 123 |
+
# borderwidth=1,
|
| 124 |
+
# borderpad=4,
|
| 125 |
+
# align="center",
|
| 126 |
+
# )
|
| 127 |
+
# fig.add_vline(
|
| 128 |
+
# x=release_week, # The specific week for the release
|
| 129 |
+
# line_dash="dash", # Dashed line style
|
| 130 |
+
# line_color="darkred", # Color of the vertical line
|
| 131 |
+
# # annotation_text="multibet release",
|
| 132 |
+
# # annotation_text="multibet release", # Label for the vertical line
|
| 133 |
+
# # annotation_position="top", # Position of the annotation
|
| 134 |
+
# # annotation=dict(
|
| 135 |
+
# # bgcolor="white", # Background color for better readability
|
| 136 |
+
# # font_size=12,
|
| 137 |
+
# # font_color="red",
|
| 138 |
+
# # ),
|
| 139 |
+
# )
|
| 140 |
+
|
| 141 |
return gr.Plot(
|
| 142 |
value=fig,
|
| 143 |
)
|
| 144 |
|
| 145 |
|
| 146 |
+
def plot_median_roi_by_creation_date(traders_df: pd.DataFrame) -> gr.Plot:
|
| 147 |
+
traders_df["creation_date"] = traders_df["creation_timestamp"].dt.date
|
| 148 |
+
|
| 149 |
+
traders_all = traders_df.copy(deep=True)
|
| 150 |
+
traders_all["market_creator"] = "all"
|
| 151 |
+
|
| 152 |
+
# merging both dataframes
|
| 153 |
+
final_traders = pd.concat([traders_all, traders_df], ignore_index=True)
|
| 154 |
+
final_traders = final_traders.sort_values(by="creation_date", ascending=True)
|
| 155 |
+
roi_daily_metrics = (
|
| 156 |
+
final_traders.groupby(
|
| 157 |
+
["creation_date", "market_creator", "trader_address"], sort=False
|
| 158 |
+
)
|
| 159 |
+
.agg(
|
| 160 |
+
median_roi=("roi", "median"),
|
| 161 |
+
mean_roi=("roi", "mean"),
|
| 162 |
+
total_trades=("roi", "count"),
|
| 163 |
+
)
|
| 164 |
+
.reset_index()
|
| 165 |
+
)
|
| 166 |
+
# Create the scatter plot with facets for each market_creator
|
| 167 |
+
fig = px.scatter(
|
| 168 |
+
roi_daily_metrics,
|
| 169 |
+
x="creation_date",
|
| 170 |
+
y="median_roi",
|
| 171 |
+
facet_col="market_creator",
|
| 172 |
+
color="market_creator",
|
| 173 |
+
color_discrete_map={
|
| 174 |
+
"pearl": "purple",
|
| 175 |
+
"quickstart": "goldenrod",
|
| 176 |
+
"all": "darkgreen",
|
| 177 |
+
},
|
| 178 |
+
title="Median ROI Over Time by Market Creator",
|
| 179 |
+
labels={
|
| 180 |
+
"creation_date": "Creation Date",
|
| 181 |
+
"median_roi": "Median ROI (%)",
|
| 182 |
+
"market_creator": "Market Creator",
|
| 183 |
+
},
|
| 184 |
+
hover_data={
|
| 185 |
+
"creation_date": "|%B %d, %Y", # Custom date format in hover
|
| 186 |
+
"median_roi": True,
|
| 187 |
+
"mean_roi": True,
|
| 188 |
+
"total_trades": True,
|
| 189 |
+
},
|
| 190 |
+
category_orders={"market_creator": ["pearl", "quickstart", "all"]},
|
| 191 |
+
# trendline=None, # Ensure no trendlines are added
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Customize the layout for better aesthetics
|
| 195 |
+
fig.update_layout(
|
| 196 |
+
template="plotly_white",
|
| 197 |
+
hovermode="closest",
|
| 198 |
+
showlegend=False, # Disable the legend as each facet has its own context
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
# Update each subplot's x-axis to share the same range
|
| 202 |
+
fig.update_xaxes(matches="x") # Link x-axes across facets
|
| 203 |
+
fig.update_yaxes(matches="y") # Link y-axes across facets
|
| 204 |
+
|
| 205 |
+
# Add a vertical dashed line in dark red at the specified date
|
| 206 |
+
vline_date = "2024-09-29"
|
| 207 |
+
vline_datetime = pd.to_datetime(vline_date, format="%Y-%m-%d")
|
| 208 |
+
fig.add_vline(
|
| 209 |
+
x=vline_datetime,
|
| 210 |
+
line_dash="dash",
|
| 211 |
+
line_color="darkred",
|
| 212 |
+
)
|
| 213 |
+
return gr.Plot(
|
| 214 |
+
value=fig,
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
import plotly.express as px
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
def create_median_roi_plot(roi_daily_metrics):
|
| 222 |
+
"""
|
| 223 |
+
Creates a Plotly scatter plot for median ROI over time, colored by market_creator.
|
| 224 |
+
|
| 225 |
+
Parameters:
|
| 226 |
+
- roi_daily_metrics (pd.DataFrame): Aggregated ROI metrics with columns:
|
| 227 |
+
['creation_date', 'market_creator', 'trader_address', 'median_roi', 'mean_roi', 'total_trades']
|
| 228 |
+
|
| 229 |
+
Returns:
|
| 230 |
+
- fig (plotly.graph_objs._figure.Figure): The Plotly figure object.
|
| 231 |
+
"""
|
| 232 |
+
# Ensure 'creation_date' is in datetime format
|
| 233 |
+
roi_daily_metrics["creation_date"] = pd.to_datetime(
|
| 234 |
+
roi_daily_metrics["creation_date"]
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
# Create the line plot with scatter markers
|
| 238 |
+
fig = px.line(
|
| 239 |
+
roi_daily_metrics,
|
| 240 |
+
x="creation_date",
|
| 241 |
+
y="median_roi",
|
| 242 |
+
color="market_creator",
|
| 243 |
+
markers=True, # Add markers to lines
|
| 244 |
+
title="Median ROI Over Time by Market Creator",
|
| 245 |
+
labels={
|
| 246 |
+
"creation_date": "Creation Date",
|
| 247 |
+
"median_roi": "Median ROI (%)",
|
| 248 |
+
"market_creator": "Market Creator",
|
| 249 |
+
},
|
| 250 |
+
hover_data={
|
| 251 |
+
"creation_date": "|%B %d, %Y", # Custom date format in hover
|
| 252 |
+
"median_roi": True,
|
| 253 |
+
"mean_roi": True,
|
| 254 |
+
"total_trades": True,
|
| 255 |
+
},
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# Customize the layout for better aesthetics
|
| 259 |
+
fig.update_layout(
|
| 260 |
+
xaxis_title="Creation Date",
|
| 261 |
+
yaxis_title="Median ROI (%)",
|
| 262 |
+
legend_title="Market Creator",
|
| 263 |
+
template="plotly_white",
|
| 264 |
+
hovermode="x unified",
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# Optional: Add vertical lines for specific events (e.g., "multibet release")
|
| 268 |
+
# Example:
|
| 269 |
+
# fig.add_vline(
|
| 270 |
+
# x=pd.to_datetime("2023-01-02"),
|
| 271 |
+
# line_dash="dash",
|
| 272 |
+
# line_color="red",
|
| 273 |
+
# annotation_text="Multibet Release",
|
| 274 |
+
# annotation_position="top left",
|
| 275 |
+
# annotation=dict(
|
| 276 |
+
# bgcolor="white",
|
| 277 |
+
# font_size=12,
|
| 278 |
+
# font_color="red"
|
| 279 |
+
# )
|
| 280 |
+
# )
|
| 281 |
+
|
| 282 |
+
return fig
|
| 283 |
+
|
| 284 |
+
|
| 285 |
def plot_trader_metrics_by_trader_type(metric_name: str, traders_df: pd.DataFrame):
|
| 286 |
"""Plots the weekly trader metrics."""
|
| 287 |
|