Spaces:
Running
Running
Update src/streamlit_app.py
Browse files129 unmodified lines1301311321331341351361371381391401411421 unmodified line14414514614714814915015115215315411 unmodified lines1661671681691701711721731741751761771781 unmodified line1801811821831841851861871881891902 unmodified lines1931941951961971981998 unmodified lines208209210211212213214338 unmodified lines553554555556557558559133 unmodified lines693694695696697698699700701702703202 unmodified lines90690790890991091191232 unmodified lines94594694794894995095139 unmodified lines991992993994995996997129 unmodified lines"""Bulk-fetch ~1 month of daily bars to compute each ticker's typical daily volume."""avg_vols = {}batch_size = 150total_batches = max(1, (len(tickers_tuple) + batch_size - 1) // batch_size)progress = st.progress(0, text="Fetching volume baselines...")for b, i in enumerate(range(0, len(tickers_tuple), batch_size)):batch = tickers_tuple[i:i + batch_size]try:data = yf.download(list(batch), period="1mo", interval="1d", group_by="ticker",progress=False, threads=True)except Exception:progress.progress((b + 1) / total_batches)1 unmodified linefor ticker in batch:try:try:raw = data[ticker]except Exception:raw = datavol = raw["Volume"].dropna()if len(vol) >= 5:avg_vols[ticker] = float(vol.mean())except Exception:11 unmodified linesresults = []batch_size = 150total_batches = max(1, (len(tickers_tuple) + batch_size - 1) // batch_size)progress = st.progress(0, text="Scanning premarket activity...")for b, i in enumerate(range(0, len(tickers_tuple), batch_size)):batch = tickers_tuple[i:i + batch_size]try:data = yf.download(list(batch), period="1d", interval="1m", prepost=True,group_by="ticker", progress=False, threads=True)except Exception:progress.progress((b + 1) / total_batches, text=f"Batch {b + 1}/{total_batches} failed, continuing...")1 unmodified linefor ticker in batch:try:try:raw = data[ticker]except Exception:raw = datadf = raw.dropna(subset=["Close"])if df.empty:continue2 unmodified linesdf.index = df.index.tz_convert("America/New_York")else:df = df.copy()df.index = df.index.tz_localize("America/New_York")premarket = df[df.index.time < dt_time(9, 30)]if premarket.empty:8 unmodified linesif pm_volume < min_abs_volume or pct_of_avg < min_pct_avg_vol:continueopen_price = premarket["Close"].iloc[0]last_price = premarket["Close"].iloc[-1]pct_change = ((last_price - open_price) / open_price * 100) if open_price else np.nan338 unmodified lines# =========================================================# TAB 3: SNIPER SCANNER (historical analog / triple-positive setup scanner)# =========================================================def sniper_rsi(series, period=14):133 unmodified linesfor ticker in batch:try:try:raw = data[ticker]except Exception:raw = datadf = raw.dropna()if len(df) < 200:continueresult = analyze_ticker(df, rsi_threshold, stretch_threshold, strict_window, lookback, win_rate_min)202 unmodified lines# =========================================================# ANALOG PATH PROJECTION (used by the Signal Tracker chart)# =========================================================ANALOG_LOOKBACK = 6032 unmodified linesend_idx = min(end_idx, len(close) - 1)if end_idx - lookback - forward < 5:return None # not enough pre-flag history for this methodmatches = _similarity_matches(close, end_idx, lookback, forward)if not matches:39 unmodified lines# =========================================================# TAB 4: SIGNAL TRACKER (predicted vs. actual, out-of-sample)# =========================================================def render_signal_tracker():129 unmodified lines1301311321331341351361371381391401411421431 unmodified line14514614714814915015115215315415515615715815916016116211 unmodified lines1741751761771781791801811821831841851861871 unmodified line1891901911921931941951961971981992002012022032042052062 unmodified lines2092102112122132142158 unmodified lines224225226227228229230338 unmodified lines569570571572573574575133 unmodified lines709710711712713714715716717718719720721722723724725202 unmodified lines92892993093193293393432 unmodified lines96796896997097197297339 unmodified lines1013101410151016101710181019129 unmodified lines"""Bulk-fetch ~1 month of daily bars to compute each ticker's typical daily volume."""avg_vols = {}batch_size = 150tickers_list = list(tickers_tuple)total_batches = max(1, (len(tickers_list) + batch_size - 1) // batch_size)progress = st.progress(0, text="Fetching volume baselines...")for b, i in enumerate(range(0, len(tickers_list), batch_size)):batch = tickers_list[i:i + batch_size]try:data = yf.download(batch, period="1mo", interval="1d", group_by="ticker",progress=False, threads=True)except Exception:progress.progress((b + 1) / total_batches)1 unmodified linefor ticker in batch:try:# Safe MultiIndex column extraction checkingif isinstance(data.columns, pd.MultiIndex):if ticker in data.columns.levels[0]:vol = data[ticker]["Volume"].dropna()else:continueelse:if len(batch) == 1:vol = data["Volume"].dropna()else:continueif len(vol) >= 5:avg_vols[ticker] = float(vol.mean())except Exception:11 unmodified linesresults = []batch_size = 150tickers_list = list(tickers_tuple)total_batches = max(1, (len(tickers_list) + batch_size - 1) // batch_size)progress = st.progress(0, text="Scanning premarket activity...")for b, i in enumerate(range(0, len(tickers_list), batch_size)):batch = tickers_list[i:i + batch_size]try:data = yf.download(batch, period="1d", interval="1m", prepost=True,group_by="ticker", progress=False, threads=True)except Exception:progress.progress((b + 1) / total_batches, text=f"Batch {b + 1}/{total_batches} failed, continuing...")1 unmodified linefor ticker in batch:try:# Bulletproof structural check for MultiIndex extraction layersif isinstance(data.columns, pd.MultiIndex):if ticker in data.columns.levels[0]:df = data[ticker].dropna(subset=["Close"])else:continueelse:if len(batch) == 1:df = data.dropna(subset=["Close"])else:continueif df.empty:continue2 unmodified linesdf.index = df.index.tz_convert("America/New_York")else:df = df.copy()df.index = df.index.tz_localize("UTC").tz_convert("America/New_York")premarket = df[df.index.time < dt_time(9, 30)]if premarket.empty:8 unmodified linesif pm_volume < min_abs_volume or pct_of_avg < min_pct_avg_vol:continueopen_price = premarket["Open"].iloc[0]last_price = premarket["Close"].iloc[-1]pct_change = ((last_price - open_price) / open_price * 100) if open_price else np.nan338 unmodified lines# =========================================================# TAB 3: SNIPER SCANNER# =========================================================def sniper_rsi(series, period=14):133 unmodified linesfor ticker in batch:try:if isinstance(data.columns, pd.MultiIndex):if ticker in data.columns.levels[0]:df = data[ticker].dropna()else:continueelse:if len(batch) == 1:df = data.dropna()else:continueif len(df) < 200:continueresult = analyze_ticker(df, rsi_threshold, stretch_threshold, strict_window, lookback, win_rate_min)202 unmodified lines# =========================================================# ANALOG PATH PROJECTION# =========================================================ANALOG_LOOKBACK = 6032 unmodified linesend_idx = min(end_idx, len(close) - 1)if end_idx - lookback - forward < 5:return Nonematches = _similarity_matches(close, end_idx, lookback, forward)if not matches:39 unmodified lines# =========================================================# TAB 4: SIGNAL TRACKER# =========================================================def render_signal_tracker():
src/streamlit_app.py+51-29