Nifty BankNifty Current Weekly Symbols

For weekly like 3rd September’s weekly, Index options shows symbol as BANKNIFTY2090321000PE.
For monthly it will show as BANKNIFTY20AUG22100PE

Lets make the command “Sell bn 22000CE” that sells BankNIFTY of current expiry automatic. This is the code that I use to get that middle string.

stuff = [['27-Aug-2020', '20AUG'], ['03-Sep-2020', '20903'], ['10-Sep-2020', '20910'], ['17-Sep-2020', '20917'], ['24-Sep-2020', '20SEP'], ['01-Oct-2020', '20O01'], ['08-Oct-2020', '20O08'], ['15-Oct-2020', '20O15'], ['22-Oct-2020', '20O22'], ['29-Oct-2020', '20OCT']]

import datetime

run_time=datetime.datetime.now()

for x in range(0,len(stuff)):

    #Convertion to Datetime Object
    stuff[x][0] = datetime.datetime.strptime(stuff[x][0], '%d-%b-%Y')

    #Adding DTE
    stuff[x].append((run_time-stuff[x][0]).days)

    #Getting Expiry
    if((run_time-stuff[x][0]).days<=0):
        weekly_dict=x
        break

def intraday_end():
    start_now=datetime.datetime.now().replace(hour=15, minute=20, second=0, microsecond=0)
    return start_now<datetime.datetime.now()

if((run_time-stuff[weekly_dict][0]).days==0):
    if(intraday_end()==True):
        weekly_dict =weekly_dict+1

print(stuff[weekly_dict][1])

This will give output as ‘20AUG’ as it is current expiry.

This will give output as ‘20903’ from expiry day’s 15:20 to next expiry day’s 15:20.

Feel free to refactor the code. (Refactor means beautify/shorten/reduce runtime… whatever :slight_smile: )