import gradio as gr import pandas as pd HEIGHT=600 WIDTH=1000 def prepare_trades(trades_df: pd.DataFrame) -> pd.DataFrame: """Prepares the trades data for analysis.""" trades_df['creation_timestamp'] = pd.to_datetime(trades_df['creation_timestamp']) trades_df['creation_timestamp'] = trades_df['creation_timestamp'].dt.tz_convert('UTC') trades_df['month_year'] = trades_df['creation_timestamp'].dt.to_period('M').astype(str) trades_df['month_year_week'] = trades_df['creation_timestamp'].dt.to_period('W').astype(str) trades_df['winning_trade'] = trades_df['winning_trade'].astype(int) return trades_df def get_overall_trades(trades_df: pd.DataFrame) -> pd.DataFrame: """Gets the overall trades data for the given tools and calculates the winning percentage.""" trades_count = trades_df.groupby('month_year_week').size().reset_index() trades_count.columns = trades_count.columns.astype(str) trades_count.rename(columns={'0': 'trades'}, inplace=True) return trades_count def get_overall_winning_trades(trades_df: pd.DataFrame) -> pd.DataFrame: """Gets the overall winning trades data for the given tools and calculates the winning percentage.""" winning_trades = trades_df.groupby(['month_year_week'])['winning_trade'].sum() / trades_df.groupby(['month_year_week'])['winning_trade'].count() * 100 # winning_trades is a series, give it a dataframe winning_trades = winning_trades.reset_index() winning_trades.columns = winning_trades.columns.astype(str) winning_trades.columns = ['month_year_week', 'winning_trade'] return winning_trades def plot_trade_details(trade_detail: str, trades_df: pd.DataFrame) -> gr.LinePlot: """Plots the trade details for the given trade detail.""" if trade_detail == "mech calls": # this is to filter out the data before 2023-09-01 trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"] trades_filtered = trades_filtered.groupby("month_year_week")["num_mech_calls"].quantile([0.25, 0.5, 0.75]).unstack() trades_filtered.columns = trades_filtered.columns.astype(str) trades_filtered.reset_index(inplace=True) trades_filtered.columns = [ "month_year_week", "25th_percentile", "50th_percentile", "75th_percentile" ] # reformat the data as percentile, date, value trades_filtered = trades_filtered.melt(id_vars=["month_year_week"], var_name="percentile", value_name="mech_calls") return gr.LinePlot( value=trades_filtered, x="month_year_week", y="mech_calls", color="percentile", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "percentile", "mech_calls"], height=HEIGHT, width=WIDTH ) if trade_detail == "collateral amount": trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"] trades_filtered = trades_filtered.groupby("month_year_week")["collateral_amount"].quantile([0.25, 0.5, 0.75]).unstack() trades_filtered.columns = trades_filtered.columns.astype(str) trades_filtered.reset_index(inplace=True) trades_filtered.columns = [ "month_year_week", "25th_percentile", "50th_percentile", "75th_percentile" ] # reformat the data as percentile, date, value trades_filtered = trades_filtered.melt(id_vars=["month_year_week"], var_name="percentile", value_name="collateral_amount") return gr.LinePlot( value=trades_filtered, x="month_year_week", y="collateral_amount", color="percentile", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "percentile", "collateral_amount"], height=HEIGHT, width=WIDTH ) if trade_detail == "earnings": trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"] trades_filtered = trades_filtered.groupby("month_year_week")["earnings"].quantile([0.25, 0.5, 0.75]).unstack() trades_filtered.columns = trades_filtered.columns.astype(str) trades_filtered.reset_index(inplace=True) trades_filtered.columns = [ "month_year_week", "25th_percentile", "50th_percentile", "75th_percentile" ] # reformat the data as percentile, date, value trades_filtered = trades_filtered.melt(id_vars=["month_year_week"], var_name="percentile", value_name="earnings") return gr.LinePlot( value=trades_filtered, x="month_year_week", y="earnings", color="percentile", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "percentile", "earnings"], height=HEIGHT, width=WIDTH ) if trade_detail == "net earnings": trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"] trades_filtered = trades_filtered.groupby("month_year_week")["net_earnings"].quantile([0.25, 0.5, 0.75]).unstack() trades_filtered.columns = trades_filtered.columns.astype(str) trades_filtered.reset_index(inplace=True) trades_filtered.columns = [ "month_year_week", "25th_percentile", "50th_percentile", "75th_percentile" ] # reformat the data as percentile, date, value trades_filtered = trades_filtered.melt(id_vars=["month_year_week"], var_name="percentile", value_name="net_earnings") return gr.LinePlot( value=trades_filtered, x="month_year_week", y="net_earnings", color="percentile", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "percentile", "net_earnings"], height=HEIGHT, width=WIDTH ) if trade_detail == "ROI": trades_filtered = trades_df[trades_df["creation_timestamp"] >"2023-09-01"] trades_filtered = trades_filtered.groupby("month_year_week")["roi"].quantile([0.25, 0.5, 0.75]).unstack() trades_filtered.columns = trades_filtered.columns.astype(str) trades_filtered.reset_index(inplace=True) trades_filtered.columns = [ "month_year_week", "25th_percentile", "50th_percentile", "75th_percentile" ] # reformat the data as percentile, date, value trades_filtered = trades_filtered.melt(id_vars=["month_year_week"], var_name="percentile", value_name="ROI") return gr.LinePlot( value=trades_filtered, x="month_year_week", y="ROI", color="percentile", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "percentile", "ROI"], height=HEIGHT, width=WIDTH ) def plot_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot: """Plots the trades data for the given tools and calculates the winning percentage.""" return gr.BarPlot( value=trades_df, x="month_year_week", y="trades", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "trades"], height=HEIGHT, width=WIDTH ) def plot_winning_trades_by_week(trades_df: pd.DataFrame) -> gr.BarPlot: """Plots the winning trades data for the given tools and calculates the winning percentage.""" return gr.BarPlot( value=trades_df, x="month_year_week", y="winning_trade", show_label=True, interactive=True, show_actions_button=True, tooltip=["month_year_week", "winning_trade"], height=HEIGHT, width=WIDTH )