Fix duplicate return, add defensive null checks for bid/ask prices, and protect PnL calculation
This commit is contained in:
@@ -61,6 +61,12 @@ class AlpacaClient:
|
||||
return None
|
||||
bid_price = getattr(quote, 'bid_price', None)
|
||||
ask_price = getattr(quote, 'ask_price', None)
|
||||
|
||||
if bid_price is None or ask_price is None:
|
||||
return None
|
||||
if bid_price <= 0 or ask_price <= 0:
|
||||
return None
|
||||
|
||||
if limit_price:
|
||||
price_source = limit_price
|
||||
else:
|
||||
|
||||
@@ -1076,7 +1076,7 @@ def main():
|
||||
status_msg = f"⏱️ {current_time} | {position_status} | {regime.upper()}"
|
||||
|
||||
if position_active:
|
||||
if entry_price > 0:
|
||||
if entry_price > 0 and current_price > 0:
|
||||
pnl_pct = ((current_price - entry_price) / entry_price) * 100 if position_type == 'long' else ((entry_price - current_price) / entry_price) * 100
|
||||
else:
|
||||
pnl_pct = 0
|
||||
|
||||
@@ -99,4 +99,3 @@ def get_vix(client: AlpacaClient, symbol: str, use_vix_filter: bool):
|
||||
logger.warning(f"Could not calculate volatility: {e}")
|
||||
logger.warning("VIX data unavailable, skipping VIX filter for this iteration")
|
||||
return 0
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user