def futname(name):
name=name.upper()
mydate = datetime.datetime.now()
end_of_month = datetime.datetime.today() + relativedelta(day=31)
last_thursday = end_of_month + relativedelta(weekday=TH(-1))
last_thursday = last_thursday.replace(hour=15,minute=20,second=0,microsecond=0)
last_friday_before_last_thursday=last_thursday+ relativedelta(weekday=FR(-1))
last_friday_before_last_thursday = last_friday_before_last_thursday.replace(hour=15,minute=20,second=0,microsecond=0)
if("NIFTY" not in name):
if(mydate>last_friday_before_last_thursday): mydate = (mydate.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)
if("NIFTY" in name):
#https://stackoverflow.com/questions/9724906/python-date-of-the-previous-month
# first = mydate.replace(day=1)
# mydate = first - datetime.timedelta(days=1)
#https://stackoverflow.com/questions/57353919/how-can-i-get-the-first-day-of-the-next-month-in-python
if(mydate>last_thursday): mydate = (mydate.replace(day=1) + datetime.timedelta(days=32)).replace(day=1)
name = name+mydate.strftime("%y")+mydate.strftime("%b")+"FUT"
return name.upper()
It will return next month’s futures scrip name after Last Thursday’s post 15:20. If it is stock, then it will show next month’s futures scrip name after Last Friday’s post 15:20 which comes before the Last Thursday i.e. expiry week.
Slight complex to hear but if you’re trader in NSE, You will know the pain.