How can I identify the given date is weekly and monthly expiry for Nifty and banknifty in python

I am trying to create dataframe of ATM combined premiums for monthly and weekly expiries of NIFTY and banknifty.



I have hardcoded the expirty dates as of now. Though i dont know how can I find the expiries dynamically. That is totally different thread.



Based on the weekly or monthly expiry date, the pattern of instruments should be created. Since monthly and weekly patterns are different. Below is sample one

NIFTY2340617000CE -> Weekly 6th Apr 2023 expiry
NIFTY23APR17000CE -> Monthly Apr 2023 expiry


Though I have hardcoded my expirty date as like below and creating pattern of symbols based on monthly or weekly. Here i dont know how can I identify the give date is monthly or weekly.

expiry = datetime.date(2023, 4, 6)
if (find some logic to get true only for monthly)
instrument_name = 'NFO:' + instrument + current_year + current_month + str(strike)
ce_ltp = round((kite.ltp(instrument_name + 'CE')).get(instrument_name + 'CE', {}).get('last_price'), 0)
elif (find some logic to identify the given date is weekly)
 ... code for weekly pattern and get ltp...
Please provide some insight to classify the given date is weekly or monthly.

Hello Silambarasan,



You can determine whether the given date is a weekly or monthly expiry date as follows: 

first, check if the date is equal to the last Thursday of the month. If so, it is a monthly expiry date. Otherwise, check if the date is a Thursday. If it is, it is a weekly expiry date. However, note that there could be two contracts (i.e. monthly and weekly) that expire on the last Thursday of the month.



In addition to this, you should also check if the given date is a trading day or not. If it is not a trading day, the expiry date will be on the next trading day after the Thursday.



One way to approach this is to create two variables from the given date: variable_1 and variable_2. For example, if the given date is April 6th, 2023, you can create variable_1 as 23406 (to represent the weekly contract) and variable_2 as 23APR (to represent the monthly contract).



You can then query the LTP for the contracts using the following instrument names:



'NFO:' + instrument + variable_1 + str(strike)

and

'NFO:' + instrument + variable_2 + str(strike)



If the contract exists, the LTP will be returned by the code:

round((kite.ltp(instrument_name + 'CE')).get(instrument_name + 'CE', {}).get('last_price'), 0)

If the contract does not exist, the code will return 'None'.

So, if variable_1 returns an LTP, the given date is a weekly expiry and you have obtained the LTP for the contract. On the other hand, if variable_2 returns an LTP, the given date is a monthly expiry and you have obtained the LTP. If both variables return an LTP, then the given date is an expiry date for both monthly and weekly contracts. If both variables return 'None', the given date is not the expiry date for either weekly or monthly contracts.

I hope this helps! Let me know if you have any further questions.