Fixed ATR division by zero, replaced hard-coded sleep with dynamic market open calculation, removed unused dependencies

This commit is contained in:
Justin Oros
2026-02-04 10:56:06 -07:00
parent 8fe44df1c0
commit 028cdf2933
2 changed files with 35 additions and 3 deletions
+34
View File
@@ -608,6 +608,10 @@ def atr_based_trailing_stop(symbol, entry_price, current_price, initial_stop, po
current_atr = atr(bars['high'], bars['low'], bars['close']).iloc[-1]
if current_atr <= 0 or pd.isna(current_atr):
debug_print(f"Invalid ATR value: {current_atr}, using initial stop")
return False
if position_type == 'long':
new_stop = current_price - (current_atr * ATR_STOP_MULTIPLIER)
if new_stop > position_state.trailing_stop:
@@ -874,6 +878,36 @@ def main():
logger.info("✅ Day complete. Waiting for next session...")
debug_print(f"Day complete. Trades: {trade_count}, PnL: ${session_pnl:+.2f}")
next_open = None
next_close = None
try:
clock = api.get_clock()
if clock.next_open and clock.next_close:
next_open = clock.next_open
next_close = clock.next_close
if next_open.tzinfo is None:
next_open = EASTERN.localize(next_open)
else:
next_open = next_open.astimezone(EASTERN)
if next_close.tzinfo is None:
next_close = EASTERN.localize(next_close)
else:
next_close = next_close.astimezone(EASTERN)
except Exception as e:
debug_print(f"Could not fetch next open time: {e}")
if next_open:
now = datetime.now(EASTERN)
wait_seconds = (next_open - now).total_seconds()
if wait_seconds > 0:
logger.info(f"⏰ Next session: {next_open.strftime('%Y-%m-%d %I:%M %p ET')}")
logger.info(f"⏳ Sleeping {seconds_to_human_readable(int(wait_seconds))}")
debug_print(f"Sleeping until next market open: {wait_seconds}s")
time.sleep(wait_seconds)
else:
time.sleep(60)
else:
logger.info("⏳ Sleeping 1 hour before retry")
time.sleep(3600)
except Exception as e:
-2
View File
@@ -4,5 +4,3 @@ pytz
python-dotenv
alpaca-trade-api
backoff
pydantic
pydantic-settings