As historical option charts are not easily available on broker terminals. I thought about writing a program to get one with the help of ICICI Direct Breeze api.
This code will work only if you have the following:
- Python 3.xx installed
- ICICI direct breeze api access or if you want to use zerodha’s kiteconnect or any other source then you will have to make necessary changes to the python code
In order to get access to breeze api you will need to register on the following site: https://api.icicidirect.com/apiuser/home
Once registered you will get an api key and secret
Importing required libraries:
import datetime as dt
import pandas as pd
from ICICI_login import icici_login
import re
import plotly.graph_objects as go
In order to login and access the icici_direct api i have created a separate file named as: ICICI_login.py the contents of that file are as below:
from breeze_connect import BreezeConnect
def icici_login():
isec = BreezeConnect(api_key="PUT YOUR API KEY HERE")
isec.generate_session(api_secret="PUT YOUR API SECRET", session_token="PUT SESSION TOKEN")
return isec
isec = icici_login()
The session token needs to be updated every day in this file. It has to be stored in the same folder as the main file that we’re working on.
Coming back to out main file:
For this example we will take finnifty call option with strike price 19200 and expiration dated 20th Dec 2022.
Giving inputs for the option that we need to analyze:
stock = 'NIFFIN' # Put stock/index code here
expiry_date = "2022-12-20T18:00:00.000Z"
right = 'call' # call or put as input
strike_price = 19200
time_interval = '5minute'
from_d = '2022-12-20' # Enter from and to date for the data
to_d = '2022-12-20'
Then in order to fetch the option data we will have to use this code:
start_date = re.sub('YYYY-MM-DD', from_d, 'YYYY-MM-DDT09:30:00.000Z', flags=re.IGNORECASE)
end_date = re.sub('YYYY-MM-DD', to_d, 'YYYY-MM-DDT09:30:00.000Z', flags=re.IGNORECASE)
def option_data(symbol, e_date, put_call, strike):
global time_interval, start_date, end_date
data11 = isec.get_historical_data(interval=time_interval, from_date=start_date, to_date=end_date,
stock_code=symbol, exchange_code="NFO", product_type="options",
expiry_date=e_date,
right=put_call, strike_price=strike)
v1 = pd.DataFrame(data11["Success"])
v1['datetime'] = v1['datetime'].apply(lambda x: dt.datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))
v1['time'] = v1['datetime'].apply(lambda x: x.time())
v1 = v1[(v1.time >= dt.time(9, 30)) & (v1.time <= dt.time(15, 29))]
v1 = v1.reset_index(drop=True)
v1['close'] = v1['close'].astype(float)
v1['high'] = v1['high'].astype(float)
v1['low'] = v1['low'].astype(float)
return v1
data = option_data(stock, e_date=expiry_date, put_call=right, strike=strike_price)
At this point of time the option data is stored in a dataframe as below:

Now, we need to visualize this data in a candlestick chart. We will use simple plotly go function as below:
fig = go.Figure(data=[go.Candlestick(x=data['datetime'],
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close'])])
fig.update_layout(xaxis_rangeslider_visible=False)
fig.show()
Finally, we get our historical option chart in a new browser tab.

If you liked what i post here regularly then kindly subscribe and visit me at https://twitter.com/rahul_12gh
