Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
@@ -18,6 +18,7 @@ class StockDataFetcher:
|
|
18 |
self.search_url = "https://groww.in/v1/api/search/v1/entity"
|
19 |
self.news_url = "https://groww.in/v1/api/stocks_company_master/v1/company_news/groww_contract_id/"
|
20 |
self.all_stocks_url = "https://groww.in/v1/api/stocks_data/v1/all_stocks"
|
|
|
21 |
|
22 |
self.indian_timezone = pytz.timezone('Asia/Kolkata')
|
23 |
self.utc_timezone = pytz.timezone('UTC')
|
@@ -82,9 +83,19 @@ class StockDataFetcher:
|
|
82 |
print(f"An error occurred: {e}")
|
83 |
return None
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
def fetch_option_chain(self, symbol):
|
|
|
86 |
if symbol.upper() == "BANKNIFTY":
|
87 |
symbol = "nifty-bank"
|
|
|
88 |
response = requests.get(self.option_chain + symbol, headers=self.headers)
|
89 |
data = response.json()['optionChain']['optionChains']
|
90 |
ltp = response.json()['livePrice']['value']
|
@@ -102,7 +113,6 @@ class StockDataFetcher:
|
|
102 |
|
103 |
chain = pd.DataFrame(chain)
|
104 |
index = chain[(chain['strikePrice'] >= ltp)].head(1).index[0]
|
105 |
-
print(response.json()['livePrice'])
|
106 |
chain = chain[index-6:index+7].reset_index(drop=True)
|
107 |
optin_exp = chain['Symbol_CE'][0][:-7]
|
108 |
return chain, optin_exp, index_ltp
|
@@ -122,7 +132,6 @@ class StockDataFetcher:
|
|
122 |
entity = data['content'][0]
|
123 |
return {"ID": entity['id'], "title": entity['title'], "NSE_Symbol": entity['nse_scrip_code'], "contract_id" : entity["groww_contract_id"]}
|
124 |
except requests.exceptions.RequestException as e:
|
125 |
-
print(f"Error during API request: {e}")
|
126 |
return None
|
127 |
|
128 |
def fetch_stock_news(self, symbol, page=1, size=1):
|
@@ -133,7 +142,6 @@ class StockDataFetcher:
|
|
133 |
try:
|
134 |
symbol_id = self.search_entity(symbol.upper())['contract_id']
|
135 |
response = requests.get(self.news_url + symbol_id, headers=self.headers, params=params).json()['results']
|
136 |
-
print(response)
|
137 |
news = []
|
138 |
for i in range(len(response)):
|
139 |
Title = response[i]['title']
|
@@ -161,7 +169,6 @@ class StockDataFetcher:
|
|
161 |
news_table = pd.DataFrame(news)
|
162 |
return news_table
|
163 |
except:
|
164 |
-
print("Something went wrong")
|
165 |
return None
|
166 |
|
167 |
def fetch_all_stock(self):
|
@@ -206,7 +213,7 @@ class StockDataFetcher:
|
|
206 |
index_symbol = "NIFTY"
|
207 |
rounding_value = 50
|
208 |
|
209 |
-
elif symbol.upper() == "
|
210 |
index_symbol = "BANKNIFTY"
|
211 |
rounding_value = 100
|
212 |
|
@@ -214,7 +221,7 @@ class StockDataFetcher:
|
|
214 |
pass
|
215 |
|
216 |
stock_data = self.fetch_stock_data(index_symbol, intervals, days)
|
217 |
-
chain, exp = self.fetch_option_chain(symbol.upper())
|
218 |
stock_data['RSI'] = ta.momentum.rsi(stock_data['Close'], window=14)
|
219 |
stock_data = stock_data.drop(columns=['Volume'])
|
220 |
stock_data['Prev_RSI'] = stock_data['RSI'].shift(1)
|
|
|
18 |
self.search_url = "https://groww.in/v1/api/search/v1/entity"
|
19 |
self.news_url = "https://groww.in/v1/api/stocks_company_master/v1/company_news/groww_contract_id/"
|
20 |
self.all_stocks_url = "https://groww.in/v1/api/stocks_data/v1/all_stocks"
|
21 |
+
self.nearest_expiries = "https://groww.in/v1/api/stocks_fo_data/v1/nearest_expiries?instrumentType=INDEX"
|
22 |
|
23 |
self.indian_timezone = pytz.timezone('Asia/Kolkata')
|
24 |
self.utc_timezone = pytz.timezone('UTC')
|
|
|
83 |
print(f"An error occurred: {e}")
|
84 |
return None
|
85 |
|
86 |
+
def fetch_nearest_expiries(self):
|
87 |
+
try:
|
88 |
+
response = requests.get(self.nearest_expiries, headers=self.headers).json()
|
89 |
+
return response
|
90 |
+
except Exception as e:
|
91 |
+
print(f"An error occurred: {e}")
|
92 |
+
return None
|
93 |
+
|
94 |
def fetch_option_chain(self, symbol):
|
95 |
+
|
96 |
if symbol.upper() == "BANKNIFTY":
|
97 |
symbol = "nifty-bank"
|
98 |
+
|
99 |
response = requests.get(self.option_chain + symbol, headers=self.headers)
|
100 |
data = response.json()['optionChain']['optionChains']
|
101 |
ltp = response.json()['livePrice']['value']
|
|
|
113 |
|
114 |
chain = pd.DataFrame(chain)
|
115 |
index = chain[(chain['strikePrice'] >= ltp)].head(1).index[0]
|
|
|
116 |
chain = chain[index-6:index+7].reset_index(drop=True)
|
117 |
optin_exp = chain['Symbol_CE'][0][:-7]
|
118 |
return chain, optin_exp, index_ltp
|
|
|
132 |
entity = data['content'][0]
|
133 |
return {"ID": entity['id'], "title": entity['title'], "NSE_Symbol": entity['nse_scrip_code'], "contract_id" : entity["groww_contract_id"]}
|
134 |
except requests.exceptions.RequestException as e:
|
|
|
135 |
return None
|
136 |
|
137 |
def fetch_stock_news(self, symbol, page=1, size=1):
|
|
|
142 |
try:
|
143 |
symbol_id = self.search_entity(symbol.upper())['contract_id']
|
144 |
response = requests.get(self.news_url + symbol_id, headers=self.headers, params=params).json()['results']
|
|
|
145 |
news = []
|
146 |
for i in range(len(response)):
|
147 |
Title = response[i]['title']
|
|
|
169 |
news_table = pd.DataFrame(news)
|
170 |
return news_table
|
171 |
except:
|
|
|
172 |
return None
|
173 |
|
174 |
def fetch_all_stock(self):
|
|
|
213 |
index_symbol = "NIFTY"
|
214 |
rounding_value = 50
|
215 |
|
216 |
+
elif symbol.upper() == "BANKNIFTY":
|
217 |
index_symbol = "BANKNIFTY"
|
218 |
rounding_value = 100
|
219 |
|
|
|
221 |
pass
|
222 |
|
223 |
stock_data = self.fetch_stock_data(index_symbol, intervals, days)
|
224 |
+
chain, exp, index_ltp = self.fetch_option_chain(symbol.upper())
|
225 |
stock_data['RSI'] = ta.momentum.rsi(stock_data['Close'], window=14)
|
226 |
stock_data = stock_data.drop(columns=['Volume'])
|
227 |
stock_data['Prev_RSI'] = stock_data['RSI'].shift(1)
|