# intraday.py import yfinance as yf import pandas as pd from chart_builder import build_chart # ------------------------------- # Fetch + Clean intraday dataset # ------------------------------- def _fetch_intraday(symbol, interval="5m", period="1d"): yfs = f"{symbol}.NS" try: df = yf.download( yfs, interval=interval, period=period, progress=False ) except Exception as e: return None, str(e) if df is None or df.empty: return None, "No intraday data returned" # Clean index timestamps df.index = pd.to_datetime(df.index) # Remove timezone if present try: df.index = df.index.tz_localize(None) except: pass # Round values df = df.round(2) return df, None # ------------------------------- # Main intraday function (UI return) # ------------------------------- def fetch_intraday(symbol, interval="5m", period="1d"): """ Supported: interval = 1m,2m,5m,15m,30m,60m period = 1d,5d,1mo """ df, err = _fetch_intraday(symbol, interval, period) if err: return { "html": f"

Intraday Error

{err}

", "data": {} } if df is None or df.empty: return { "html": f"

No Intraday Data for {symbol}

", "data": {} } # Build chart using indicator engine chart_html = build_chart(df) # Convert last rows to table table_html = df.tail(200).to_html( classes="styled-table", border=0 ) final = f"""

Intraday Chart — {symbol} ({interval}, {period})

{chart_html}

Last 200 Rows

{table_html}
""" return { "html": final, "data": df.tail(200).to_dict() }