import sys
from Dhan_Tradehull import Tradehull
import datetime
import time
import pandas as pd
from Dhan_Tradehull import Tradehull
import ssl
import datetime as dt
from pprint import pprint
import xlwings as xw
Credentials
API_KEY = “1106293837”
ACCESS_TOKEN = “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzUxNTMzODUwLCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwNjI5MzgzNyJ9.A3u_rVoGq2z1iBrYdOke-EJUCALiwScsK6DOViEnEKQB8-FnvGQ6gP0by7E0gQWc_pIUDcLwUiYO9LADcGE69Q”
Initialize Tradehull
tsl = Tradehull(API_KEY, ACCESS_TOKEN)
Open the workbook
try:
wb = xw.Book(‘nifty_data.xlsx’)
except Exception as e:
print(f"Error opening workbook: {e}")
sys.exit(1)
Ensure sheet2 exists
try:
sheet2 = wb.sheets[‘sheet2’]
except Exception:
sheet2 = wb.sheets.add(‘sheet2’) # Create sheet2 if it doesn’t exist
Ensure sheet1 exists
try:
sheet1 = wb.sheets[‘sheet1’]
except Exception:
sheet1 = wb.sheets.add(‘sheet1’) # Create sheet1 if it doesn’t exist
while True:
# Fetch Nifty Cash Data
try:
nifty_cash_data = tsl.get_intraday_data(‘NIFTY’,‘INDEX’,5)
sheet2.range(‘A1’).value = nifty_cash_data # Write to sheet2 cell A1
print(“Nifty Cash Data:”, nifty_cash_data)
except Exception as e:
print(f"Error fetching Nifty Cash data: {e}")
# Fetch Nifty Futures Data
try:
nifty_futures_data = tsl.get_intraday_data('NIFTYFUTURE','NFO', 'FUTIDX',5)
sheet2.range('k1').value = nifty_futures_data # Write to sheet2 cell A15
print("Nifty Futures Data:", nifty_futures_data)
except Exception as e:
print(f"Error fetching Nifty Futures data: {e}")
# Fetch Option Chain Data
try:
atm_strikes, option_chain = tsl.get_option_chain("NIFTY", "INDEX", expiry=0, num_strikes=5)
sheet1.range('B1').value = option_chain # Write to sheet1 cell B15
print("Option Chain Data:", option_chain)
except Exception as e:
print(f"Error fetching Option Chain data: {e}")
# Fetch Option Chain Data next expiry
try:
atm_strikes, option_chain = tsl.get_option_chain("NIFTY", "INDEX", expiry=1, num_strikes=5)
sheet1.range('B15').value = option_chain # Write to sheet1 cell B15
print("Option Chain Data:", option_chain)
except Exception as e:
print(f"Error fetching Option Chain data: {e}")
# Save Excel File
try:
wb.save('nifty_data.xlsx')
except Exception as e:
print(f"Error saving workbook: {e}")
time.sleep(300) # Wait for 1 minute before the next iteration @Tradehull_Imran sir iwant fatch nifty futuredata what should write code ,nifty 24800ce,pe
strong text