简体   繁体   中英

Is there a way to fetch historical nifty option data for particular strike price?

I am analyzing the nifty historical option data from specific dates to expiry dates for a particular strike price. Say, 25/11/2021 is Thursday (option expiry days are Thursdays) and I want to analyze data from 15/11/2021 which is a Monday for strike price on 18/11/2021 (Thursday). So the start date will be 15/11/2021, end date will be 25/11/2021, expiry date will be 18/11/2021 (Thursday) and strike price on 18/11/2021. I am able to do it manually but I am unable to do it in for loop for consecutive Thursdays. I have enclosed the code below. Is there any mistake in my code or is there a way to fetch options data for consecutive Thursdays?

for yr in year_list:
    for mnth in month_list:
        exp_dt= list (get_expiry_date(year=yr, month = mnth))
        exp_dt.sort()
        strike = nifty_data.loc[nifty_data['Date'].isin(exp_dt)]
        strikeprice = list (strike['Close'])
        for s, e in zip(strikeprice, exp_dt):
            prev_exp_dt = e - timedelta(days=10)
            expi = e - timedelta(days = 7)
            nifty_opt = get_history(symbol = 'NIFTY',
                                       start = prev_exp_dt, end = e,
                                       index = True, option_type = 'PE',
                                       strike_price = float(s),
                                       expiry_date = expi)
            option_data = option_data.append(nifty_opt)
            nifty_opt = get_history(symbol = 'NIFTY',
                                   start = prev_exp_dt, end = e,
                                   index = True, option_type = 'CE',
                                   strike_price = float(s),
                                   expiry_date = expi)
            
            option_data = option_data.append(nifty_opt)
        
   

I am getting empty dataframe for the above code.

Thank you fellas for taking time to looking into my post.

I got it: And below is the code:

for yr in yr_list:
    for mnth in month_list:
        exp_dt= list (get_expiry_date(year=yr, month = mnth))
        exp_dt.sort()
        strike = nifty_data.loc[nifty_data['Date'].isin(exp_dt)]
        strikeprice = list (strike['Strike'])
        for e in exp_dt:
            prev_exp_dt = e - timedelta(days =3)
            for s in strikeprice:
                nifty_opt = get_history(symbol = 'NIFTY',
                                           start = prev_exp_dt, end = e,
                                           index = True, option_type = 'PE',
                                           strike_price = float(s),
                                           expiry_date = e)
                option_data = option_data.append(nifty_opt)
                nifty_opt = get_history(symbol = 'NIFTY',
                                           start = prev_exp_dt, end = e,
                                           index = True, option_type = 'CE',
                                           strike_price = float(s),
                                           expiry_date = e)
                option_data = option_data.append(nifty_opt)

Thank you again! Hope it helps you too!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM