Skip to content

Commit 8831c5e

Browse files
authored
Merge pull request #36 from devfinwiz/fix/nse
Modifications to fix two of the nse APIs
2 parents 1168a85 + ce3917a commit 8831c5e

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

fin_maestro_kin/modules/data_toolkit/nse/nse_operations.py

+28-10
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,21 @@ def process_index_data(data):
333333
processed_data.append(processed_entry)
334334
return processed_data
335335

336+
@staticmethod
337+
def process_index_ratios(historical_data):
338+
rounded_data = Helper.convert_dataframe_to_dict(historical_data)
339+
processed_data = []
340+
for entry in rounded_data:
341+
processed_entry = {
342+
"INDEX_NAME": entry["INDEX_NAME"],
343+
"HistoricalDate": entry["HistoricalDate"],
344+
"OPEN": entry["OPEN"],
345+
"HIGH": entry["HIGH"],
346+
"LOW": entry["LOW"],
347+
"CLOSE": entry["CLOSE"],
348+
}
349+
processed_data.append(processed_entry)
350+
return processed_data
336351

337352
class NSEIndices(Helper):
338353
def __init__(self):
@@ -372,12 +387,12 @@ def get_nse_index_history(
372387
except Exception as e:
373388
raise HTTPException(status_code=500, detail=f"Error fetching historical data: {e}")
374389

375-
def index_pe_pb_div(self, symbol, start_date, end_date):
390+
def index_pe_pb_div(self, symbol, start_date, end_date, index_name):
376391
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y").strftime("%d %b %Y")
377392
end_date = datetime.datetime.strptime(end_date, "%d-%b-%Y").strftime("%d %b %Y")
378393

379-
data = {"cinfo": f"{{'name':'{symbol}','startDate':'{start_date}','endDate':'{end_date}'}}"}
380-
payload = requests.post('https://niftyindices.com/Backpage.aspx/getpepbHistoricaldataDBtoString', headers=self.niftyindices_headers, json=data).json()
394+
data = {"cinfo": f"{{'name':'{symbol}','startDate':'{start_date}','endDate':'{end_date}','indexName':'{index_name}'}}"}
395+
payload = requests.post('https://niftyindices.com/Backpage.aspx/getHistoricaldatatabletoString', headers=self.niftyindices_headers, json=data).json()
381396
payload = json.loads(payload["d"])
382397

383398
if not payload:
@@ -390,19 +405,21 @@ def get_nse_indices_ratios(
390405
self,
391406
symbol: str = Query(..., title="Symbol", description="Nifty indices symbol"),
392407
start_date: str = Query(..., title="Start Date", description="Start date for historical data in dd-mmm-yyyy format"),
393-
end_date: str = Query(..., title="End Date", description="End date for historical data in dd-mmm-yyyy format")
408+
end_date: str = Query(..., title="End Date", description="End date for historical data in dd-mmm-yyyy format"),
409+
index_name: str = Query(..., title="Index Name", description="Nifty index name")
394410
):
395411
try:
396-
historical_ratios_data = self.index_pe_pb_div(symbol, start_date, end_date)
397-
return historical_ratios_data.to_dict(orient='records')
412+
historical_ratios_data = self.index_pe_pb_div(symbol, start_date, end_date, index_name)
413+
processed_data = self.process_index_ratios(historical_ratios_data)
414+
return processed_data
398415
except Exception as e:
399416
raise HTTPException(status_code=500, detail=f"Error fetching historical ratios data: {e}")
400417

401-
def index_total_returns(self, symbol,start_date,end_date):
418+
def index_total_returns(self, symbol,start_date,end_date, index_name):
402419
start_date = datetime.datetime.strptime(start_date, "%d-%b-%Y").strftime("%d %b %Y")
403420
end_date = datetime.datetime.strptime(end_date, "%d-%b-%Y").strftime("%d %b %Y")
404421

405-
data = {"cinfo": f"{{'name':'{symbol}','startDate':'{start_date}','endDate':'{end_date}'}}"}
422+
data = {"cinfo": f"{{'name':'{symbol}','startDate':'{start_date}','endDate':'{end_date}','indexName':'{index_name}'}}"}
406423
payload = requests.post('https://niftyindices.com/Backpage.aspx/getTotalReturnIndexString', headers=self.niftyindices_headers, json=data).json()
407424
payload = json.loads(payload["d"])
408425

@@ -416,10 +433,11 @@ def get_nse_indices_returns(
416433
self,
417434
symbol: str = Query(..., title="Symbol", description="Nifty indices symbol"),
418435
start_date: str = Query(..., title="Start Date", description="Start date for historical data in dd-mmm-yyyy format"),
419-
end_date: str = Query(..., title="End Date", description="End date for historical data in dd-mmm-yyyy format")
436+
end_date: str = Query(..., title="End Date", description="End date for historical data in dd-mmm-yyyy format"),
437+
index_name: str = Query(..., title="Index Name", description="Nifty index name")
420438
):
421439
try:
422-
historical_returns_data = self.index_total_returns(symbol, start_date, end_date)
440+
historical_returns_data = self.index_total_returns(symbol, start_date, end_date, index_name)
423441
return historical_returns_data.to_dict(orient='records')
424442
except Exception as e:
425443
raise HTTPException(status_code=500, detail=f"Error fetching historical ratios data: {e}")

0 commit comments

Comments
 (0)