|
@@ -332,70 +332,69 @@ class TradeLifecycleManager:
|
|
leverage: Optional[float] = None,
|
|
leverage: Optional[float] = None,
|
|
position_value: Optional[float] = None,
|
|
position_value: Optional[float] = None,
|
|
unrealized_pnl_percentage: Optional[float] = None) -> bool:
|
|
unrealized_pnl_percentage: Optional[float] = None) -> bool:
|
|
- """Update market-related data for an open trade lifecycle."""
|
|
|
|
|
|
+ """Update market data for a trade lifecycle."""
|
|
try:
|
|
try:
|
|
- updates = []
|
|
|
|
|
|
+ # Build dynamic update query based on provided fields
|
|
|
|
+ update_fields = []
|
|
params = []
|
|
params = []
|
|
|
|
|
|
if unrealized_pnl is not None:
|
|
if unrealized_pnl is not None:
|
|
- updates.append("unrealized_pnl = ?")
|
|
|
|
|
|
+ update_fields.append("unrealized_pnl = ?")
|
|
params.append(unrealized_pnl)
|
|
params.append(unrealized_pnl)
|
|
|
|
+
|
|
if mark_price is not None:
|
|
if mark_price is not None:
|
|
- updates.append("mark_price = ?")
|
|
|
|
|
|
+ update_fields.append("mark_price = ?")
|
|
params.append(mark_price)
|
|
params.append(mark_price)
|
|
|
|
+
|
|
if current_position_size is not None:
|
|
if current_position_size is not None:
|
|
- updates.append("current_position_size = ?")
|
|
|
|
|
|
+ update_fields.append("current_position_size = ?")
|
|
params.append(current_position_size)
|
|
params.append(current_position_size)
|
|
|
|
+
|
|
if entry_price is not None:
|
|
if entry_price is not None:
|
|
- updates.append("entry_price = ?")
|
|
|
|
|
|
+ update_fields.append("entry_price = ?")
|
|
params.append(entry_price)
|
|
params.append(entry_price)
|
|
|
|
+
|
|
if liquidation_price is not None:
|
|
if liquidation_price is not None:
|
|
- updates.append("liquidation_price = ?")
|
|
|
|
|
|
+ update_fields.append("liquidation_price = ?")
|
|
params.append(liquidation_price)
|
|
params.append(liquidation_price)
|
|
|
|
+
|
|
if margin_used is not None:
|
|
if margin_used is not None:
|
|
- updates.append("margin_used = ?")
|
|
|
|
|
|
+ update_fields.append("margin_used = ?")
|
|
params.append(margin_used)
|
|
params.append(margin_used)
|
|
|
|
+
|
|
if leverage is not None:
|
|
if leverage is not None:
|
|
- updates.append("leverage = ?")
|
|
|
|
|
|
+ update_fields.append("leverage = ?")
|
|
params.append(leverage)
|
|
params.append(leverage)
|
|
|
|
+
|
|
if position_value is not None:
|
|
if position_value is not None:
|
|
- updates.append("position_value = ?")
|
|
|
|
|
|
+ update_fields.append("position_value = ?")
|
|
params.append(position_value)
|
|
params.append(position_value)
|
|
|
|
+
|
|
if unrealized_pnl_percentage is not None:
|
|
if unrealized_pnl_percentage is not None:
|
|
- # Store ROE as a decimal (e.g., -0.326 for -32.6%)
|
|
|
|
- # We'll handle the conversion to percentage when displaying
|
|
|
|
- updates.append("unrealized_pnl_percentage = ?")
|
|
|
|
|
|
+ update_fields.append("unrealized_pnl_percentage = ?")
|
|
params.append(unrealized_pnl_percentage)
|
|
params.append(unrealized_pnl_percentage)
|
|
-
|
|
|
|
- if not updates:
|
|
|
|
- logger.debug(f"No market data fields provided to update for lifecycle {trade_lifecycle_id}.")
|
|
|
|
- return True
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ if not update_fields:
|
|
|
|
+ logger.warning(f"No fields to update for trade {trade_lifecycle_id}")
|
|
|
|
+ return False
|
|
|
|
+
|
|
|
|
+ # Add timestamp and lifecycle_id
|
|
timestamp = datetime.now(timezone.utc).isoformat()
|
|
timestamp = datetime.now(timezone.utc).isoformat()
|
|
- updates.append("updated_at = ?")
|
|
|
|
|
|
+ update_fields.append("updated_at = ?")
|
|
params.append(timestamp)
|
|
params.append(timestamp)
|
|
-
|
|
|
|
- set_clause = ", ".join(updates)
|
|
|
|
|
|
+ params.append(trade_lifecycle_id)
|
|
|
|
+
|
|
query = f"""
|
|
query = f"""
|
|
- UPDATE trades
|
|
|
|
- SET {set_clause}
|
|
|
|
|
|
+ UPDATE trades
|
|
|
|
+ SET {', '.join(update_fields)}
|
|
WHERE trade_lifecycle_id = ? AND status = 'position_opened'
|
|
WHERE trade_lifecycle_id = ? AND status = 'position_opened'
|
|
"""
|
|
"""
|
|
- params.append(trade_lifecycle_id)
|
|
|
|
|
|
|
|
- cursor = self.db.conn.cursor()
|
|
|
|
- cursor.execute(query, tuple(params))
|
|
|
|
- self.db.conn.commit()
|
|
|
|
- updated_rows = cursor.rowcount
|
|
|
|
-
|
|
|
|
- if updated_rows > 0:
|
|
|
|
- logger.debug(f"💹 Updated market data for lifecycle {trade_lifecycle_id}")
|
|
|
|
- return True
|
|
|
|
- else:
|
|
|
|
- return False
|
|
|
|
-
|
|
|
|
|
|
+ self.db._execute_query(query, params)
|
|
|
|
+ return True
|
|
|
|
+
|
|
except Exception as e:
|
|
except Exception as e:
|
|
- logger.error(f"❌ Error updating market data for trade lifecycle {trade_lifecycle_id}: {e}")
|
|
|
|
|
|
+ logger.error(f"❌ Error updating trade market data: {e}")
|
|
return False
|
|
return False
|
|
|
|
|
|
def get_recent_trades(self, limit: int = 10) -> List[Dict[str, Any]]:
|
|
def get_recent_trades(self, limit: int = 10) -> List[Dict[str, Any]]:
|