|
@@ -409,12 +409,40 @@ class TradingStats:
|
|
|
start_date_obj = datetime.fromisoformat(start_date_iso) if start_date_iso else datetime.now(timezone.utc)
|
|
|
days_active = (datetime.now(timezone.utc) - start_date_obj).days + 1
|
|
|
|
|
|
- # 'last_trade' timestamp could be the last update to token_stats or an open trade
|
|
|
- last_activity_ts = token_stats_summary['overall_last_cycle_closed'] if token_stats_summary else None
|
|
|
- last_open_trade_ts_row = self.db_manager._fetchone_query("SELECT MAX(updated_at) as last_update FROM trades WHERE status = 'position_opened'")
|
|
|
+ # Get last activity timestamp
|
|
|
+ last_activity_ts = None
|
|
|
+ last_activity_query = """
|
|
|
+ SELECT MAX(updated_at) as last_update
|
|
|
+ FROM trades
|
|
|
+ WHERE status IN ('position_opened', 'position_closed')
|
|
|
+ """
|
|
|
+ last_activity_row = self.db_manager._fetchone_query(last_activity_query)
|
|
|
+ if last_activity_row and last_activity_row['last_update']:
|
|
|
+ last_activity_ts = last_activity_row['last_update']
|
|
|
+ # Ensure timezone-aware
|
|
|
+ if isinstance(last_activity_ts, str):
|
|
|
+ last_activity_ts = datetime.fromisoformat(last_activity_ts)
|
|
|
+ if last_activity_ts.tzinfo is None:
|
|
|
+ last_activity_ts = last_activity_ts.replace(tzinfo=timezone.utc)
|
|
|
+
|
|
|
+ # Get last open trade timestamp
|
|
|
+ last_open_trade_query = """
|
|
|
+ SELECT MAX(updated_at) as last_update
|
|
|
+ FROM trades
|
|
|
+ WHERE status = 'position_opened'
|
|
|
+ """
|
|
|
+ last_open_trade_ts_row = self.db_manager._fetchone_query(last_open_trade_query)
|
|
|
if last_open_trade_ts_row and last_open_trade_ts_row['last_update']:
|
|
|
- if not last_activity_ts or datetime.fromisoformat(last_open_trade_ts_row['last_update']) > datetime.fromisoformat(last_activity_ts):
|
|
|
- last_activity_ts = last_open_trade_ts_row['last_update']
|
|
|
+ last_open_trade_ts = last_open_trade_ts_row['last_update']
|
|
|
+ # Ensure timezone-aware
|
|
|
+ if isinstance(last_open_trade_ts, str):
|
|
|
+ last_open_trade_ts = datetime.fromisoformat(last_open_trade_ts)
|
|
|
+ if last_open_trade_ts.tzinfo is None:
|
|
|
+ last_open_trade_ts = last_open_trade_ts.replace(tzinfo=timezone.utc)
|
|
|
+
|
|
|
+ # Now both datetimes are timezone-aware, we can compare them
|
|
|
+ if not last_activity_ts or last_open_trade_ts > last_activity_ts:
|
|
|
+ last_activity_ts = last_open_trade_ts
|
|
|
|
|
|
return {
|
|
|
'total_trades': total_trades_redefined,
|