Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import json
|
|
| 7 |
import firebase_admin
|
| 8 |
from firebase_admin import credentials, db
|
| 9 |
from datetime import datetime
|
|
|
|
| 10 |
|
| 11 |
# --- Configuration ---
|
| 12 |
# The Firebase DB reference name must match the one used in your agent (App3.txt)
|
|
@@ -121,36 +122,47 @@ def create_equity_curve(df):
|
|
| 121 |
return fig
|
| 122 |
|
| 123 |
def create_pnl_analytics_charts(df):
|
| 124 |
-
"""Creates bar charts for PnL breakdown by strategy and regime."""
|
| 125 |
if df.empty:
|
| 126 |
no_data_fig = px.bar(title="PnL by Strategy (No Data)").update_layout(template="plotly_dark")
|
| 127 |
return no_data_fig, no_data_fig
|
| 128 |
|
| 129 |
-
#
|
|
|
|
|
|
|
|
|
|
| 130 |
pnl_by_strategy = df.groupby('strategy_chosen')['pnl'].sum().reset_index()
|
|
|
|
|
|
|
|
|
|
| 131 |
fig_strategy = px.bar(
|
| 132 |
pnl_by_strategy,
|
| 133 |
x='strategy_chosen',
|
| 134 |
y='pnl',
|
| 135 |
-
color='
|
| 136 |
-
|
| 137 |
title="PnL by Strategy",
|
| 138 |
labels={'strategy_chosen': 'Strategy', 'pnl': 'Total PnL'}
|
| 139 |
)
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
-
# PnL by Market Regime
|
| 143 |
pnl_by_regime = df.groupby('market_regime')['pnl'].sum().reset_index()
|
|
|
|
|
|
|
|
|
|
| 144 |
fig_regime = px.bar(
|
| 145 |
pnl_by_regime,
|
| 146 |
x='market_regime',
|
| 147 |
y='pnl',
|
| 148 |
-
color='
|
| 149 |
-
|
| 150 |
title="PnL by Market Regime",
|
| 151 |
labels={'market_regime': 'Market Regime', 'pnl': 'Total PnL'}
|
| 152 |
)
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
return fig_strategy, fig_regime
|
| 156 |
|
|
@@ -192,7 +204,7 @@ def update_dashboard():
|
|
| 192 |
|
| 193 |
return (kpis["total_pnl"], kpis["win_rate"], kpis["total_closed_trades"], kpis["average_pnl"],
|
| 194 |
equity_plot,
|
| 195 |
-
|
| 196 |
closed_trades_display.iloc[::-1], # Show newest first
|
| 197 |
strategy_plot,
|
| 198 |
regime_plot,
|
|
@@ -250,10 +262,6 @@ with gr.Blocks(theme=gr.themes.Glass(), title="Agent Analyst Hub") as demo:
|
|
| 250 |
demo.load(fn=update_dashboard, inputs=[], outputs=outputs_list)
|
| 251 |
|
| 252 |
if __name__ == "__main__":
|
| 253 |
-
# For local testing, you can set environment variables like this:
|
| 254 |
-
# os.environ['FIRESTORE_SA_KEY'] = '{"type": "service_account", ...}'
|
| 255 |
-
# os.environ['FIREBASE_DB_URL'] = 'https://your-project-id.firebaseio.com/'
|
| 256 |
-
|
| 257 |
# Check if secrets are present before launching
|
| 258 |
if not all([os.environ.get(k) for k in ['FIRESTORE_SA_KEY', 'FIREBASE_DB_URL']]):
|
| 259 |
print("\nWARNING: Firebase secrets are not set as environment variables.")
|
|
|
|
| 7 |
import firebase_admin
|
| 8 |
from firebase_admin import credentials, db
|
| 9 |
from datetime import datetime
|
| 10 |
+
import numpy as np # Added for color logic
|
| 11 |
|
| 12 |
# --- Configuration ---
|
| 13 |
# The Firebase DB reference name must match the one used in your agent (App3.txt)
|
|
|
|
| 122 |
return fig
|
| 123 |
|
| 124 |
def create_pnl_analytics_charts(df):
|
| 125 |
+
"""Creates bar charts for PnL breakdown by strategy and regime with distinct profit/loss colors."""
|
| 126 |
if df.empty:
|
| 127 |
no_data_fig = px.bar(title="PnL by Strategy (No Data)").update_layout(template="plotly_dark")
|
| 128 |
return no_data_fig, no_data_fig
|
| 129 |
|
| 130 |
+
# Define the discrete colors for profit (green) and loss (red)
|
| 131 |
+
color_map = {'Profit': 'darkgreen', 'Loss': 'darkred'}
|
| 132 |
+
|
| 133 |
+
# --- PnL by Strategy Chart ---
|
| 134 |
pnl_by_strategy = df.groupby('strategy_chosen')['pnl'].sum().reset_index()
|
| 135 |
+
# Create a new column to categorize as 'Profit' or 'Loss'
|
| 136 |
+
pnl_by_strategy['Outcome'] = np.where(pnl_by_strategy['pnl'] >= 0, 'Profit', 'Loss')
|
| 137 |
+
|
| 138 |
fig_strategy = px.bar(
|
| 139 |
pnl_by_strategy,
|
| 140 |
x='strategy_chosen',
|
| 141 |
y='pnl',
|
| 142 |
+
color='Outcome', # Color bars based on the 'Outcome' column
|
| 143 |
+
color_discrete_map=color_map, # Apply our specific green/red colors
|
| 144 |
title="PnL by Strategy",
|
| 145 |
labels={'strategy_chosen': 'Strategy', 'pnl': 'Total PnL'}
|
| 146 |
)
|
| 147 |
+
# Clean up layout: dark theme, centered title, no redundant legend
|
| 148 |
+
fig_strategy.update_layout(template="plotly_dark", title_x=0.5, font=dict(color="white"), showlegend=False)
|
| 149 |
|
| 150 |
+
# --- PnL by Market Regime Chart ---
|
| 151 |
pnl_by_regime = df.groupby('market_regime')['pnl'].sum().reset_index()
|
| 152 |
+
# Create the same 'Outcome' column for this chart
|
| 153 |
+
pnl_by_regime['Outcome'] = np.where(pnl_by_regime['pnl'] >= 0, 'Profit', 'Loss')
|
| 154 |
+
|
| 155 |
fig_regime = px.bar(
|
| 156 |
pnl_by_regime,
|
| 157 |
x='market_regime',
|
| 158 |
y='pnl',
|
| 159 |
+
color='Outcome', # Color bars based on the 'Outcome' column
|
| 160 |
+
color_discrete_map=color_map, # Apply our specific green/red colors
|
| 161 |
title="PnL by Market Regime",
|
| 162 |
labels={'market_regime': 'Market Regime', 'pnl': 'Total PnL'}
|
| 163 |
)
|
| 164 |
+
# Clean up layout
|
| 165 |
+
fig_regime.update_layout(template="plotly_dark", title_x=0.5, font=dict(color="white"), showlegend=False)
|
| 166 |
|
| 167 |
return fig_strategy, fig_regime
|
| 168 |
|
|
|
|
| 204 |
|
| 205 |
return (kpis["total_pnl"], kpis["win_rate"], kpis["total_closed_trades"], kpis["average_pnl"],
|
| 206 |
equity_plot,
|
| 207 |
+
open_ trades_display.iloc[::-1], # Show newest first
|
| 208 |
closed_trades_display.iloc[::-1], # Show newest first
|
| 209 |
strategy_plot,
|
| 210 |
regime_plot,
|
|
|
|
| 262 |
demo.load(fn=update_dashboard, inputs=[], outputs=outputs_list)
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
# Check if secrets are present before launching
|
| 266 |
if not all([os.environ.get(k) for k in ['FIRESTORE_SA_KEY', 'FIREBASE_DB_URL']]):
|
| 267 |
print("\nWARNING: Firebase secrets are not set as environment variables.")
|