Live Option Chain Fetch¶
Pull live NIFTY/BANKNIFTY strikes from ServLoci's public option-chain endpoint.
Part 11 of 37 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).")
Reading an option chain¶
An option chain lists, for every traded strike of an underlying, the call (CE) and put (PE) contract at that strike — its last traded price (LTP), bid/ask, and open interest (OI). Three things matter before you use one:
- Moneyness. A strike is at-the-money (ATM) if it's closest to spot, in-the-money (ITM) if exercising it now would be profitable (call strike below spot, put strike above spot), and out-of-the-money (OTM) otherwise. Most retail activity clusters within a few strikes either side of ATM.
- Open interest as a crowding signal. OI is the count of contracts still open at a strike, not traded volume. Large OI at a strike is read informally as a support/resistance level — a lot of capital has a stake in price not crossing it — but it's a crowd signal, not a law of physics; large OI can and does get blown through.
- Bid-ask spread as a liquidity check. A wide spread relative to LTP means slippage on entry/exit will eat into any edge the strategy has. Illiquid far-OTM strikes routinely have spreads worse than 5-10% of premium — always check spread before sizing a trade, not just the mid price.
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. Before trusting a strike's premium, glance at its bid-ask spread and OI in the raw response — a tight theoretical payoff chart built on an illiquid strike is not a tradeable one.
« Previous: Strategy: Iron Condor
Next: Implied Volatility Skew »
Try the concepts above interactively: Options Strategy Builder · Docs · Get your static IP