Spaces:
Sleeping
Sleeping
File size: 11,826 Bytes
c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 c975193 c17ec23 f32b8de c17ec23 a3b7b62 c17ec23 a3b7b62 c17ec23 a3b7b62 c17ec23 a3b7b62 c17ec23 f76aebb a3b7b62 f76aebb a3b7b62 c17ec23 a3b7b62 c17ec23 a3b7b62 c17ec23 a3b7b62 c17ec23 4249723 c17ec23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | 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):
import requests
from bs4 import BeautifulSoup
import re
import numpy as np
try:
url = f"https://ticker.finology.in/company/{ticker}"
html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text
soup = BeautifulSoup(html, 'html.parser')
def clean(val):
if not val: return np.nan
try: return float(re.sub(r'[^\d.]', '', val))
except: return np.nan
data = {}
for div in soup.find_all('div', class_=re.compile(r'compess')):
text = div.get_text(" ", strip=True)
if "P/E" in text: data['PE_Ratio'] = clean(text.replace('P/E', ''))
if "Sales Growth" in text: data['Sales_Growth'] = clean(text.replace('Sales Growth', ''))
if "ROE" in text: data['ROE'] = clean(text.replace('ROE', ''))
if "ROCE" in text: data['ROCE'] = clean(text.replace('ROCE', ''))
if "CASH" in text: data['CASH'] = clean(text.replace('CASH', ''))
if "DEBT " in text: data['DEBT'] = clean(text.replace('DEBT', ''))
if "Book Value" in text: data['BV'] = clean(text.replace('Book Value', ''))
if "No. of Shares" in text: data['Shares'] = clean(text.replace('No. of Shares', ''))
debt_to_equity = np.nan
if 'DEBT' in data and 'BV' in data and 'Shares' in data and data['BV'] > 0 and data['Shares'] > 0:
total_equity = data['BV'] * data['Shares']
debt_to_equity = data['DEBT'] / total_equity if total_equity > 0 else 0
return {
'Ticker': ticker,
'Sales_Growth': data.get('Sales_Growth', np.nan),
'OPM': np.nan, # Imputer will naturally handle this gracefully
'ROCE': data.get('ROCE', np.nan),
'ROE': data.get('ROE', np.nan),
'Debt_to_Equity': debt_to_equity,
'PE_Ratio': data.get('PE_Ratio', np.nan)
}
except Exception:
return None
def get_market_cap_tier(ticker):
print(f"[{ticker}] Resolving market cap classification via Finology...")
try:
import requests
from bs4 import BeautifulSoup
import re
url = f"https://ticker.finology.in/company/{ticker}"
html = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}).text
soup = BeautifulSoup(html, 'html.parser')
cap = 0
for div in soup.find_all('div', class_=re.compile(r'compess')):
text = div.get_text(" ", strip=True)
if "Market Cap" in text:
try: cap = float(re.sub(r'[^\d.]', '', text.replace('Market Cap', '')))
except: pass
if cap >= 20000: return "Large"
if cap >= 5000: return "Mid"
return "Small"
except Exception as e:
print(f"Warning: Could not fetch market cap ({e}). Defaulting to Small.")
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 get_prediction(ticker):
tier = get_market_cap_tier(ticker)
features = fetch_live_features(ticker)
if not features:
return {"error": "Could not extract live data."}
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:
return {"error": f"Error loading models: {e}"}
X_imputed = imputer.transform(X)
X_scaled = scaler.transform(X_imputed)
prob = float(model.predict_proba(X_scaled)[0][1])
decision = "BUY" if prob > 0.65 else "PASS"
reasoning = generate_reasoning(features, tier, prob)
import math
clean_features = {}
for k, v in features.items():
if isinstance(v, float) and math.isnan(v):
clean_features[k] = None
else:
clean_features[k] = v
return {
"Ticker": ticker,
"Tier": tier,
"Decision": decision,
"Confidence": prob,
"Reasoning": reasoning,
"Features": clean_features
}
def run_inference(ticker):
res = get_prediction(ticker)
if "error" in res:
print(f"[{ticker}] {res['error']}")
return
print("\n" + "="*50)
print(f" FORECAST FOR {ticker} ({res['Tier']} Cap Model)")
print("="*50)
print(f" Decision: {res['Decision']} (Confidence: {res['Confidence']*100:.1f}%)")
print(f" Reasoning: {res['Reasoning']}")
print("-" * 50)
for k, v in res['Features'].items():
print(f" {k}: {v}")
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 math
for item in report_dict:
for k, v in item.items():
if isinstance(v, float) and math.isnan(v):
item[k] = None
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.")
|