|
| 1 | +from bs4 import BeautifulSoup |
| 2 | +import requests |
| 3 | +import pandas as pd |
| 4 | +import time |
| 5 | + |
| 6 | +url = str(input("Enter URL: ")) |
| 7 | +headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' |
| 8 | + 'Chrome/80.0.3987.149 Safari/537.36', |
| 9 | + 'accept-language': 'en,gu;q=0.9,hi;q=0.8', 'accept-encoding': 'gzip, deflate, br'} |
| 10 | +sp = str(input("Enter Strike Price: ")) |
| 11 | + |
| 12 | +previous_time = "" |
| 13 | +seconds = 30 |
| 14 | + |
| 15 | +while True: |
| 16 | + try: |
| 17 | + response = requests.get(url, headers=headers) |
| 18 | + except: |
| 19 | + time.sleep(seconds) |
| 20 | + continue |
| 21 | + |
| 22 | + html_content = BeautifulSoup(response.content, "html.parser") |
| 23 | + my_time = (html_content.findAll('span')[1]).text.split(" ")[5] |
| 24 | + if previous_time == "": |
| 25 | + previous_time = my_time |
| 26 | + elif previous_time != my_time: |
| 27 | + previous_time = my_time |
| 28 | + else: |
| 29 | + time.sleep(seconds) |
| 30 | + continue |
| 31 | + |
| 32 | + print("------------------------------------------------------------------------------------------------------", |
| 33 | + end='') |
| 34 | + print("-----------------------------------------------------") |
| 35 | + |
| 36 | + my_table = html_content.find('table', {'id': 'octable'}) |
| 37 | + links = my_table.findAll('th') |
| 38 | + rows = my_table.find_all('tr') |
| 39 | + my_points = (html_content.findAll('b')[0]).text.split(" ")[1] |
| 40 | + data = [] |
| 41 | + for row in rows: |
| 42 | + cols = row.find_all('td') |
| 43 | + cols = [ele.text.strip() for ele in cols] |
| 44 | + data.append([ele for ele in cols if ele]) |
| 45 | + |
| 46 | + long_head_row = [] |
| 47 | + for i in range(4, 25): |
| 48 | + for link in links: |
| 49 | + long_head_row.append(links[i].get('title')) |
| 50 | + break |
| 51 | + |
| 52 | + df = pd.DataFrame() |
| 53 | + pd.set_option('display.max_rows', None) |
| 54 | + pd.set_option('display.max_columns', None) |
| 55 | + pd.set_option('display.width', 320) |
| 56 | + y = 0 |
| 57 | + for i in range(2, (len(data) - 1)): |
| 58 | + df[str(y)] = data[i] |
| 59 | + y += 1 |
| 60 | + df = df.transpose() |
| 61 | + df.columns = long_head_row |
| 62 | + # print(df, "\n") |
| 63 | + print(my_time, end=' ') |
| 64 | + print(my_points, end=' ') |
| 65 | + |
| 66 | + call_oi_list = [] |
| 67 | + for i in range(len(df)): |
| 68 | + if df.iloc[i, [0]][0] == "-": |
| 69 | + int_call_oi = 0 |
| 70 | + else: |
| 71 | + int_call_oi = int(df.iloc[i, [0]][0].replace(',', '')) |
| 72 | + call_oi_list.append(int_call_oi) |
| 73 | + call_oi_index = call_oi_list.index(max(call_oi_list)) |
| 74 | + max_call_oi = round(max(call_oi_list) / 100000, 1) |
| 75 | + |
| 76 | + put_oi_list = [] |
| 77 | + for i in range(len(df)): |
| 78 | + if df.iloc[i, [20]][0] == "-": |
| 79 | + int_put_oi = 0 |
| 80 | + else: |
| 81 | + int_put_oi = int(df.iloc[i, [20]][0].replace(',', '')) |
| 82 | + put_oi_list.append(int_put_oi) |
| 83 | + put_oi_index = put_oi_list.index(max(put_oi_list)) |
| 84 | + max_put_oi = round(max(put_oi_list) / 100000, 1) |
| 85 | + total_call_oi = sum(call_oi_list) |
| 86 | + total_put_oi = sum(put_oi_list) |
| 87 | + try: |
| 88 | + put_call_ratio = round(total_put_oi / total_call_oi, 2) |
| 89 | + except ZeroDivisionError: |
| 90 | + put_call_ratio = 0 |
| 91 | + |
| 92 | + print(f"Max Call OI: {max_call_oi}, Strike Price: {df.iloc[call_oi_index]['Strike Price']} ", end='') |
| 93 | + print(f"Max Put OI: {max_put_oi}, Strike Price: {df.iloc[put_oi_index]['Strike Price']} ", end='') |
| 94 | + print(f"PCR: {put_call_ratio} ") |
| 95 | + |
| 96 | + index = int(df[df['Strike Price'] == f'{sp}'].index.tolist()[0]) |
| 97 | + a = df[['Change in Open Interest']][df['Strike Price'] == f'{sp}'] |
| 98 | + b1 = a.iloc[:, 0] |
| 99 | + c1 = b1.get(0, 'Change in Open Interest') |
| 100 | + b2 = df.iloc[:, 1] |
| 101 | + c2 = b2.get((index + 1), 'Change in Open Interest') |
| 102 | + b3 = df.iloc[:, 1] |
| 103 | + c3 = b3.get((index + 2), 'Change in Open Interest') |
| 104 | + try: |
| 105 | + c1 = int(c1.replace(',', '')) |
| 106 | + except ValueError: |
| 107 | + c1 = 0 |
| 108 | + try: |
| 109 | + c2 = int(c2.replace(',', '')) |
| 110 | + except ValueError: |
| 111 | + c2 = 0 |
| 112 | + try: |
| 113 | + c3 = int(c3.replace(',', '')) |
| 114 | + except ValueError: |
| 115 | + c3 = 0 |
| 116 | + call_sum = round((c1 + c2 + c3) / 100000, 1) |
| 117 | + call_boundary = round(c3 / 100000, 1) |
| 118 | + o1 = a.iloc[:, 1] |
| 119 | + p1 = o1.get(0, 'Change in Open Interest') |
| 120 | + o2 = df.iloc[:, 19] |
| 121 | + p2 = o2.get((index + 1), 'Change in Open Interest') |
| 122 | + p3 = o2.get((index + 2), 'Change in Open Interest') |
| 123 | + p4 = o2.get((index + 4), 'Change in Open Interest') |
| 124 | + o3 = df.iloc[:, 1] |
| 125 | + p5 = o3.get((index + 4), 'Change in Open Interest') |
| 126 | + p6 = o3.get((index - 2), 'Change in Open Interest') |
| 127 | + p7 = o2.get((index - 2), 'Change in Open Interest') |
| 128 | + try: |
| 129 | + p1 = int(p1.replace(',', '')) |
| 130 | + except ValueError: |
| 131 | + p1 = 0 |
| 132 | + try: |
| 133 | + p2 = int(p2.replace(',', '')) |
| 134 | + except ValueError: |
| 135 | + p2 = 0 |
| 136 | + try: |
| 137 | + p3 = int(p3.replace(',', '')) |
| 138 | + except ValueError: |
| 139 | + p3 = 0 |
| 140 | + try: |
| 141 | + p4 = int(p4.replace(',', '')) |
| 142 | + except ValueError: |
| 143 | + p4 = 0 |
| 144 | + try: |
| 145 | + p5 = int(p5.replace(',', '')) |
| 146 | + except ValueError: |
| 147 | + p5 = 0 |
| 148 | + try: |
| 149 | + p6 = int(p6.replace(',', '')) |
| 150 | + except ValueError: |
| 151 | + p6 = 0 |
| 152 | + try: |
| 153 | + p7 = int(p7.replace(',', '')) |
| 154 | + except ValueError: |
| 155 | + p7 = 0 |
| 156 | + |
| 157 | + put_sum = round((p1 + p2 + p3) / 100000, 1) |
| 158 | + put_boundary = round(p1 / 100000, 1) |
| 159 | + difference = round(call_sum - put_sum, 1) |
| 160 | + try: |
| 161 | + call_itm = round(p4 / p5, 1) |
| 162 | + except ZeroDivisionError: |
| 163 | + call_itm = 0 |
| 164 | + try: |
| 165 | + put_itm = round(p6 / p7, 1) |
| 166 | + except ZeroDivisionError: |
| 167 | + put_itm = 0 |
| 168 | + |
| 169 | + print(f"Call Sum: {call_sum} ", end='') |
| 170 | + print(f"Put Sum: {put_sum} ", end='') |
| 171 | + print(f"Difference: {difference} ", end='') |
| 172 | + print(f"Call Boundary: {call_boundary} ", end='') |
| 173 | + print(f"Put Boundary: {put_boundary} ", end='') |
| 174 | + print(f"Call ITM: {call_itm} ", end='') |
| 175 | + print(f"Put ITM: {put_itm} ") |
| 176 | + |
| 177 | + time.sleep(seconds) |
0 commit comments