From a35e6b6d8ef6318e20d6fa116bab8df6740a99af Mon Sep 17 00:00:00 2001 From: Justin Oros Date: Wed, 4 Feb 2026 16:54:13 -0700 Subject: [PATCH] Remove redundant loss.replace, add constants for magic numbers, document threading safety --- alpaca_trader/engine.py | 18 +++++++++++++++++- alpaca_trader/indicators.py | 1 - 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/alpaca_trader/engine.py b/alpaca_trader/engine.py index 4e47393..47905dc 100644 --- a/alpaca_trader/engine.py +++ b/alpaca_trader/engine.py @@ -16,6 +16,17 @@ from .filters import check_volume, check_candle_pattern, check_macd_confirmation from .filters import check_multiframe_confluence from .utils import EASTERN, seconds_to_human_readable +BARS_FOR_200_SMA = 210 +BARS_FOR_SIGNAL = 200 +BARS_FOR_REGIME = 50 +BARS_FOR_ATR = 50 +MIN_BARS_FOR_ATR = 14 +VOLUME_LOOKBACK = 20 +DEFAULT_STOP_LOSS_PCT = 0.02 +VIX_LOOKBACK_DAYS = 5 +SPY_VOLATILITY_LOOKBACK = 20 +VOLATILITY_ANNUALIZATION_FACTOR = 252 + SCRIPT_DIR = Path(__file__).parent LOG_PATH = SCRIPT_DIR / "trading.log" DEBUG_LOG_PATH = SCRIPT_DIR / "debug.log" @@ -327,6 +338,11 @@ class SignalState: self.last_bearish_crossover_bar = -999 class PositionState: + """ + Tracks position state for profit taking and stop management. + NOTE: This bot runs single-threaded - no locks needed. + If extending to multi-threaded, add threading.Lock() protection. + """ def __init__(self): self.target_1_hit = False self.trailing_stop = None @@ -591,7 +607,7 @@ def calculate_position_size(equity, stop_loss, current_price): def advanced_signal_generator(symbol): debug_print(f"Generating signal for {symbol}") - bars = get_recent_bars(symbol, 200) + bars = get_recent_bars(symbol, BARS_FOR_SIGNAL) if bars is None or len(bars) < LONG_WINDOW: debug_print("Insufficient data for signal generation") return None, 0, 0, None diff --git a/alpaca_trader/indicators.py b/alpaca_trader/indicators.py index 241b35f..e0a1064 100644 --- a/alpaca_trader/indicators.py +++ b/alpaca_trader/indicators.py @@ -10,7 +10,6 @@ def rsi(data, window=14): delta = data.diff() gain = (delta.where(delta > 0, 0)).rolling(window=window).mean() loss = (-delta.where(delta < 0, 0)).rolling(window=window).mean() - loss = loss.replace(0, 1e-10) loss = loss.clip(lower=1e-10) rs = gain / loss rsi_val = 100 - (100 / (1 + rs))