Spaces:
Sleeping
Sleeping
Create universe.py
Browse files- universe.py +30 -0
universe.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
class UniverseManager:
|
| 5 |
+
def __init__(self, csv_file="EQUITY_L.csv"):
|
| 6 |
+
self.file = csv_file
|
| 7 |
+
self.universe = self.load_universe()
|
| 8 |
+
|
| 9 |
+
def load_universe(self):
|
| 10 |
+
# Fallback if CSV is missing
|
| 11 |
+
if not os.path.exists(self.file):
|
| 12 |
+
print(f"⚠️ {self.file} not found. Using fallback tickers.")
|
| 13 |
+
return ["RELIANCE.NS", "TCS.NS", "INFY.NS", "HDFCBANK.NS"]
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
df = pd.read_csv(self.file)
|
| 17 |
+
# Clean column names (strip spaces)
|
| 18 |
+
df.columns = [c.strip() for c in df.columns]
|
| 19 |
+
|
| 20 |
+
# Filter for Equity Series (EQ)
|
| 21 |
+
if 'SERIES' in df.columns:
|
| 22 |
+
df = df[df['SERIES'] == 'EQ']
|
| 23 |
+
|
| 24 |
+
# Create Yahoo Finance compatible symbols
|
| 25 |
+
tickers = [f"{x}.NS" for x in df['SYMBOL'].tolist()]
|
| 26 |
+
print(f"✅ Universe loaded: {len(tickers)} stocks.")
|
| 27 |
+
return tickers
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"❌ Error loading universe CSV: {e}")
|
| 30 |
+
return ["RELIANCE.NS", "TCS.NS"]
|