diff --git a/indian_paper_trading_strategy/engine/runner.py b/indian_paper_trading_strategy/engine/runner.py index 7410309..74e1d3a 100644 --- a/indian_paper_trading_strategy/engine/runner.py +++ b/indian_paper_trading_strategy/engine/runner.py @@ -156,6 +156,19 @@ def _last_execution_anchor(state: dict, mode: str) -> str | None: return state.get("last_run") or state.get("last_sip_ts") +def _parse_market_timestamp(value: str | None) -> datetime | None: + if not value: + return None + try: + parsed = datetime.fromisoformat(value) + except ValueError: + return None + market_tz = market_now().tzinfo + if parsed.tzinfo is None: + return parsed.replace(tzinfo=market_tz) + return parsed.astimezone(market_tz) + + def _pause_for_auth_expiry( user_id: str, run_id: str, @@ -330,9 +343,9 @@ def _engine_loop(config, stop_event: threading.Event): ) if last_run and not is_first_run: - next_run = datetime.fromisoformat(last_run) + delta - next_run = align_to_market_open(next_run) - if now < next_run: + parsed_last_run = _parse_market_timestamp(last_run) + next_run = align_to_market_open(parsed_last_run + delta) if parsed_last_run else None + if next_run is not None and now < next_run: wait_seconds = 5 if unit == "minutes" else 60 log_event( event="SIP_WAITING",