|
@@ -73,14 +73,17 @@ class AggregationManager:
|
|
except Exception:
|
|
except Exception:
|
|
duration_seconds = 0
|
|
duration_seconds = 0
|
|
|
|
|
|
|
|
+ # Calculate ROE percentage
|
|
|
|
+ roe_percentage = (realized_pnl / entry_value * 100) if entry_value > 0 else 0.0
|
|
|
|
+
|
|
# Update token_stats
|
|
# Update token_stats
|
|
token_upsert_query = """
|
|
token_upsert_query = """
|
|
INSERT INTO token_stats (
|
|
INSERT INTO token_stats (
|
|
token, total_realized_pnl, total_completed_cycles, winning_cycles, losing_cycles,
|
|
token, total_realized_pnl, total_completed_cycles, winning_cycles, losing_cycles,
|
|
total_entry_volume, total_exit_volume, sum_of_winning_pnl, sum_of_losing_pnl,
|
|
total_entry_volume, total_exit_volume, sum_of_winning_pnl, sum_of_losing_pnl,
|
|
largest_winning_cycle_pnl, largest_losing_cycle_pnl, largest_winning_cycle_entry_volume, largest_losing_cycle_entry_volume,
|
|
largest_winning_cycle_pnl, largest_losing_cycle_pnl, largest_winning_cycle_entry_volume, largest_losing_cycle_entry_volume,
|
|
- first_cycle_closed_at, last_cycle_closed_at, total_duration_seconds, updated_at
|
|
|
|
- ) VALUES (?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
|
|
+ first_cycle_closed_at, last_cycle_closed_at, total_duration_seconds, roe_percentage, updated_at
|
|
|
|
+ ) VALUES (?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
ON CONFLICT(token) DO UPDATE SET
|
|
ON CONFLICT(token) DO UPDATE SET
|
|
total_realized_pnl = total_realized_pnl + excluded.total_realized_pnl,
|
|
total_realized_pnl = total_realized_pnl + excluded.total_realized_pnl,
|
|
total_completed_cycles = total_completed_cycles + 1,
|
|
total_completed_cycles = total_completed_cycles + 1,
|
|
@@ -109,6 +112,7 @@ class AggregationManager:
|
|
first_cycle_closed_at = MIN(first_cycle_closed_at, excluded.first_cycle_closed_at),
|
|
first_cycle_closed_at = MIN(first_cycle_closed_at, excluded.first_cycle_closed_at),
|
|
last_cycle_closed_at = MAX(last_cycle_closed_at, excluded.last_cycle_closed_at),
|
|
last_cycle_closed_at = MAX(last_cycle_closed_at, excluded.last_cycle_closed_at),
|
|
total_duration_seconds = total_duration_seconds + excluded.total_duration_seconds,
|
|
total_duration_seconds = total_duration_seconds + excluded.total_duration_seconds,
|
|
|
|
+ roe_percentage = excluded.roe_percentage,
|
|
updated_at = excluded.updated_at
|
|
updated_at = excluded.updated_at
|
|
"""
|
|
"""
|
|
is_win = 1 if realized_pnl > 0 else 0
|
|
is_win = 1 if realized_pnl > 0 else 0
|
|
@@ -124,7 +128,7 @@ class AggregationManager:
|
|
token, realized_pnl, is_win, is_loss, entry_value, exit_value,
|
|
token, realized_pnl, is_win, is_loss, entry_value, exit_value,
|
|
win_pnl_contrib, loss_pnl_contrib, win_pnl_contrib, loss_pnl_contrib,
|
|
win_pnl_contrib, loss_pnl_contrib, win_pnl_contrib, loss_pnl_contrib,
|
|
largest_win_entry_volume, largest_loss_entry_volume,
|
|
largest_win_entry_volume, largest_loss_entry_volume,
|
|
- closed_at_str, closed_at_str, duration_seconds, now_iso
|
|
|
|
|
|
+ closed_at_str, closed_at_str, duration_seconds, roe_percentage, now_iso
|
|
))
|
|
))
|
|
|
|
|
|
# Update daily_aggregated_stats
|
|
# Update daily_aggregated_stats
|