How to get nifty weekly and monthly expiry "dates", and trading holidays dates using python?

How to get nifty weekly and monthly expiry “dates”, and trading holidays dates using python?

I know this is not an answer to your question. But sharing how i am handling the exact requirement of mine. What i do is to select the distinct expiry dates from http://public.fyers.in/sym_details/NSE_FO.csv. Setup a cron to clear old data and update the database comparing current date, so that it remains updated on each day.

I have another table for holidays, where the trading holidays are collected from nse website https://www.nseindia.com/resources/exchange-communication-holidays. Once in a year process.

You can use pandas_market_calendar module to get list of all holidays historical to current. Refer this https://github.com/rsheftel/pandas_market_calendars. Make sure to change market to BSE.

This doesn’t help in backtesting at all as it will give only current expiry dates

its not an actual metho but u can find the next thurday in caps with this code

from datetime import date
from datetime import timedelta
import calendar

today = date.today()
DayName = calendar.day_name[today.weekday()]

dayNumbered = {“Friday”: 6, “Saturday”:5, “Sunday”:4, “Monday”:3 ,“Tuesday”:2, “Wednesday”:1, “Thurday”:0}
ThursdayDate = today+timedelta(dayNumbered.get(DayName))
print(ThursdayDate)

print(str(ThursdayDate.strftime(“%d%b%Y”)).upper())

The expiry Date column in the csv file only shows last date on which the CSV is updated

instrument_days = (pd.to_datetime((instruments[‘year’] + instruments[‘mon’] + instruments[‘day’]),
format=‘%y%b%d’)).dt.date
expiry_day = (instrument_days[instrument_days >= date.today()]).head(1)
date_filter = ((pd.to_datetime(expiry_day)).strftime(‘%y-%b-%d’)).split(‘-’)
instruments = instruments[(instruments[‘year’] == date_filter[0]) &
(instruments[‘mon’] == date_filter[1]) & (instruments[‘day’] == date_filter[2])]

instrument_days = (pd.to_datetime((instruments[‘year’] + instruments[‘mon’] + instruments[‘day’]),
format=‘%y%b%d’)).dt.date
expiry_day = (instrument_days[instrument_days >= date.today()]).head(1)
date_filter = ((pd.to_datetime(expiry_day)).strftime(‘%y-%b-%d’)).split(‘-’)
instruments = instruments[(instruments[‘year’] == date_filter[0]) &
(instruments[‘mon’] == date_filter[1]) & (instruments[‘day’] == date_filter[2])]