Live Option Chain Fetch¶
Pull live NIFTY/BANKNIFTY strikes from ServLoci's public option-chain endpoint.
Part 11 of 20 in the ServLoci algo/options trading notebook series — full index in notebooks/README.md.
Setup¶
# Get your dedicated static IPv6 + SOCKS5 credentials free:
# https://comm.servloci.in/register (or /auth/google?free=1 for an instant trial)
# Your api_key / api_secret pair shows up in the portal after signup:
# https://comm.servloci.in/user
!pip install -q "requests[socks]"
!curl -sL https://comm.servloci.in/sdk/servloci.py -o servloci.py
import os
from servloci import ServLoci
SERVLOCI_API_KEY = os.environ.get("SERVLOCI_API_KEY", "dhan:1000000001") # broker:client_id
SERVLOCI_API_SECRET = os.environ.get("SERVLOCI_API_SECRET", "") # from the portal — leave blank to run this notebook in demo mode
sl = None
if SERVLOCI_API_SECRET:
sl = ServLoci(api_key=SERVLOCI_API_KEY, api_secret=SERVLOCI_API_SECRET)
print("ServLoci configured:", sl.host, sl.port)
else:
print("SERVLOCI_API_SECRET not set — running in demo mode (no live proxy calls).")
No proxy needed for this one — this hits ServLoci's own public option-chain endpoint (5s server-side cache), the same one /tools/strategy-builder uses.
import requests
resp = requests.get("https://comm.servloci.in/api/market/option-chain", params={"symbol": "NIFTY"}, timeout=10)
resp.raise_for_status()
data = resp.json()
spot, chain = data.get("spot"), data.get("strikes", [])
print("Spot:", spot, "| strikes returned:", len(chain))
chain[:5]
chain is shaped [{strike, ce: {ltp, ...}, pe: {ltp, ...}}, ...] — pass it straight into the find_premium() / template builders from notebooks 08-10 for real (not Black-Scholes-estimated) premiums.
« Previous: Strategy: Iron Condor
Next: Implied Volatility Skew »
Try the concepts above interactively: Options Strategy Builder · Docs · Get your static IP