multiticker / forecaster_cli.py
Jitendra12421's picture
Upload 12 files
c17ec23 verified
Raw
History Blame
11.5 kB
import argparse
import pandas as pd
import numpy as np
import concurrent.futures
import time
import joblib
import datetime
import os
import warnings
warnings.filterwarnings('ignore')
from feature_pipeline import ScreenerScraper
TIERS = {
"Large": "https://archives.nseindia.com/content/indices/ind_nifty100list.csv",
"Mid": "https://archives.nseindia.com/content/indices/ind_niftymidcap150list.csv",
"Small": "https://archives.nseindia.com/content/indices/ind_niftysmallcap250list.csv",
"Nifty50": "https://archives.nseindia.com/content/indices/ind_nifty50list.csv"
}
def get_current_historic_val(table_data, row_name):
if not table_data: return np.nan
for row in table_data.get('rows', []):
if row and row[0].lower() == row_name.lower():
for val_raw in reversed(row[1:]):
val = str(val_raw).replace(',', '').replace('%', '').strip()
if val not in ['-', '']:
try: return float(val)
except: continue
return np.nan
def fetch_live_features(ticker):
try:
scraper = ScreenerScraper()
data = None
for _ in range(3):
try:
data = scraper.get_stock_info(ticker)
if "error" in data and "429" in data["error"]:
time.sleep(1.5)
continue
break
except Exception:
time.sleep(1)
if not data or "error" in data: return None
pl = data['history'].get('profit-loss', {})
bs = data['history'].get('balance-sheet', {})
ratios = data['history'].get('ratios', {})
sales_row = None
for row in pl.get('rows', []):
if row and row[0].lower() == 'sales':
sales_row = row
break
sales = np.nan
prev_sales = np.nan
if sales_row and len(sales_row) >= 3:
try:
sales = float(str(sales_row[-1]).replace(',', '').strip())
prev_sales = float(str(sales_row[-2]).replace(',', '').strip())
except:
pass
opm = get_current_historic_val(pl, 'OPM %')
net_profit = get_current_historic_val(pl, 'Net Profit')
equity = get_current_historic_val(bs, 'Equity Capital')
reserves = get_current_historic_val(bs, 'Reserves')
borrowings = get_current_historic_val(bs, 'Borrowings')
roce = get_current_historic_val(ratios, 'ROCE %')
sales_growth = ((sales - prev_sales) / prev_sales * 100) if pd.notnull(prev_sales) and prev_sales > 0 else np.nan
total_eq = equity + reserves if pd.notnull(equity) and pd.notnull(reserves) else np.nan
roe = (net_profit / total_eq * 100) if pd.notnull(total_eq) and total_eq > 0 else np.nan
debt_to_equity = (borrowings / total_eq) if pd.notnull(total_eq) and total_eq > 0 else np.nan
pe = np.nan
for metric in data.get('metrics', []):
if metric['name'] == 'Stock P/E':
pe = metric['value']
break
return {
'Ticker': ticker,
'Sales_Growth': sales_growth,
'OPM': opm,
'ROCE': roce,
'ROE': roe,
'Debt_to_Equity': debt_to_equity,
'PE_Ratio': pe
}
except Exception:
return None
def get_market_cap_tier(ticker):
print(f"[{ticker}] Resolving market cap classification from NSE servers...")
try:
large = pd.read_csv(TIERS["Large"])['Symbol'].tolist()
if ticker in large: return "Large"
mid = pd.read_csv(TIERS["Mid"])['Symbol'].tolist()
if ticker in mid: return "Mid"
small = pd.read_csv(TIERS["Small"])['Symbol'].tolist()
if ticker in small: return "Small"
except Exception as e:
print(f"Warning: Could not fetch NSE lists ({e}). Defaulting to Small Cap.")
return "Small" # Default to small cap model for everything else
def generate_reasoning(features, tier, prob):
reasons = []
if tier == "Large":
if pd.notnull(features.get('Sales_Growth')):
if features['Sales_Growth'] < 5: reasons.append(f"Weak Sales Growth ({features['Sales_Growth']:.1f}%) drags down Large Cap momentum.")
elif features['Sales_Growth'] > 15: reasons.append(f"Strong Sales Growth ({features['Sales_Growth']:.1f}%) is an excellent indicator for Large Caps.")
if pd.notnull(features.get('ROE')) and features['ROE'] > 20:
reasons.append(f"High ROE ({features['ROE']:.1f}%) shows efficient capital use.")
elif tier == "Small":
if pd.notnull(features.get('Debt_to_Equity')):
if features['Debt_to_Equity'] > 1.5: reasons.append(f"Dangerously high Debt/Equity ({features['Debt_to_Equity']:.2f}) signals severe structural risk.")
elif features['Debt_to_Equity'] < 0.5: reasons.append(f"Low Debt/Equity ({features['Debt_to_Equity']:.2f}) provides strong survival padding.")
if pd.notnull(features.get('OPM')) and features['OPM'] < 10:
reasons.append(f"Low margins ({features['OPM']:.1f}%) leave little room for error.")
elif tier == "Mid":
reasons.append("Mid caps exhibit inverted return logic. Metrics are volatile and evaluated in aggregate.")
if not reasons:
reasons.append("Fundamentals are mixed or average, showing no extreme strengths or weaknesses.")
return " ".join(reasons)
def run_inference(ticker):
tier = get_market_cap_tier(ticker)
print(f"[{ticker}] Classified as: {tier} Cap")
print(f"[{ticker}] Extracting live fundamental data...")
features = fetch_live_features(ticker)
if not features:
print(f"[{ticker}] Error: Could not extract live data.")
return
df = pd.DataFrame([features])
model_features = ['Sales_Growth', 'OPM', 'ROCE', 'ROE', 'Debt_to_Equity', 'PE_Ratio']
X = df[model_features].copy()
try:
model = joblib.load(f'rf_model_{tier.lower()}.pkl')
imputer = joblib.load(f'imputer_{tier.lower()}.pkl')
scaler = joblib.load(f'scaler_{tier.lower()}.pkl')
except Exception as e:
print(f"Error loading models: {e}")
return
X_imputed = imputer.transform(X)
X_scaled = scaler.transform(X_imputed)
prob = model.predict_proba(X_scaled)[0][1]
if prob > 0.65:
decision = "BUY"
else:
decision = "PASS"
reasoning = generate_reasoning(features, tier, prob)
print("\n" + "="*50)
print(f" FORECAST FOR {ticker} ({tier} Cap Model)")
print("="*50)
print(f" Decision: {decision} (Confidence: {prob*100:.1f}%)")
print(f" Reasoning: {reasoning}")
print("-" * 50)
print(f" Sales Growth: {features['Sales_Growth']:.2f}%")
print(f" ROE: {features['ROE']:.2f}%")
print(f" ROCE: {features['ROCE']:.2f}%")
print(f" Debt/Equity: {features['Debt_to_Equity']:.2f}")
print(f" OPM: {features['OPM']:.2f}%")
print("="*50 + "\n")
return {"Ticker": ticker, "Tier": tier, "Decision": decision, **features}
def run_daemon():
print("Starting NIFTY 50 Forecasting Daemon...")
try:
df_nifty = pd.read_csv(TIERS["Nifty50"])
tickers = df_nifty['Symbol'].tolist()
except Exception as e:
print(f"Could not load NIFTY 50 tickers: {e}")
return
print(f"[{datetime.datetime.now()}] Waking up to process NIFTY 50...")
# We don't fetch market cap tiers because NIFTY 50 is all Large Cap
# Verify rate limit status first with a test call
print("Testing connection...")
test_feat = fetch_live_features("RELIANCE")
if not test_feat:
print(f"[{datetime.datetime.now()}] Error: IP Rate Limit Block detected on startup. Will not proceed.")
return
results = []
total = len(tickers)
processed = 0
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
futures = {executor.submit(fetch_live_features, ticker): ticker for ticker in tickers}
for future in concurrent.futures.as_completed(futures):
res = future.result()
processed += 1
if res:
results.append(res)
pct = int((processed / total) * 100)
bar = '#' * (pct // 5) + '-' * (20 - (pct // 5))
print(f"\r[NIFTY 50] {bar} {pct}% ({processed}/{total})", end='', flush=True)
print("\n", flush=True)
if results:
df = pd.DataFrame(results)
features = ['Sales_Growth', 'OPM', 'ROCE', 'ROE', 'Debt_to_Equity', 'PE_Ratio']
try:
# NIFTY 50 is strictly Large Cap
model = joblib.load('rf_model_large.pkl')
imputer = joblib.load('imputer_large.pkl')
scaler = joblib.load('scaler_large.pkl')
X = df[features].copy()
X_imputed = imputer.transform(X)
X_scaled = scaler.transform(X_imputed)
probs = model.predict_proba(X_scaled)[:, 1]
df['Confidence'] = probs
df['Decision'] = df['Confidence'].apply(lambda x: "BUY" if x > 0.65 else "PASS")
# Generate reasoning for each
reasonings = []
for _, row in df.iterrows():
feat_dict = {
'Sales_Growth': row['Sales_Growth'],
'ROE': row['ROE'],
'ROCE': row['ROCE'],
'Debt_to_Equity': row['Debt_to_Equity'],
'OPM': row['OPM']
}
r = generate_reasoning(feat_dict, "Large", row['Confidence'])
reasonings.append(r)
df['Reasoning'] = reasonings
# Format report and save to JSON
report = df[['Ticker', 'Decision', 'Confidence', 'Reasoning', 'Sales_Growth', 'ROE', 'Debt_to_Equity', 'OPM']]
report_dict = report.to_dict(orient='records')
import json
with open("nifty50_predictions.json", "w") as f:
json.dump({"last_updated": datetime.datetime.now().isoformat(), "predictions": report_dict}, f, indent=4)
print(f"[{datetime.datetime.now()}] Successfully generated nifty50_predictions.json with {len(df)} stocks.", flush=True)
except Exception as e:
print(f"Error during NIFTY 50 prediction: {e}", flush=True)
else:
print(f"[{datetime.datetime.now()}] Error: Failed to extract live data for all 50 stocks (Likely IP Rate Limit Block). Skipping report generation.", flush=True)
print("Daemon run completed.", flush=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Multi-Cap Stock Forecasting Engine")
parser.add_argument("--ticker", type=str, help="Run an on-demand forecast for a specific ticker")
parser.add_argument("--daemon", action="store_true", help="Run the background 14-day loop for NIFTY 50")
args = parser.parse_args()
if args.daemon:
run_daemon()
elif args.ticker:
run_inference(args.ticker.upper())
else:
print("Please provide a --ticker or run with --daemon. Use -h for help.")