Historical Data for Backtesting¶
Pull 2 years of NIFTY daily closes for the backtests that follow.
Part 13 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).")
Why the data matters as much as the strategy¶
Every backtest is only as trustworthy as the data feeding it. Three traps show up constantly with retail backtests:
- Corporate actions and index reconstitution. Index levels (like NIFTY) get rebalanced periodically — constituents change, weights change. A price series that doesn't handle this consistently can show artificial gaps or drift that has nothing to do with market behavior.
- Missing or misaligned sessions. Exchange holidays, special trading sessions (muhurat trading), and half-days need to line up with your strategy's calendar logic, or a "Monday entry" rule can silently fire on the wrong day.
- Look-ahead bias from adjusted data. Adjusted close prices bake in future information (like a stock split announced later) into historical rows. Using adjusted data naively can make a strategy look like it "knew" something it couldn't have known at the time. For an index like NIFTY this specific trap doesn't apply directly, but it's the single most common way retail backtests silently cheat.
Why 2 years. A weekly options strategy trades roughly 50 times a year — 2 years gives on the order of 100 trades, enough to see the strategy across a few different volatility regimes (calm stretches, at least one shock) without pretending 100 data points is statistically bulletproof. Treat any conclusion from this sample size as a hypothesis, not a proven edge — the backtest notebook that follows says this again, because it's worth repeating.
Pulls historical NIFTY closes for the backtesting notebooks that follow. No ServLoci proxy needed — Yahoo Finance is publicly reachable.
!pip install -q yfinance
import yfinance as yf
nifty = yf.download("^NSEI", period="2y", interval="1d", progress=False, multi_level_index=False)
nifty.to_csv("nifty_2y.csv")
print(nifty.tail())
nifty_2y.csv is reused by notebooks 14 and 17 — re-run this cell first if you're opening those standalone.
« Previous: Implied Volatility Skew
Next: Backtest: Weekly Short Straddle »
Try the concepts above interactively: Options Strategy Builder · Docs · Get your static IP