Download Expired Options data using icicidirect

In this blog we will see how to download Expired Options data. But before that lets see why is the data acutely required.

Why Backtesting Matters

  • Backtesting rewinds history to test strategies. No time machine needed!
  • Spot dangers like drawdowns before risking real money. Your trading seatbelt!
  • Get hard data on profits, win rate, and risk. No gut feelings!
  • Optimize strategy parameters for the best results.
  • Proven strategies make real trading easier.
 

Example Backtesting results

Backtesting performance

 

 

 

 

Expired Contracts Data

Expired Options data is a critical component for Options Backtesting,  Also at the same time it can cost up to Lakhs to access expired data.

In this blog we will see how we can get it for free using icicidirect Breeze api.

We can access up to 3 years of options data from the api.

 

Code to download data

import os
import pandas as pd
from breeze_connect import BreezeConnect

# Initialize Breeze API connection
breeze = BreezeConnect(api_key="h23J9r0446442o(93~55n9G7270e9p89")
breeze.generate_session(api_secret="174957B6939Q2j92120A929D912697Qs", session_token="50784494")

# Define parameters
underlying = "NIFTY"  # Underlying asset
no_of_strikes_from_atm = 20  # Number of strikes to fetch around ATM
step_value = 50  # Strike step interval
opening_price = 22939  # Opening price of the underlying
expiry_date = '2025-02-20T07:00:00.000Z'  # Option expiry date
start_date = '2025-02-20T07:00:00.000Z'  # Start date for historical data
file_name = '2025-02-20'  # Folder name for saving data

# Create directory for storing option data
os.makedirs(f"Options Data/{underlying}/{file_name}", exist_ok=True)

# Calculate ATM strike price and generate strike price range
atm_strike = int(opening_price / step_value) * step_value  # Round to nearest step
strikes = [atm_strike + i * step_value for i in range(-no_of_strikes_from_atm, no_of_strikes_from_atm + 1)]

# Loop through each strike and option type
for strike in strikes:
    for right in ['call', 'put']:
        try:
            print(f"{strikes.index(strike)} Getting data for {strike} {right} strike")  # Log progress
            
            # Fetch historical data
            df = pd.DataFrame(
                breeze.get_historical_data(
                    interval="1minute",  # Time interval
                    from_date=start_date,  # Start date
                    to_date=expiry_date,  # End date
                    stock_code=underlying,  # Underlying stock
                    exchange_code="NFO",  # Exchange code for options
                    product_type="options",  # Product type
                    expiry_date=expiry_date,  # Expiry date
                    right=right,  # Option type (Call/Put)
                    strike_price=strike  # Strike price
                )['Success']  # Extract data from response
            )
        except Exception as e:
            print(e)  # Print error message if request fails
            continue  # Skip to next iteration
        
        # Format filename for saving data
        expiry_unit = expiry_date.split("-")
        year = expiry_unit[0][2:]  # Extract year (last two digits)
        month = expiry_unit[1]  # Extract month
        date = expiry_unit[2][:2]  # Extract day
        name = f"{underlying}_{year}_{month}_{date}_{strike}_{right}"  # Construct filename
        
        # Save data to CSV
        df.to_csv(f"Options Data/{underlying}/{file_name}/{name}.csv")

Your Business Potential with Our Proven Strategies

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Your Algo Trading Partner. Trade Smart, Trade Right “Trade Algo!

Company

Blog

Privacy Policy

Help

Address: WZ-1087, 2F,
Rani Bagh, Delhi – 110034

Phone: 701-153-3217

Terms And Conditions

Refund & Cancellation Policy

Subscribe Us Today

Stay Ahead in Trading – Subscribe for Updates and Insights

Address: WZ-1087, 2F, Rani Bagh, Delhi – 110034 | Phone: 701-153-3217 | Email: [email protected]

 Copyright © 2025 – TradeHull