Skip to content

Commit 9d31d62

Browse files
committed
2nd Highest Call and Put Open Interest is now calculated only between the OI boundary range (highest in the range)
1 parent 2fc0410 commit 9d31d62

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

NSE_Option_Chain_Analyzer.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -723,21 +723,52 @@ def main(self) -> None:
723723
for i in range(len(df)):
724724
int_call_oi: int = int(df.iloc[i, [0]][0])
725725
call_oi_list.append(int_call_oi)
726-
call_oi_list_sorted: List[int] = sorted(call_oi_list)
727-
call_oi_index: int = call_oi_list.index(call_oi_list_sorted[-1])
728-
call_oi_index_2: int = call_oi_list.index(call_oi_list_sorted[-2])
729-
self.max_call_oi: float = round(call_oi_list_sorted[-1] / 1000, 1)
730-
self.max_call_oi_2: float = round(call_oi_list_sorted[-2] / 1000, 1)
726+
call_oi_index: int = call_oi_list.index(max(call_oi_list))
727+
self.max_call_oi: float = round(max(call_oi_list) / 1000, 1)
728+
self.max_call_oi_sp: numpy.float64 = df.iloc[call_oi_index]['Strike Price']
731729

732730
put_oi_list: List[int] = []
733731
for i in range(len(df)):
734732
int_put_oi: int = int(df.iloc[i, [20]][0])
735733
put_oi_list.append(int_put_oi)
736-
put_oi_list_sorted: List[int] = sorted(put_oi_list)
737-
put_oi_index: int = put_oi_list.index(put_oi_list_sorted[-1])
738-
put_oi_index_2: int = put_oi_list.index(put_oi_list_sorted[-2])
739-
self.max_put_oi: float = round(put_oi_list_sorted[-1] / 1000, 1)
740-
self.max_put_oi_2: float = round(put_oi_list_sorted[-2] / 1000, 1)
734+
put_oi_index: int = put_oi_list.index(max(put_oi_list))
735+
self.max_put_oi: float = round(max(put_oi_list) / 1000, 1)
736+
self.max_put_oi_sp: numpy.float64 = df.iloc[put_oi_index]['Strike Price']
737+
738+
sp_range_list: List[numpy.float64] = []
739+
for i in range(put_oi_index, call_oi_index + 1):
740+
sp_range_list.append(df.iloc[i]['Strike Price'])
741+
742+
self.max_call_oi_2: float
743+
self.max_call_oi_sp_2: numpy.float64
744+
self.max_put_oi_2: float
745+
self.max_put_oi_sp_2: numpy.float64
746+
if self.max_call_oi_sp == self.max_put_oi_sp:
747+
self.max_call_oi_2 = self.max_call_oi
748+
self.max_call_oi_sp_2 = self.max_call_oi_sp
749+
self.max_put_oi_2 = self.max_put_oi
750+
self.max_put_oi_sp_2 = self.max_put_oi_sp
751+
elif len(sp_range_list) == 2:
752+
self.max_call_oi_2 = df[df['Strike Price'] == self.max_put_oi_sp].iloc[0, 0]
753+
self.max_call_oi_sp_2 = self.max_put_oi_sp
754+
self.max_put_oi_2 = df[df['Strike Price'] == self.max_call_oi_sp].iloc[0, 20]
755+
self.max_put_oi_sp_2 = self.max_call_oi_sp
756+
else:
757+
call_oi_list_2: List[int] = []
758+
for i in range(put_oi_index, call_oi_index):
759+
int_call_oi_2: int = int(df.iloc[i, [0]][0])
760+
call_oi_list_2.append(int_call_oi_2)
761+
call_oi_index_2: int = put_oi_index + call_oi_list_2.index(max(call_oi_list_2))
762+
self.max_call_oi_2 = round(max(call_oi_list_2) / 1000, 1)
763+
self.max_call_oi_sp_2 = df.iloc[call_oi_index_2]['Strike Price']
764+
765+
put_oi_list_2: List[int] = []
766+
for i in range(put_oi_index + 1, call_oi_index + 1):
767+
int_put_oi_2: int = int(df.iloc[i, [20]][0])
768+
put_oi_list_2.append(int_put_oi_2)
769+
put_oi_index_2: int = put_oi_index + 1 + put_oi_list_2.index(max(put_oi_list_2))
770+
self.max_put_oi_2 = round(max(put_oi_list_2) / 1000, 1)
771+
self.max_put_oi_sp_2 = df.iloc[put_oi_index_2]['Strike Price']
741772

742773
total_call_oi: int = sum(call_oi_list)
743774
total_put_oi: int = sum(put_oi_list)
@@ -747,11 +778,6 @@ def main(self) -> None:
747778
except ZeroDivisionError:
748779
self.put_call_ratio = 0
749780

750-
self.max_call_oi_sp: numpy.float64 = df.iloc[call_oi_index]['Strike Price']
751-
self.max_call_oi_sp_2: numpy.float64 = df.iloc[call_oi_index_2]['Strike Price']
752-
self.max_put_oi_sp: numpy.float64 = df.iloc[put_oi_index]['Strike Price']
753-
self.max_put_oi_sp_2: numpy.float64 = df.iloc[put_oi_index_2]['Strike Price']
754-
755781
try:
756782
index: int = int(df[df['Strike Price'] == self.sp].index.tolist()[0])
757783
except IndexError as err:

0 commit comments

Comments
 (0)