Skip to content

Commit 1b4800f

Browse files
committed
Fix multiple errors due to NSE API changes
1 parent 16f58fd commit 1b4800f

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

NSE_Option_Chain_Analyzer.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ def __init__(self, window: Tk) -> None:
3939
self.dates: List[str] = [""]
4040
self.indices: List[str] = []
4141
self.stocks: List[str] = []
42+
self.expiry_date: str = ""
4243
self.url_oc: str = "https://www.nseindia.com/option-chain"
43-
self.url_index: str = "https://www.nseindia.com/api/option-chain-indices?symbol="
44-
self.url_stock: str = "https://www.nseindia.com/api/option-chain-equities?symbol="
44+
self.url_index: str = "https://www.nseindia.com/api/option-chain-contract-info?symbol="
45+
self.url_stock: str = "https://www.nseindia.com/api/option-chain-contract-info?symbol="
46+
self.url_index_data: str = "https://www.nseindia.com/api/option-chain-v3?type=Indices&symbol={}&expiry={}"
47+
self.url_stock_data: str = "https://www.nseindia.com/api/option-chain-v3?type=Equity&symbol={}&expiry={}"
4548
self.url_symbols: str = "https://www.nseindia.com/api/underlying-information"
4649
self.url_icon_png: str = "https://raw.githubusercontent.com/VarunS2002/" \
4750
"Python-NSE-Option-Chain-Analyzer/master/nse_logo.png"
@@ -332,6 +335,9 @@ def get_data_first_run(self) -> Optional[Tuple[Optional[requests.Response], Any]
332335
self.config_parser.write(f)
333336

334337
url: str = self.url_index + self.index if self.option_mode == 'Index' else self.url_stock + self.stock
338+
if self.expiry_date != "":
339+
url: str = self.url_index_data.format(self.index, self.expiry_date) if self.option_mode == 'Index' \
340+
else self.url_stock_data.format(self.stock, self.expiry_date)
335341
try:
336342
response = self.session.get(url, headers=self.headers, timeout=5, cookies=self.cookies)
337343
except Exception as err:
@@ -364,7 +370,9 @@ def get_data_first_run(self) -> Optional[Tuple[Optional[requests.Response], Any]
364370
print(err, sys.exc_info()[0], "3")
365371
return
366372
self.dates.clear()
367-
for dates in json_data['records']['expiryDates']:
373+
expiry_dates_list: List[str] = json_data['expiryDates'] if 'expiryDates' in json_data \
374+
else json_data['records']['expiryDates']
375+
for dates in expiry_dates_list:
368376
self.dates.append(dates)
369377
try:
370378
self.date_menu.config(values=tuple(self.dates))
@@ -377,7 +385,8 @@ def get_data_first_run(self) -> Optional[Tuple[Optional[requests.Response], Any]
377385
def get_data_refresh(self) -> Optional[Tuple[Optional[requests.Response], Any]]:
378386
request: Optional[requests.Response] = None
379387
response: Optional[requests.Response] = None
380-
url: str = self.url_index + self.index if self.option_mode == 'Index' else self.url_stock + self.stock
388+
url: str = self.url_index_data.format(self.index, self.expiry_date) if self.option_mode == 'Index' \
389+
else self.url_stock_data.format(self.stock, self.expiry_date)
381390
try:
382391
response = self.session.get(url, headers=self.headers, timeout=5, cookies=self.cookies)
383392
if response.status_code == 401:
@@ -1055,9 +1064,9 @@ def get_dataframe(self) -> Optional[Tuple[pandas.DataFrame, str, float]]:
10551064
df = df.transpose()
10561065

10571066
ce_values: List[dict] = [data['CE'] for data in json_data['records']['data'] if
1058-
"CE" in data and data['expiryDate'].lower() == self.expiry_date.lower()]
1067+
"CE" in data and data['expiryDates'].lower() == self.expiry_date.lower()]
10591068
pe_values: List[dict] = [data['PE'] for data in json_data['records']['data'] if
1060-
"PE" in data and data['expiryDate'].lower() == self.expiry_date.lower()]
1069+
"PE" in data and data['expiryDates'].lower() == self.expiry_date.lower()]
10611070
points: float = pe_values[0]['underlyingValue']
10621071
if points == 0:
10631072
for item in pe_values:
@@ -1073,10 +1082,11 @@ def get_dataframe(self) -> Optional[Tuple[pandas.DataFrame, str, float]]:
10731082
self.change_state()
10741083
return
10751084
columns_ce: List[str] = ['openInterest', 'changeinOpenInterest', 'totalTradedVolume', 'impliedVolatility',
1076-
'lastPrice',
1077-
'change', 'bidQty', 'bidprice', 'askPrice', 'askQty', 'strikePrice']
1078-
columns_pe: List[str] = ['strikePrice', 'bidQty', 'bidprice', 'askPrice', 'askQty', 'change', 'lastPrice',
1079-
'impliedVolatility', 'totalTradedVolume', 'changeinOpenInterest', 'openInterest']
1085+
'lastPrice', 'change', 'buyQuantity1', 'buyPrice1', 'sellPrice1', 'sellQuantity1',
1086+
'strikePrice']
1087+
columns_pe: List[str] = ['strikePrice', 'buyQuantity1', 'buyPrice1', 'sellPrice1', 'sellQuantity1', 'change',
1088+
'lastPrice', 'impliedVolatility', 'totalTradedVolume', 'changeinOpenInterest',
1089+
'openInterest']
10801090
ce_data_f = ce_data_f[columns_ce]
10811091
pe_data_f = pe_data_f[columns_pe]
10821092
merged_inner: pandas.DataFrame = pandas.merge(left=ce_data_f, right=pe_data_f, left_on='strikePrice',

0 commit comments

Comments
 (0)