To generate Nifty weekly expiry dates in Python, list every scheduled expiry weekday of the year, check each date against the NSE trading holiday list, and move any date that falls on a holiday back to the previous trading day. In April 2023 I asked ChatGPT-4 to write this code for the Thursday expiry that applied then. Since 1 September 2025 NSE weekly contracts expire on Tuesday, so below you will find the original GPT-4 code and output, followed by an updated 2026 version that handles the Tuesday expiry, weekends and monthly expiries.
Key Takeaways
- Rule: the expiry day is the scheduled weekday; if it is a trading holiday, expiry moves to the previous trading day.
- In 2023 Nifty weekly options expired on Thursday. From 1 September 2025 NSE equity derivatives expire on Tuesday, and monthly contracts on the last Tuesday of the month.
- GPT-4 produced correct code on the first attempt in 2023, while GPT-3.5 kept ignoring the Thursday holidays.
- The updated code below takes the expiry weekday as a parameter and also skips weekends while shifting a holiday expiry.
- In 2026, six Nifty weekly expiries move from Tuesday to Monday because of holidays.
Nifty Weekly Expiry Rules (2023)
The National Stock Exchange of India (NSE) is one of the largest stock exchanges in the world, with Nifty being its flagship index. Nifty weekly options contracts have a significant impact on the Indian financial markets. These contracts expire on Thursdays, but if a holiday falls on a Thursday, the expiry date moves to the previous trading day. In this blog post, we will discuss how to generate Nifty weekly expiry dates for the year 2023 using Python, taking into account the list of holidays provided by the NSEIndia Portal.
Update (2026): the Thursday rule above applied until August 2025. NSE equity F&O contracts expiring on or after 1 September 2025 expire on Tuesday, with monthly contracts on the last Tuesday of the month (Moneylife report on the change). The same “move back to the previous trading day” rule still applies when Tuesday is a holiday.
Step 1: Get the NSE Holiday Calendar
Getting the NSE Holiday Calendar from the NSEIndia Portal. Copy the table content and prepare the following prompt.

