|
@@ -79,7 +79,7 @@ class RiskCleanupManager:
|
|
active_position_exists = False
|
|
active_position_exists = False
|
|
cached_positions = self.market_monitor_cache.cached_positions or []
|
|
cached_positions = self.market_monitor_cache.cached_positions or []
|
|
for pos in cached_positions:
|
|
for pos in cached_positions:
|
|
- if pos.get('symbol') == symbol and abs(float(pos.get('contracts', 0))) > 1e-9: # Check for non-zero contracts
|
|
|
|
|
|
+ if pos.get('symbol') == symbol and abs(float(pos.get('contracts') or 0)) > 1e-9: # Check for non-zero contracts
|
|
active_position_exists = True
|
|
active_position_exists = True
|
|
break
|
|
break
|
|
|
|
|
|
@@ -139,10 +139,12 @@ class RiskCleanupManager:
|
|
for position in positions:
|
|
for position in positions:
|
|
try:
|
|
try:
|
|
symbol = position.get('symbol', '')
|
|
symbol = position.get('symbol', '')
|
|
- contracts = float(position.get('current_position_size', 0))
|
|
|
|
- entry_price = float(position.get('entry_price', 0))
|
|
|
|
- roe_percentage = float(position.get('roe_percentage', 0))
|
|
|
|
- unrealized_pnl = float(position.get('unrealized_pnl', 0))
|
|
|
|
|
|
+
|
|
|
|
+ # Safely convert position values, handling None values
|
|
|
|
+ contracts = float(position.get('current_position_size') or 0)
|
|
|
|
+ entry_price = float(position.get('entry_price') or 0)
|
|
|
|
+ roe_percentage = float(position.get('roe_percentage') or 0)
|
|
|
|
+ unrealized_pnl = float(position.get('unrealized_pnl') or 0)
|
|
|
|
|
|
if contracts == 0 or entry_price <= 0:
|
|
if contracts == 0 or entry_price <= 0:
|
|
logger.info(f"Skipping position {symbol}: contracts={contracts}, entry_price={entry_price}")
|
|
logger.info(f"Skipping position {symbol}: contracts={contracts}, entry_price={entry_price}")
|
|
@@ -225,7 +227,7 @@ Action: Executing emergency exit order..."""
|
|
if current_positions:
|
|
if current_positions:
|
|
for pos in current_positions:
|
|
for pos in current_positions:
|
|
symbol = pos.get('symbol')
|
|
symbol = pos.get('symbol')
|
|
- contracts = float(pos.get('contracts', 0))
|
|
|
|
|
|
+ contracts = float(pos.get('contracts') or 0)
|
|
if symbol and contracts != 0:
|
|
if symbol and contracts != 0:
|
|
position_symbols.add(symbol)
|
|
position_symbols.add(symbol)
|
|
|
|
|
|
@@ -324,14 +326,14 @@ Time: {datetime.now(timezone.utc).strftime('%H:%M:%S')}\\n\\n
|
|
position_map = {}
|
|
position_map = {}
|
|
for position in positions:
|
|
for position in positions:
|
|
symbol = position.get('symbol')
|
|
symbol = position.get('symbol')
|
|
- contracts = float(position.get('contracts', 0))
|
|
|
|
|
|
+ contracts = float(position.get('contracts') or 0)
|
|
if symbol and contracts != 0:
|
|
if symbol and contracts != 0:
|
|
token_map_key = symbol.split('/')[0] if '/' in symbol else symbol.split(':')[0]
|
|
token_map_key = symbol.split('/')[0] if '/' in symbol else symbol.split(':')[0]
|
|
position_map[token_map_key] = {
|
|
position_map[token_map_key] = {
|
|
'symbol': symbol,
|
|
'symbol': symbol,
|
|
'contracts': contracts,
|
|
'contracts': contracts,
|
|
'side': 'long' if contracts > 0 else 'short',
|
|
'side': 'long' if contracts > 0 else 'short',
|
|
- 'entry_price': float(position.get('entryPx', 0))
|
|
|
|
|
|
+ 'entry_price': float(position.get('entryPx') or 0)
|
|
}
|
|
}
|
|
|
|
|
|
newly_detected = 0
|
|
newly_detected = 0
|
|
@@ -397,7 +399,7 @@ Time: {datetime.now(timezone.utc).strftime('%H:%M:%S')}\\n\\n
|
|
return
|
|
return
|
|
|
|
|
|
current_positions = self.market_monitor_cache.cached_positions or []
|
|
current_positions = self.market_monitor_cache.cached_positions or []
|
|
- position_symbols = {pos.get('symbol') for pos in current_positions if pos.get('symbol') and abs(float(pos.get('contracts', 0))) > 1e-9}
|
|
|
|
|
|
+ position_symbols = {pos.get('symbol') for pos in current_positions if pos.get('symbol') and abs(float(pos.get('contracts') or 0)) > 1e-9}
|
|
|
|
|
|
current_open_orders_on_exchange = self.market_monitor_cache.cached_orders or []
|
|
current_open_orders_on_exchange = self.market_monitor_cache.cached_orders or []
|
|
open_exchange_order_ids = {order.get('id') for order in current_open_orders_on_exchange if order.get('id')}
|
|
open_exchange_order_ids = {order.get('id') for order in current_open_orders_on_exchange if order.get('id')}
|
|
@@ -451,7 +453,7 @@ Time: {datetime.now(timezone.utc).strftime('%H:%M:%S')}\\n\\n
|
|
# Get current open positions on the exchange from the cache
|
|
# Get current open positions on the exchange from the cache
|
|
active_position_symbols = {
|
|
active_position_symbols = {
|
|
pos.get('symbol') for pos in (self.market_monitor_cache.cached_positions or [])
|
|
pos.get('symbol') for pos in (self.market_monitor_cache.cached_positions or [])
|
|
- if pos.get('symbol') and abs(float(pos.get('contracts', 0))) > 1e-9
|
|
|
|
|
|
+ if pos.get('symbol') and abs(float(pos.get('contracts') or 0)) > 1e-9
|
|
}
|
|
}
|
|
|
|
|
|
cleaned_count = 0
|
|
cleaned_count = 0
|
|
@@ -547,7 +549,7 @@ Time: {datetime.now(timezone.utc).strftime('%H:%M:%S')}\\n\\n
|
|
logger.warning("⚠️ Failed to fetch exchange positions - skipping stop loss check")
|
|
logger.warning("⚠️ Failed to fetch exchange positions - skipping stop loss check")
|
|
continue
|
|
continue
|
|
position_exists = any(
|
|
position_exists = any(
|
|
- pos['symbol'] == symbol and abs(float(pos.get('contracts', 0))) > 1e-9
|
|
|
|
|
|
+ pos['symbol'] == symbol and abs(float(pos.get('contracts') or 0)) > 1e-9
|
|
for pos in exchange_positions
|
|
for pos in exchange_positions
|
|
)
|
|
)
|
|
|
|
|