Guides /
A NIFTY options dataset for machine learning: features, labels and leak-free splits
Using NIFTY options history as a machine learning dataset: turning minute bars into features, labelling without look-ahead, and splitting by time and expiry.
Market data makes it easy to build a model that looks brilliant and fails live. Most failures are data mistakes: features that peek at the future, or train and test sets that share a session. This guide sets up a NIFTY options dataset so those mistakes are hard to make.
Features from minute bars
import pandas as pd
df = pd.read_parquet("nifty_atm_straddle_1min.parquet") # one row per minute
df = df.set_index("datetime").sort_index()
f = pd.DataFrame(index=df.index)
f["ret_5"] = df["straddle"].pct_change(5)
f["ret_15"] = df["straddle"].pct_change(15)
f["vol_15"] = df["straddle"].pct_change().rolling(15).std()
f["oi_chg"] = df["oi"].diff(15)
f["mins_to_close"] = (15 * 60 + 30) - (df.index.hour * 60 + df.index.minute)
Everything above uses only past and current bars.
Labels without look-ahead
Define the label as the change over the next N minutes, then drop rows where those N minutes cross the end of the session. Never fill a label from the next day.
N = 15
f["y"] = df["straddle"].shift(-N) / df["straddle"] - 1
same_day = df.index.normalize() == df.index.to_series().shift(-N).dt.normalize()
f = f[same_day].dropna()
Splitting
- Split by date, never by random rows: minutes in one session are strongly related.
- Leave at least one full day out between train and test.
- Keep whole expiry weeks together; a weekly series should sit entirely on one side.
More history gives more independent sessions: 3 years or since 2020.
Questions
Is NIFTY options data suitable for machine learning?
Yes. 1-minute Parquet files load directly into pandas or any ML library. Split by time to avoid leakage.
How much history should I use?
As many sessions as you can. The since-2020 pack gives the longest NIFTY history at 1-minute.
What format is the dataset?
Parquet, with typed columns. CSV on request.