Step 2: Prompt ChatGPT-4 to Write the Python Code
Now open ChatGPT and ensure that you have access to a ChatGPT Plus subscription to access the GPT-4 model.
Now copy the following prompt and paste in your chatgpt.
List of Holidays in 2023
Here is the detailed share market holiday list 2023-
SR. NO DATE DAY DESCRIPTION
1 26-Jan-2023 Thursday Republic Day
2 07-Mar-2023 Tuesday Holi
3 30-Mar-2023 Thursday Ram Navami
4 04-Apr-2023 Tuesday Mahavir Jayanti
5 07-Apr-2023 Friday Good Friday
6 14-Apr-2023 Friday Dr. Baba Saheb Ambedkar Jayanti
7 01-May-2023 Monday Maharashtra Day
8 28-Jun-2023 Wednesday Bakri Id
9 15-Aug-2023 Tuesday Independence Day
10 19-Sep-2023 Tuesday Ganesh Chaturthi
11 02-Oct-2023 Monday Mahatma Gandhi Jayanti
12 24-Oct-2023 Tuesday Dussehra
13 14-Nov-2023 Tuesday Diwali-Balipratipada
14 27-Nov-2023 Monday Gurunanak Jayanti
15 25-Dec-2023 Monday Christmas
Write a Python Code to Generate All Nifty Weekly Expiry Dates in a list for the year 2023.
Nifty expiry is usually on Thursday Also check if the expiry day is a holiday in NSE Exchange
if yes then use the previous trading day as an expiry day.
Generate the Expriy date list in DD-MM-YYYY format.
If thursday is a holiday then weekly expiry points to one day prior to the holiday
that needs to be factored in properly
Step 3: Python Code for Nifty Weekly Expiry Dates Generated by GPT-4
Here is the following Python Code generated by chatGPT. Tried the same prompt with GPT3.5 and the generated python code always ignored the Thursday holidays. But GPT4 recognized the context very well and generated the code at the very first attempt.
from datetime import datetime, timedelta
holidays = [
"26-01-2023",
"07-03-2023",
"30-03-2023",
"04-04-2023",
"07-04-2023",
"14-04-2023",
"01-05-2023",
"28-06-2023",
"15-08-2023",
"19-09-2023",
"02-10-2023",
"24-10-2023",
"14-11-2023",
"27-11-2023",
"25-12-2023",
]
holidays = [datetime.strptime(h, "%d-%m-%Y") for h in holidays]
def generate_expiry_dates(year):
expiry_dates = []
start_date = datetime(year, 1, 1)
end_date = datetime(year, 12, 31)
while start_date.weekday() != 3: # Find the first Thursday of the year
start_date += timedelta(days=1)
while start_date <= end_date:
if start_date not in holidays:
expiry_dates.append(start_date.strftime("%d-%m-%Y"))
else: # If Thursday is a holiday, use the previous trading day as expiry day
adjusted_expiry_date = start_date - timedelta(days=1)
while adjusted_expiry_date in holidays:
adjusted_expiry_date -= timedelta(days=1)
expiry_dates.append(adjusted_expiry_date.strftime("%d-%m-%Y"))
start_date += timedelta(weeks=1)
return expiry_dates
expiry_dates_list = generate_expiry_dates(2023)
print(expiry_dates_list)
Python Output
['05-01-2023', '12-01-2023', '19-01-2023', '25-01-2023', '02-02-2023', '09-02-2023', '16-02-2023',
'23-02-2023', '02-03-2023', '09-03-2023', '16-03-2023', '23-03-2023', '29-03-2023', '06-04-2023',
'13-04-2023', '20-04-2023', '27-04-2023', '04-05-2023', '11-05-2023', '18-05-2023', '25-05-2023',
'01-06-2023', '08-06-2023', '15-06-2023', '22-06-2023', '29-06-2023', '06-07-2023', '13-07-2023',
'20-07-2023', '27-07-2023', '03-08-2023', '10-08-2023', '17-08-2023', '24-08-2023', '31-08-2023',
'07-09-2023', '14-09-2023', '21-09-2023', '28-09-2023', '05-10-2023', '12-10-2023', '19-10-2023',
'26-10-2023', '02-11-2023', '09-11-2023', '16-11-2023', '23-11-2023', '30-11-2023', '07-12-2023',
'14-12-2023', '21-12-2023', '28-12-2023']
Check the output against the holiday list: 26 January 2023 (Thursday, Republic Day) correctly becomes 25-01-2023, and 30 March 2023 (Thursday, Ram Navami) becomes 29-03-2023.
Code Explanation
- We import the necessary libraries,
datetimeandtimedelta, for handling dates and time calculations. - We define the list of holidays and convert the holiday dates into datetime objects.
- We create a function called
generate_expiry_datesthat takes a year as input and generates the Nifty weekly expiry dates for that year. - Inside the function, we first find the first Thursday of the year and loop through all Thursdays until the end of the year.
- We check if the current Thursday is a holiday. If it’s not, we add it to the expiry_dates list. If it is, we find the previous trading day and add that to the list instead.
- Finally, we generate the expiry dates for 2023 and print the list.
Updated Version (2026): Tuesday Expiry With Holidays and Weekends
The GPT-4 code works for 2023, but it has two limits for reuse. The expiry weekday is fixed to Thursday, and when it steps back from a holiday it only checks the holiday list, not weekends. If a holiday ever fell on the first trading day of the week, the code could return a Sunday or Saturday. The version below fixes both, and adds a function for monthly expiries (last Tuesday of the month, shifted back for holidays).
The 2026 holiday list used here is the NSE equity derivatives list as published by brokers such as smallcase; always confirm it against the official NSE holiday page, because NSE can add special holidays during the year (15 January 2026 was one, for the Maharashtra municipal elections, as reported by Business Today).
from datetime import date, timedelta
# NSE trading holidays for 2026 (equity derivatives segment, weekdays only).
# Always confirm against the latest list on nseindia.com before using this in a live system.
holidays_2026 = {
date(2026, 1, 15), # Municipal Corporation elections, Maharashtra
date(2026, 1, 26), # Republic Day
date(2026, 3, 3), # Holi
date(2026, 3, 26), # Shri Ram Navami
date(2026, 3, 31), # Shri Mahavir Jayanti
date(2026, 4, 3), # Good Friday
date(2026, 4, 14), # Dr. Baba Saheb Ambedkar Jayanti
date(2026, 5, 1), # Maharashtra Day
date(2026, 5, 28), # Bakri Id
date(2026, 6, 26), # Muharram
date(2026, 9, 14), # Ganesh Chaturthi
date(2026, 10, 2), # Mahatma Gandhi Jayanti
date(2026, 10, 20), # Dussehra
date(2026, 11, 10), # Diwali Balipratipada
date(2026, 11, 24), # Prakash Gurpurb Sri Guru Nanak Dev
date(2026, 12, 25), # Christmas
}
TUESDAY = 1 # Monday = 0 ... Sunday = 6
def is_trading_day(d, holidays):
return d.weekday() < 5 and d not in holidays
def previous_trading_day(d, holidays):
while not is_trading_day(d, holidays):
d -= timedelta(days=1)
return d
def weekly_expiries(year, holidays, expiry_weekday=TUESDAY):
d = date(year, 1, 1)
while d.weekday() != expiry_weekday: # first scheduled expiry weekday of the year
d += timedelta(days=1)
expiries = []
while d.year == year:
expiries.append(previous_trading_day(d, holidays)) # shift back over holidays and weekends
d += timedelta(weeks=1)
return expiries
def monthly_expiries(year, holidays, expiry_weekday=TUESDAY):
out = []
for month in range(1, 13):
d = date(year + (month == 12), month % 12 + 1, 1) - timedelta(days=1) # last day of month
while d.weekday() != expiry_weekday: # last scheduled expiry weekday of the month
d -= timedelta(days=1)
out.append(previous_trading_day(d, holidays))
return out
weekly = weekly_expiries(2026, holidays_2026)
print(len(weekly), "weekly expiries")
print([d.strftime("%d-%m-%Y") for d in weekly])
print("Shifted because of a holiday:",
[d.strftime("%a %d-%m-%Y") for d in weekly if d.weekday() != TUESDAY])
print("Monthly:", [d.strftime("%d-%m-%Y") for d in monthly_expiries(2026, holidays_2026)])
Output: Nifty Expiry Dates 2026
52 weekly expiries
['06-01-2026', '13-01-2026', '20-01-2026', '27-01-2026', '03-02-2026', '10-02-2026', '17-02-2026', '24-02-2026', '02-03-2026', '10-03-2026', '17-03-2026', '24-03-2026', '30-03-2026', '07-04-2026', '13-04-2026', '21-04-2026', '28-04-2026', '05-05-2026', '12-05-2026', '19-05-2026', '26-05-2026', '02-06-2026', '09-06-2026', '16-06-2026', '23-06-2026', '30-06-2026', '07-07-2026', '14-07-2026', '21-07-2026', '28-07-2026', '04-08-2026', '11-08-2026', '18-08-2026', '25-08-2026', '01-09-2026', '08-09-2026', '15-09-2026', '22-09-2026', '29-09-2026', '06-10-2026', '13-10-2026', '19-10-2026', '27-10-2026', '03-11-2026', '09-11-2026', '17-11-2026', '23-11-2026', '01-12-2026', '08-12-2026', '15-12-2026', '22-12-2026', '29-12-2026']
Shifted because of a holiday: ['Mon 02-03-2026', 'Mon 30-03-2026', 'Mon 13-04-2026', 'Mon 19-10-2026', 'Mon 09-11-2026', 'Mon 23-11-2026']
Monthly: ['27-01-2026', '24-02-2026', '30-03-2026', '28-04-2026', '26-05-2026', '30-06-2026', '28-07-2026', '25-08-2026', '29-09-2026', '27-10-2026', '23-11-2026', '29-12-2026']
Six weekly expiries shift to Monday because the Tuesday is a trading holiday: Holi (3 March), Shri Mahavir Jayanti (31 March), Dr. Ambedkar Jayanti (14 April), Dussehra (20 October), Diwali Balipratipada (10 November) and Guru Nanak Jayanti (24 November). The 24 November shift also moves the November monthly expiry to 23-11-2026.
Common Mistakes When Generating Expiry Dates
- Using a stale holiday list: holidays change every year and NSE can declare extra ones at short notice. Load the list from a file you update, not from code you never revisit.
- Hard-coding the weekday: the switch from Thursday to Tuesday in September 2025 broke many scripts. Keep the weekday as a parameter.
- Mixing weekly and monthly logic: the monthly expiry is the last Tuesday of the month, which is also that week’s weekly expiry. Generate both separately and tag them.
- Assuming one rule for all exchanges: BSE contracts follow their own expiry day. Check the exchange before you reuse the function for Sensex options.
- Trusting AI output blindly: as the GPT-3.5 result showed, a model can produce code that runs but ignores a key rule. Always test it against known holiday dates.
If you trade on expiry day, read what happens when you trade ATM and OTM options on expiry day. For running such scripts inside a broker-agnostic automation setup, see how to fetch broker historical data using OpenAlgo.
Frequently Asked Questions
What is the Nifty weekly expiry day in 2026?
Tuesday. Since 1 September 2025, NSE equity derivatives expire on Tuesday. If Tuesday is a trading holiday, expiry moves to the previous trading day, which in 2026 is Monday in six weeks.
When did Nifty weekly expiry change from Thursday to Tuesday?
The change applied to contracts expiring on or after 1 September 2025. Before that, Nifty weekly options expired on Thursday.
What happens to Nifty expiry if Tuesday is a holiday?
The contract expires on the previous trading day. For example, Tuesday 3 March 2026 was a holiday for Holi, so that week’s expiry was Monday 2 March 2026.
Can ChatGPT generate expiry dates reliably?
It can write the code quickly, but you must give it the correct rules and holiday list and check the result. In this test GPT-4 got it right first time while GPT-3.5 ignored Thursday holidays.
Learn by watching: free videos every week
Market Profile, order flow, Python, AI and OpenAlgo tutorials on YouTube.

