Hey @Tradehull_Imran , thanks for the prompt reply really appreciate it .
however this code is running properly, but my inital code for Pulling option chain data of Nifty 50 symbol is now giving error. Even after having 5 second delay between each symbol being pulled .
STOCK_SYMBOLS = [
“HDFCBANK”, “ICICIBANK”, “KOTAKBANK”, “AXISBANK”,
“RELIANCE”, “ITC”, “INFY”, “TCS”, “HCLTECH”, “BHARTIARTL”,
“ADANIENT”, “ASIANPAINT”, “JSWSTEEL”, “CIPLA”, “MARUTI”,
“BAJFINANCE”, “HINDUNILVR”, “TECHM”, “VEDL”, “SBIN”
]
INDEX_SYMBOLS = [
{“name”: “NIFTY”, “exchange”: “INDEX”},
{“name”: “BANKNIFTY”, “exchange”: “INDEX”},
{“name”: “SENSEX”, “exchange”: “INDEX”},
]
── Config ────────────────────────────────────────────────────────────────────
BASE_FOLDER = r"C:\Users\ravi_\Options Data"
os.makedirs(BASE_FOLDER, exist_ok=True)
CE_COLS = [“Strike Price”, “CE LTP”, “CE OI”, “CE Chg in OI”, “CE Volume”, “CE IV”, “CE Vega”, “CE Gamma”, “CE Delta”, “CE Theta”]
PE_COLS = [“Strike Price”, “PE LTP”, “PE OI”, “PE Chg in OI”, “PE Volume”, “PE IV”, “PE Vega”, “PE Gamma”, “PE Delta”, “PE Theta”]
today = pd.Timestamp.now().strftime(“%d-%b-%Y”)
── Core fetch ────────────────────────────────────────────────────────────────
def fetch_and_save(name, exchange, expiry, suffix, num_strikes):
try:
spot, df = tsl.get_option_chain(Underlying=name, exchange=exchange, expiry=expiry, num_strikes=num_strikes)
merged = df[CE_COLS].merge(df[PE_COLS], on=“Strike Price”)
merged[“Spot”] = spot
merged[“Date”] = today
# For stocks also add spot from get_ltp_data for accuracy
if exchange == "NSE":
try:
merged["Spot"] = tsl.get_ltp_data(names=[name])[name]
except:
pass
filepath = os.path.join(BASE_FOLDER, f"{name}_{today}{suffix}.xlsx")
# filepath = os.path.join(BASE_FOLDER, f"{name}_{today}{suffix}.csv")
# merged.to_csv(filepath, index=False)
merged.to_excel(filepath, index=False)
print(f" ✅ {name} (expiry={expiry}) → {filepath}")
except Exception as e:
print(f" ❌ {name} (expiry={expiry}) failed: {e}")
── Run ───────────────────────────────────────────────────────────────────────
print(f"\n{‘=’*60}“)
print(f” OPTIONS DATA FETCHER — {today}“)
print(f” Stocks: {len(STOCK_SYMBOLS)} | Indices: {len(INDEX_SYMBOLS)}“)
print(f”{‘=’*60}\n")
Stocks
print(“[ STOCKS ]”)
for i, name in enumerate(STOCK_SYMBOLS, 1):
print(f"[{i}/{len(STOCK_SYMBOLS)}] {name}")
for expiry, suffix in [(0, “”), (1, “A”)]:
fetch_and_save(name, “NSE”, expiry, suffix, num_strikes=15)
time.sleep(10)
print()
Indices
print(“[ INDICES ]”)
for sym in INDEX_SYMBOLS:
print(f"{sym[‘name’]}")
for expiry, suffix in [(0, “”), (1, “A”)]:
fetch_and_save(sym[“name”], sym[“exchange”], expiry, suffix, num_strikes=10)
time.sleep(2)
print()
total = (len(STOCK_SYMBOLS) + len(INDEX_SYMBOLS)) * 2
print(f"{‘=’*60}“)
print(f” DONE | Total files saved: {total} → {BASE_FOLDER}“)
print(f”{‘=’*60}")
OPTIONS DATA FETCHER — 01-Jun-2026
Stocks: 50 | Indices: 3
[ STOCKS ]
[1/20] HDFCBANK
Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: ‘’}
HDFCBANK (expiry=0) → C:\Users\ravi_\Options Data\HDFCBANK_01-Jun-2026.xlsx
Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: ‘’}
HDFCBANK (expiry=1) → C:\Users\ravi_\Options Data\HDFCBANK_01-Jun-2026A.xlsx