INNER CODE UNIT · Python
_backfill_ticks_if_stale
aaryansinha16/AI-trader · backend/app.py:601
def _backfill_ticks_if_stale():
"""
During market hours, scan today's NIFTY-I tick coverage for gaps and
fill each one via TrueData REST `getticks`. Idempotent: deletes the
affected window first to dedupe with anything the websocket has
streamed in parallel.
Why mirror this from the candle backfill rather than relying on the
websocket alone:
• TrueData WebSocket drops several times per session (Connection
timed out → 5-15 minute reconnect gap), and on 2026-04-08 produced
long sparse intervals in the live tick chart.
• REST `getticks` covers the last 5 days at full bid/ask/OI fidelity,
so any gap inside the 5-day window can be silently patched.
• EOD scripts also patch gaps, but waiting until 16:00 means the
scoring loop and the live chart see incomplete data all day.
Throttled to once per minute (the scanner runs every 30s) — gaps don't