INNER CODE UNIT · Python
_detect_tick_gaps_today
aaryansinha16/AI-trader · backend/app.py:554
def _detect_tick_gaps_today(min_gap_secs: int = TICK_STALENESS_THRESHOLD_SECS) -> list:
"""
Find time windows in today's NIFTY-I tick stream where no ticks exist
for >= min_gap_secs. Returns a list of (start_dt, end_dt) tuples in IST
naive datetimes. Includes the trailing gap (last tick → now) if stale.
Used by _backfill_ticks_if_stale() so it fixes BOTH leading/internal
gaps (09:15 → first observed tick) AND any subsequent dropouts.
"""
today = date.today()
session_start = datetime(today.year, today.month, today.day, 9, 15, 0)
now_ist = datetime.now()
if now_ist < session_start:
return []
df = read_sql(
"SELECT timestamp FROM tick_data WHERE symbol = 'NIFTY-I' "
"AND timestamp::date = :d ORDER BY timestamp",