Im trying to fetch option chain to excel via DhanAPI, can someone help in this regard.
from dhanhq import dhanhq
import os
import xlwings as xw
import pandas as pd
import numpy as np
import time
import dateutil.parser
import threading
clientID = “—”
accessToken = “—”
dhan = dhanhq(clientID, accessToken)
dhan.get_fund_limits()
if not os.path.exists(“dhan.xlsx”):
try:
wb = xw.book()
wb.sheets.add(“OptionChain”)
wb.save(“dhan.xlsx”)
wb.close()
except Exception as e:
print(f"error creating file : {e}")
wb = xw.Book (“dhan.xlsx”)
oc = wb.sheets(“OptionChain”)
oc.range(“a:b”).value = oc.range(“d6:e20”).value = oc.range(“g1:v4000”).value = None
exchange = None
while True:
if exchange is None:
try:
exchange = pd.DataFrame(dhan.instruments(“NFO”))
exchange = exchange[exchange[“segment”]==“NFO-OPT”]
break
except:
print(“Exchange download error”)
time.sleep(10)