INNER CODE UNIT · Python

_backfill_candles_if_stale

aaryansinha16/AI-trader · backend/app.py:693

def _backfill_candles_if_stale():
    """
    During market hours:
    1. If NIFTY-I candles are >2 min old, fetch fresh bars from REST.
    2. If the current ATM option has no candles today, fetch its full-day bars.
       This handles the case where NIFTY moves significantly after market open
       and the new ATM strike was never subscribed by the tick collector.
    """
    if not _is_market_hours():
        return
    try:
        stale = read_sql(
            "SELECT MAX(timestamp) as latest FROM minute_candles WHERE symbol = 'NIFTY-I'"
        )
        if stale.empty or stale.iloc[0]["latest"] is None:
            return
        latest_ts = pd.to_datetime(stale.iloc[0]["latest"], utc=True)
        age_seconds = (pd.Timestamp.now(tz="UTC") - latest_ts).total_seconds()

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…