def nse_optionchain_ltp(payload,strikePrice,optionType,inp=0,intent=""):
expiry_dates = payload['records']['expiryDates']
expiry_dates = [datetime.datetime.strptime(date, "%d-%b-%Y") for date in expiry_dates]
expiry_dates = [date.strftime("%d-%b-%Y") for date in expiry_dates if date >= datetime.datetime.now()]
expiryDate=expiry_dates[inp]
for x in range(len(payload['records']['data'])):
if((payload['records']['data'][x]['strikePrice']==strikePrice) & (payload['records']['data'][x]['expiryDate']==expiryDate)):
if(intent==""): return payload['records']['data'][x][optionType]['lastPrice']
if(intent=="sell"): return payload['records']['data'][x][optionType]['bidprice']
if(intent=="buy"): return payload['records']['data'][x][optionType]['askPrice']
def nse_expirydetails(payload,i=0): #Can make problem. Use nse_expirydetails_by_symbol()
expiry_dates = payload['records']['expiryDates']
expiry_dates = [datetime.datetime.strptime(date, "%d-%b-%Y") for date in expiry_dates]
expiry_dates = [date.strftime("%d-%b-%Y") for date in expiry_dates if date >= datetime.datetime.now()]
currentExpiry=expiry_dates[i]
currentExpiry = datetime.datetime.strptime(currentExpiry,'%d-%b-%Y').date() # converting json datetime to alice datetime
date_today = run_time.strftime('%Y-%m-%d') # required to remove hh:mm:ss
date_today = datetime.datetime.strptime(date_today,'%Y-%m-%d').date()
dte = (currentExpiry - date_today).days
return currentExpiry,dte
def nse_quote_ltp(symbol,expiryDate="latest",optionType="-",strikePrice=0):
payload = nse_quote(symbol)
meta = "Options"
if(optionType=="Fut"): meta = "Futures"
if(optionType=="PE"):optionType="Put"
if(optionType=="CE"):optionType="Call"
if(expiryDate=="latest") or (expiryDate=="next"):
if(meta=="Futures"):
selected_key = next((key for key in payload["expiryDatesByInstrument"] if "futures" in key.lower()), None)
if(meta=="Options"):
selected_key = next((key for key in payload["expiryDatesByInstrument"] if "options" in key.lower()), None)
expiry_dates=payload["expiryDatesByInstrument"][selected_key]
expiry_dates = [datetime.datetime.strptime(date, "%d-%b-%Y") for date in expiry_dates]
expiry_dates = [date.strftime("%d-%b-%Y") for date in expiry_dates if date >= datetime.datetime.now()]
if(expiryDate=="latest"): expiryDate=expiry_dates[0]
if(expiryDate=="next"): expiryDate=expiry_dates[1]
if(optionType!="-"):
for i in payload['stocks']:
if meta in i['metadata']['instrumentType']:
#print(i['metadata'])
if(optionType=="Fut"):
if(i['metadata']['expiryDate']==expiryDate):
lastPrice = i['metadata']['lastPrice']
if((optionType=="Put")or(optionType=="Call")):
if (i['metadata']["expiryDate"]==expiryDate):
if (i['metadata']["optionType"]==optionType):
if (i['metadata']["strikePrice"]==strikePrice):
#print(i['metadata'])
lastPrice = i['metadata']['lastPrice']
if(optionType=="-"):
lastPrice = payload['underlyingValue']
return lastPrice
def nse_expirydetails_by_symbol(symbol,meta ="Futures",i=0):
payload = nse_quote(symbol)
if(meta=="Futures"):
selected_key = next((key for key in payload["expiryDatesByInstrument"] if "futures" in key.lower()), None)
if(meta=="Options"):
selected_key = next((key for key in payload["expiryDatesByInstrument"] if "options" in key.lower()), None)
expiry_dates=payload["expiryDatesByInstrument"][selected_key]
expiry_dates = [datetime.datetime.strptime(date, "%d-%b-%Y") for date in expiry_dates]
expiry_dates = [date.strftime("%d-%b-%Y") for date in expiry_dates if date >= datetime.datetime.now()]
currentExpiry=expiry_dates[i]
currentExpiry = datetime.datetime.strptime(currentExpiry,'%d-%b-%Y').date()
dte = (currentExpiry - datetime.datetime.now().date()).days
return currentExpiry,dte
Version 2.5 Updated with the above changes