eshan6704 commited on
Commit
6d8dc29
·
verified ·
1 Parent(s): 5266f30

Update nsepython.py

Browse files
Files changed (1) hide show
  1. nsepython.py +77 -38
nsepython.py CHANGED
@@ -181,65 +181,104 @@ def nsetools_get_quote(symbol):
181
  def nse_index():
182
  p=nsefetch('https://iislliveblob.niftyindices.com/jsonfiles/LiveIndicesWatch.json')
183
  return pd.DataFrame(p['data'])
184
-
185
- import json
186
  import requests
 
187
  import pandas as pd
188
 
189
- def index_history(sym, sd, ed):
190
- sd = sd.replace("-", "")
191
- ed = ed.replace("-", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
- payload = {
194
- "cinfo": f"{{'name':'{sym}','startDate':'{sd}','endDate':'{ed}','indexName':'{sym}'}}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
  }
196
 
197
- r = requests.post(
198
- "https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString",
199
  headers=niftyindices_headers,
200
- json=payload
201
- )
202
-
203
- p = json.loads(r.json())
204
- print(p)
205
- return pd.DataFrame.from_records(p)
206
 
207
 
208
- def index_pe_pb_div(sym, sd, ed):
209
- sd = sd.replace("-", "")
210
- ed = ed.replace("-", "")
211
 
212
- payload = {
213
- "cinfo": f"{{'name':'{sym}','startDate':'{sd}','endDate':'{ed}','indexName':'{sym}'}}"
 
 
 
214
  }
215
 
216
- r = requests.post(
217
- "https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString",
218
  headers=niftyindices_headers,
219
- json=payload
220
- )
221
 
222
- p = json.loads(r.json()["d"])
223
- return pd.DataFrame.from_records(p)
224
 
225
 
226
- def index_total_returns(sym, sd, ed):
227
- sd = sd.replace("-", "")
228
- ed = ed.replace("-", "")
229
 
230
- payload = {
231
- "cinfo": f"{{'name':'{sym}','startDate':'{sd}','endDate':'{ed}','indexName':'{sym}'}}"
 
 
 
232
  }
233
 
234
- r = requests.post(
235
- "https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString",
236
  headers=niftyindices_headers,
237
- json=payload
238
- )
239
-
240
- p = json.loads(r.json()["d"])
241
- return pd.DataFrame.from_records(p)
242
 
 
 
243
 
244
  def nse_bhavcopy(d): return pd.read_csv("https://archives.nseindia.com/products/content/sec_bhavdata_full_"+d.replace("-","")+".csv")
245
  def nse_bulkdeals(): return pd.read_csv("https://archives.nseindia.com/content/equities/bulk.csv")
 
181
  def nse_index():
182
  p=nsefetch('https://iislliveblob.niftyindices.com/jsonfiles/LiveIndicesWatch.json')
183
  return pd.DataFrame(p['data'])
 
 
184
  import requests
185
+ import json
186
  import pandas as pd
187
 
188
+ # ==============================
189
+ # Headers (DO NOT MODIFY)
190
+ # ==============================
191
+ niftyindices_headers = {
192
+ 'Connection': 'keep-alive',
193
+ 'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
194
+ 'Accept': 'application/json, text/javascript, */*; q=0.01',
195
+ 'DNT': '1',
196
+ 'X-Requested-With': 'XMLHttpRequest',
197
+ 'sec-ch-ua-mobile': '?0',
198
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36',
199
+ 'Content-Type': 'application/json; charset=UTF-8',
200
+ 'Origin': 'https://niftyindices.com',
201
+ 'Sec-Fetch-Site': 'same-origin',
202
+ 'Sec-Fetch-Mode': 'cors',
203
+ 'Sec-Fetch-Dest': 'empty',
204
+ 'Referer': 'https://niftyindices.com/reports/historical-data',
205
+ 'Accept-Language': 'en-US,en;q=0.9,hi;q=0.8',
206
+ }
207
 
208
+ # ==============================
209
+ # Internal helper (SAFE)
210
+ # ==============================
211
+ def _fmt_date(d):
212
+ """
213
+ Accepts: dd-mm-yyyy or ddmmyyyy
214
+ Returns: ddmmyyyy
215
+ """
216
+ return d.replace("-", "")
217
+
218
+ # ==============================
219
+ # Index APIs
220
+ # ==============================
221
+ def index_history(symbol, start_date, end_date):
222
+ start_date = _fmt_date(start_date)
223
+ end_date = _fmt_date(end_date)
224
+
225
+ data = {
226
+ 'cinfo': "{'name':'" + symbol +
227
+ "','startDate':'" + start_date +
228
+ "','endDate':'" + end_date +
229
+ "','indexName':'" + symbol + "'}"
230
  }
231
 
232
+ payload = requests.post(
233
+ 'https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString',
234
  headers=niftyindices_headers,
235
+ json=data
236
+ ).json()
237
+
238
+ payload = json.loads(payload["d"])
239
+ return pd.DataFrame.from_records(payload)
 
240
 
241
 
242
+ def index_pe_pb_div(symbol, start_date, end_date):
243
+ start_date = _fmt_date(start_date)
244
+ end_date = _fmt_date(end_date)
245
 
246
+ data = {
247
+ 'cinfo': "{'name':'" + symbol +
248
+ "','startDate':'" + start_date +
249
+ "','endDate':'" + end_date +
250
+ "','indexName':'" + symbol + "'}"
251
  }
252
 
253
+ payload = requests.post(
254
+ 'https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString',
255
  headers=niftyindices_headers,
256
+ json=data
257
+ ).json()
258
 
259
+ payload = json.loads(payload["d"])
260
+ return pd.DataFrame.from_records(payload)
261
 
262
 
263
+ def index_total_returns(symbol, start_date, end_date):
264
+ start_date = _fmt_date(start_date)
265
+ end_date = _fmt_date(end_date)
266
 
267
+ data = {
268
+ 'cinfo': "{'name':'" + symbol +
269
+ "','startDate':'" + start_date +
270
+ "','endDate':'" + end_date +
271
+ "','indexName':'" + symbol + "'}"
272
  }
273
 
274
+ payload = requests.post(
275
+ 'https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString',
276
  headers=niftyindices_headers,
277
+ json=data
278
+ ).json()
 
 
 
279
 
280
+ payload = json.loads(payload["d"])
281
+ return pd.DataFrame.from_records(payload)
282
 
283
  def nse_bhavcopy(d): return pd.read_csv("https://archives.nseindia.com/products/content/sec_bhavdata_full_"+d.replace("-","")+".csv")
284
  def nse_bulkdeals(): return pd.read_csv("https://archives.nseindia.com/content/equities/bulk.csv")