Please check my code to fetch sum of change in CE and PE Vega from ATM to up till OTM

Hi, Here is my code to get sum of change in CE and PE vega of nifty, BN and Sensex. Can someone check it and confirm if it is good to go or some amendment is required to fetch date bit faster
"import pdb
import time
import datetime
import traceback
from Dhan_Tradehull import Tradehull
import pandas as pd
from pprint import pprint
import talib
import pandas_ta as ta
import xlwings as xw

client_code = “1100889055”
token_id = “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzM2NTA1OTQ4LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwMDg4OTA1NSJ9.8OYDUlPPzCh47H7Ps49aPtOjntzy6UpE765pQYJCMTTFGz88w3Eu4LkalchSnCeH5HCVZsWeIsTQKn4TcaTd7Q”
tsl = Tradehull(client_code, token_id)

book = xw.Book(‘Live Nifty Option chain.xlsx’)
sheet = book.sheets[‘Nifty Option chain’]
sheet1 = book.sheets[‘BN Option Chain’]
sheet2 = book.sheets[‘Sensex OC’]
sheet3 = book.sheets[‘Analysis’]

while True:
current_time = datetime.datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)

# Fetch Option chains for NIFTY, BANKNIFTY, and SENSEX
nifty_option_chain = tsl.get_option_chain(Underlying="NIFTY", exchange="INDEX", expiry=0)
bn_option_chain = tsl.get_option_chain(Underlying="BANKNIFTY", exchange="INDEX", expiry=0)
sensex_option_chain = tsl.get_option_chain(Underlying="SENSEX", exchange="INDEX", expiry=0)

# Display in Excel sheets (for debugging or tracking)
sheet.range('A1').value = nifty_option_chain
sheet1.range('A1').value = bn_option_chain
sheet2.range('A1').value = sensex_option_chain

time.sleep(1)
print(f"Updated option chain {current_time}")

# Get ATM strikes for each underlying
_, _, atm_strike_nifty = tsl.ATM_Strike_Selection(Underlying='NIFTY', Expiry=0)
_, _, atm_strike_bn = tsl.ATM_Strike_Selection(Underlying='BANKNIFTY', Expiry=0)
_, _, atm_strike_sensex = tsl.ATM_Strike_Selection(Underlying='SENSEX', Expiry=0)

# Initialize Vega sums for each
total_ce_vega_change_nifty = 0
total_pe_vega_change_nifty = 0
total_ce_vega_change_bn = 0
total_pe_vega_change_bn = 0
total_ce_vega_change_sensex = 0
total_pe_vega_change_sensex = 0

# Function to process option chain
def process_option_chain(option_chain, atm_strike, total_ce_vega_change, total_pe_vega_change):
    for row in option_chain:
        if isinstance(row, dict):
            strike = int(row['strikePrice'])  # Access the strike price as expected
            vega_change = 0.0  # Default value for vega_change
            if 'vega' in row and 'lastPrice' in row:
                try:
                    vega = float(row['vega'])
                    ltp = float(row['lastPrice'])
                    vega_change = vega * ltp
                except ValueError:
                    vega_change = 0

            if row['optionType'] == 'CE' and strike >= atm_strike:
                total_ce_vega_change += vega_change
            elif row['optionType'] == 'PE' and strike <= atm_strike:
                total_pe_vega_change += vega_change
        else:
            print("Skipping invalid row:", row)

    return total_ce_vega_change, total_pe_vega_change

# Process each option chain
total_ce_vega_change_nifty, total_pe_vega_change_nifty = process_option_chain(nifty_option_chain, atm_strike_nifty, total_ce_vega_change_nifty, total_pe_vega_change_nifty)
total_ce_vega_change_bn, total_pe_vega_change_bn = process_option_chain(bn_option_chain, atm_strike_bn, total_ce_vega_change_bn, total_pe_vega_change_bn)
total_ce_vega_change_sensex, total_pe_vega_change_sensex = process_option_chain(sensex_option_chain, atm_strike_sensex, total_ce_vega_change_sensex, total_pe_vega_change_sensex)

# Write results to Sheet3
sheet3.range('A1').value = 'Time'
sheet3.range('B1').value = 'Nifty CE Vega Change'
sheet3.range('C1').value = 'Nifty PE Vega Change'
sheet3.range('D1').value = 'BN CE Vega Change'
sheet3.range('E1').value = 'BN PE Vega Change'
sheet3.range('F1').value = 'Sensex CE Vega Change'
sheet3.range('G1').value = 'Sensex PE Vega Change'
sheet3.range('J1').value='CE vega'
sheet3.range('K1').value='PE vega'
sheet3.range('I2').value = 'Nifty'
sheet3.range('I3').value = 'BN'
sheet3.range('I4').value = 'Sensex'

row = 2
while sheet3.range(f'A{row}').value is not None:
    row += 1

# Write current values to Sheet3
sheet3.range(f'A{row}').value = current_time
sheet3.range(f'B{row}').value = total_ce_vega_change_nifty
sheet3.range(f'C{row}').value = total_pe_vega_change_nifty
sheet3.range(f'D{row}').value = total_ce_vega_change_bn
sheet3.range(f'E{row}').value = total_pe_vega_change_bn
sheet3.range(f'F{row}').value = total_ce_vega_change_sensex
sheet3.range(f'G{row}').value = total_pe_vega_change_sensex
sheet3.range('J' + str(2)).value = sheet3.range('B' + str(2)).value - total_ce_vega_change_nifty
sheet3.range('K' + str(2)).value = sheet3.range('C' + str(2)).value - total_pe_vega_change_nifty
sheet3.range('J' + str(3)).value = sheet3.range('D' + str(2)).value - total_ce_vega_change_bn
sheet3.range('K' + str(3)).value = sheet3.range('E' + str(2)).value - total_pe_vega_change_bn
sheet3.range('J' + str(4)).value = sheet3.range('F' + str(2)).value - total_ce_vega_change_sensex
sheet3.range('K' + str(4)).value = sheet3.range('G' + str(2)).value - total_pe_vega_change_sensex
time.sleep(0)  # Sleep to prevent overloading the system

Please let me know of the changes.

Hello @Sobhit , Its looking good only. You can remove and try without time.sleep. The limits handling already inplace by default. So please try it once without time.sleep for checking better execution.