MISC
This commit is contained in:
@@ -1518,9 +1518,89 @@ class TradingOrchestrator:
|
||||
with open(self.ui_state_file, "w") as f:
|
||||
json.dump(ui_state, f, indent=4)
|
||||
logger.debug(f"UI state saved to {self.ui_state_file}")
|
||||
# Also append a session snapshot for persistence across restarts
|
||||
self._append_session_snapshot()
|
||||
except Exception as e:
|
||||
logger.error(f"Error saving UI state: {e}")
|
||||
|
||||
def _append_session_snapshot(self):
|
||||
"""Append current session metrics to persistent JSON until cleared manually."""
|
||||
try:
|
||||
session_file = os.path.join("data", "session_state.json")
|
||||
os.makedirs(os.path.dirname(session_file), exist_ok=True)
|
||||
|
||||
# Load existing
|
||||
existing = {}
|
||||
if os.path.exists(session_file):
|
||||
try:
|
||||
with open(session_file, "r", encoding="utf-8") as f:
|
||||
existing = json.load(f) or {}
|
||||
except Exception:
|
||||
existing = {}
|
||||
|
||||
# Collect metrics
|
||||
balance = 0.0
|
||||
pnl_total = 0.0
|
||||
closed_trades = []
|
||||
try:
|
||||
if hasattr(self, "trading_executor") and self.trading_executor:
|
||||
balance = float(getattr(self.trading_executor, "account_balance", 0.0) or 0.0)
|
||||
if hasattr(self.trading_executor, "trade_history"):
|
||||
for t in self.trading_executor.trade_history:
|
||||
try:
|
||||
closed_trades.append({
|
||||
"symbol": t.symbol,
|
||||
"side": t.side,
|
||||
"qty": t.quantity,
|
||||
"entry": t.entry_price,
|
||||
"exit": t.exit_price,
|
||||
"pnl": t.pnl,
|
||||
"timestamp": getattr(t, "timestamp", None)
|
||||
})
|
||||
pnl_total += float(t.pnl or 0.0)
|
||||
except Exception:
|
||||
continue
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Models and performance (best-effort)
|
||||
models = {}
|
||||
try:
|
||||
models = {
|
||||
"dqn": {
|
||||
"available": bool(getattr(self, "rl_agent", None)),
|
||||
"last_losses": getattr(getattr(self, "rl_agent", None), "losses", [])[-10:] if getattr(getattr(self, "rl_agent", None), "losses", None) else []
|
||||
},
|
||||
"cnn": {
|
||||
"available": bool(getattr(self, "cnn_model", None))
|
||||
},
|
||||
"cob_rl": {
|
||||
"available": bool(getattr(self, "cob_rl_agent", None))
|
||||
},
|
||||
"decision_fusion": {
|
||||
"available": bool(getattr(self, "decision_model", None))
|
||||
}
|
||||
}
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
snapshot = {
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"balance": balance,
|
||||
"session_pnl": pnl_total,
|
||||
"closed_trades": closed_trades,
|
||||
"models": models
|
||||
}
|
||||
|
||||
if "snapshots" not in existing:
|
||||
existing["snapshots"] = []
|
||||
existing["snapshots"].append(snapshot)
|
||||
|
||||
with open(session_file, "w", encoding="utf-8") as f:
|
||||
json.dump(existing, f, indent=2)
|
||||
except Exception as e:
|
||||
logger.error(f"Error appending session snapshot: {e}")
|
||||
|
||||
def get_model_toggle_state(self, model_name: str) -> Dict[str, bool]:
|
||||
"""Get toggle state for a model"""
|
||||
key = self._normalize_model_name(model_name)
|
||||
|
||||
Reference in New Issue
Block a user