Spaces:
Sleeping
Sleeping
File size: 3,868 Bytes
3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd 8d4d0bb 3e964dd | 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 | #!/usr/bin/env python3
"""
Fetch S&P 500 + NIFTY 50 tickers and write data/universe/stocks.json.
Run once before starting the backend:
python backend/scripts/build_universe.py
"""
import io
import json
import sys
import time
from pathlib import Path
import requests
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
OUTPUT = ROOT / "backend" / "data" / "universe" / "stocks.json"
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
_HEADERS = {
"User-Agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/124.0 Safari/537.36"
),
"Accept-Language": "en-US,en;q=0.9",
}
def _html_tables(url: str) -> list:
import pandas as pd
resp = requests.get(url, headers=_HEADERS, timeout=30)
resp.raise_for_status()
return pd.read_html(io.StringIO(resp.text), header=0)
def fetch_sp500() -> list[dict]:
print("Fetching S&P 500 from Wikipedia...")
tables = _html_tables("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")
df = tables[0]
stocks = []
for _, row in df.iterrows():
ticker = str(row.get("Symbol", row.get("Ticker symbol", ""))).strip().replace(".", "-")
name = str(row.get("Security", row.get("Company", ""))).strip()
sector = str(row.get("GICS Sector", row.get("Sector", ""))).strip()
if not ticker or not name:
continue
stocks.append({
"ticker": ticker,
"yf_ticker": ticker,
"name": name,
"exchange": "NYSE/NASDAQ",
"sector": sector,
"country": "US",
"currency": "USD",
})
print(f" S&P 500: {len(stocks)} stocks")
return stocks
def fetch_nifty50() -> list[dict]:
print("Fetching NIFTY 50 from Wikipedia...")
tables = _html_tables("https://en.wikipedia.org/wiki/NIFTY_50")
df = None
for t in tables:
cols = [str(c).lower() for c in t.columns]
if any("symbol" in c or "ticker" in c for c in cols):
df = t
break
if df is None:
raise RuntimeError("No NIFTY 50 table found on Wikipedia")
col_map = {str(c).lower(): c for c in df.columns}
sym_col = next((col_map[c] for c in col_map if "symbol" in c or "ticker" in c), None)
name_col = next((col_map[c] for c in col_map if "company" in c or "name" in c or "stock" in c), None)
sect_col = next((col_map[c] for c in col_map if "sector" in c or "industry" in c), None)
if sym_col is None or name_col is None:
raise RuntimeError("NIFTY 50 table missing required columns")
stocks = []
for _, row in df.iterrows():
ticker = str(row[sym_col]).strip()
name = str(row[name_col]).strip()
sector = str(row[sect_col]).strip() if sect_col else ""
if not ticker or ticker.lower() == "nan":
continue
stocks.append({
"ticker": ticker,
"yf_ticker": f"{ticker}.NS",
"name": name,
"exchange": "NSE",
"sector": sector,
"country": "IN",
"currency": "INR",
})
print(f" NIFTY 50: {len(stocks)} stocks")
return stocks
def build() -> None:
sp500 = fetch_sp500()
time.sleep(1)
nifty50 = fetch_nifty50()
# Deduplicate by yf_ticker
seen: set[str] = set()
all_stocks: list[dict] = []
for s in sp500 + nifty50:
key = s["yf_ticker"].upper()
if key not in seen:
seen.add(key)
all_stocks.append(s)
print(f"\nTotal universe: {len(all_stocks)} stocks")
OUTPUT.write_text(json.dumps(all_stocks, indent=2, ensure_ascii=False))
print(f"Written → {OUTPUT}")
if __name__ == "__main__":
try:
build()
except Exception as exc:
print(f"ERROR: {exc}", file=sys.stderr)
sys.exit(1)
|