From 6c878c4e636adf3aac367ec95c6106eaf2982158 Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Wed, 4 Feb 2026 16:47:03 -0700 Subject: [PATCH] Fix duplicate return, add defensive null checks for bid/ask prices, and protect PnL calculation --- alpaca_trader/api.py | 6 ++++++ alpaca_trader/engine.py | 2 +- alpaca_trader/filters.py | 1 - 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/alpaca_trader/api.py b/alpaca_trader/api.py index aa89606..7c58adc 100644 --- a/alpaca_trader/api.py +++ b/alpaca_trader/api.py @@ -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: diff --git a/alpaca_trader/engine.py b/alpaca_trader/engine.py index 4ef7423..4e47393 100644 --- a/alpaca_trader/engine.py +++ b/alpaca_trader/engine.py @@ -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 diff --git a/alpaca_trader/filters.py b/alpaca_trader/filters.py index e427182..7a58f2f 100644 --- a/alpaca_trader/filters.py +++ b/alpaca_trader/filters.py @@ -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