|
@@ -128,40 +128,28 @@ class RiskCleanupManager:
|
|
|
logger.debug(f"Risk management disabled or STOP_LOSS_PERCENTAGE <= 0 (value: {Config.STOP_LOSS_PERCENTAGE})")
|
|
|
return
|
|
|
|
|
|
- positions = self.market_monitor_cache.cached_positions or []
|
|
|
+ # Use DB positions for risk checks to ensure validated, up-to-date data
|
|
|
+ stats = self.trading_engine.get_stats()
|
|
|
+ positions = stats.get_open_positions() if stats else []
|
|
|
if not positions:
|
|
|
- logger.debug("No positions found in cache for risk management check.")
|
|
|
+ logger.debug("No open positions found in DB for risk management check.")
|
|
|
await self._cleanup_orphaned_stop_losses() # Call within class
|
|
|
return
|
|
|
|
|
|
for position in positions:
|
|
|
try:
|
|
|
symbol = position.get('symbol', '')
|
|
|
- contracts = float(position.get('contracts', 0))
|
|
|
- entry_price = float(position.get('entryPx', 0))
|
|
|
- mark_price = float(position.get('markPx', 0))
|
|
|
- unrealized_pnl = float(position.get('unrealizedPnl', 0))
|
|
|
-
|
|
|
+ contracts = float(position.get('current_position_size', 0))
|
|
|
+ entry_price = float(position.get('entry_price', 0))
|
|
|
+ mark_price = float(position.get('mark_price', 0))
|
|
|
+ unrealized_pnl = float(position.get('unrealized_pnl', 0))
|
|
|
+ roe_percentage = float(position.get('roe_percentage', 0))
|
|
|
+
|
|
|
if contracts == 0 or entry_price <= 0 or mark_price <= 0:
|
|
|
logger.debug(f"Skipping position {symbol}: contracts={contracts}, entry_price={entry_price}, mark_price={mark_price}")
|
|
|
continue
|
|
|
|
|
|
- # Get ROE directly from exchange data
|
|
|
- info_data = position.get('info', {})
|
|
|
- position_info = info_data.get('position', {})
|
|
|
- roe_raw = position_info.get('returnOnEquity')
|
|
|
- if roe_raw is not None:
|
|
|
- try:
|
|
|
- roe_percentage = float(roe_raw) * 100
|
|
|
- logger.debug(f"[RiskMgmt] {symbol}: ROE from exchange: {roe_percentage:+.2f}% (raw: {roe_raw})")
|
|
|
- except (ValueError, TypeError):
|
|
|
- logger.warning(f"Could not parse ROE value: {roe_raw} for {symbol}")
|
|
|
- roe_percentage = 0.0
|
|
|
- else:
|
|
|
- logger.warning(f"No ROE data available from exchange for {symbol}")
|
|
|
- roe_percentage = 0.0
|
|
|
-
|
|
|
- logger.info(f"[RiskMgmt] {symbol}: ROE={roe_percentage:+.2f}%, Threshold=-{Config.STOP_LOSS_PERCENTAGE}% (Trigger: {roe_percentage <= -Config.STOP_LOSS_PERCENTAGE})")
|
|
|
+ logger.debug(f"[RiskMgmt] {symbol}: ROE={roe_percentage:+.2f}%, Threshold=-{Config.STOP_LOSS_PERCENTAGE}% (Trigger: {roe_percentage <= -Config.STOP_LOSS_PERCENTAGE})")
|
|
|
|
|
|
if roe_percentage <= -Config.STOP_LOSS_PERCENTAGE:
|
|
|
token = symbol.split('/')[0] if '/' in symbol else symbol.split(':')[0]
|