@@ -183,11 +183,10 @@ def nse_quote_ltp(symbol,expiryDate="latest",optionType="-",strikePrice=0):
183183 #https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists
184184 #https://stackoverflow.com/questions/19199984/sort-a-list-in-python
185185 if (expiryDate == "latest" ) or (expiryDate == "next" ):
186- if (optionType != "Fut" ):
187- dates = list (set ((payload ["expiryDates" ])))
188- dates .sort (key = lambda date : datetime .datetime .strptime (date , '%d-%b-%Y' ))
189- expiryDate = dates [0 ]
190- if (expiryDate == "next" ):expiryDate = dates [1 ]
186+ dates = list (set ((payload ["expiryDates" ])))
187+ dates .sort (key = lambda date : datetime .datetime .strptime (date , '%d-%b-%Y' ))
188+ if (expiryDate == "latest" ): expiryDate = dates [0 ]
189+ if (expiryDate == "next" ): expiryDate = dates [1 ]
191190
192191 meta = "Options"
193192 if (optionType == "Fut" ): meta = "Futures"
@@ -198,26 +197,31 @@ def nse_quote_ltp(symbol,expiryDate="latest",optionType="-",strikePrice=0):
198197 for i in payload ['stocks' ]:
199198 if meta in i ['metadata' ]['instrumentType' ]:
200199 #print(i['metadata'])
201- if (expiryDate == "latest" ):
202- lastPrice = i ['metadata' ]['lastPrice' ]
203- break
204-
205- if (i ['metadata' ]["expiryDate" ]== expiryDate ):
206- if (i ['metadata' ]["optionType" ]== optionType ):
207- if (i ['metadata' ]["strikePrice" ]== strikePrice ):
208- #print(i['metadata'])
200+ if (optionType == "Fut" ):
201+ if (i ['metadata' ]['expiryDate' ]== expiryDate ):
209202 lastPrice = i ['metadata' ]['lastPrice' ]
210203
204+ if ((optionType == "Put" )or (optionType == "Call" )):
205+ if (i ['metadata' ]["expiryDate" ]== expiryDate ):
206+ if (i ['metadata' ]["optionType" ]== optionType ):
207+ if (i ['metadata' ]["strikePrice" ]== strikePrice ):
208+ #print(i['metadata'])
209+ lastPrice = i ['metadata' ]['lastPrice' ]
210+
211211 if (optionType == "-" ):
212212 lastPrice = payload ['underlyingValue' ]
213213
214214 return lastPrice
215215
216- # print(nse_quote_ltp("BANKNIFTY"))
217- # print(nse_quote_ltp("RELIANCE","latest","Fut"))
218- # print(nse_quote_ltp("RELIANCE"))
219- # print(nse_quote_ltp("BANKNIFTY","13-Aug-2020","PE",21000))
220- # print(nse_quote_ltp("RELIANCE","latest","PE",2300))
216+ # print(nse_quote_ltp("RELIANCE"))
217+ # print(nse_quote_ltp("RELIANCE","latest","Fut"))
218+ # print(nse_quote_ltp("RELIANCE","next","Fut"))
219+ # print(nse_quote_ltp("BANKNIFTY","latest","PE",32000))
220+ # print(nse_quote_ltp("BANKNIFTY","next","PE",32000))
221+ # print(nse_quote_ltp("BANKNIFTY","10-Jun-2021","PE",32000))
222+ # print(nse_quote_ltp("BANKNIFTY","17-Jun-2021","PE",32000))
223+ # print(nse_quote_ltp("RELIANCE","latest","PE",2300))
224+ # print(nse_quote_ltp("RELIANCE","next","PE",2300))
221225
222226def nse_optionchain_ltp (payload ,strikePrice ,optionType ,inp = 0 ,intent = "" ):
223227 expiryDate = payload ['records' ]['expiryDates' ][inp ]
@@ -293,11 +297,20 @@ def nse_past_results(symbol):
293297 symbol = nsesymbolpurify (symbol )
294298 return nsefetch ('https://www.nseindia.com/api/results-comparision?symbol=' + symbol )
295299
296- def expiry_list (symbol ):
300+ def expiry_list (symbol , type = "list" ):
297301 logging .info ("Getting Expiry List of: " + symbol )
298- payload = nse_optionchain_scrapper (symbol )
299- payload = pd .DataFrame ({'Date' :payload ['records' ]['expiryDates' ]})
300- return payload
302+
303+ if (type != "list" ):
304+ payload = nse_optionchain_scrapper (symbol )
305+ payload = pd .DataFrame ({'Date' :payload ['records' ]['expiryDates' ]})
306+ return payload
307+
308+ if (type == "list" ):
309+ payload = nse_quote (symbol )
310+ dates = list (set ((payload ["expiryDates" ])))
311+ dates .sort (key = lambda date : datetime .datetime .strptime (date , '%d-%b-%Y' ))
312+ return dates
313+
301314
302315def nse_custom_function_secfno (symbol ,attribute = "lastPrice" ):
303316 positions = nsefetch ('https://www.nseindia.com/api/equity-stockIndices?index=SECURITIES%20IN%20F%26O' )
0 commit comments