Hi,
Is it possible to get NIFTY 50 Historical 1 day data using API’s.
I did get some data back but it stops at
19655.4 19678.25 19423.55 19526.55 290666798 02/08/2023
and doesnt go any further than that, even though if I hard code the date all returned is No Data.
Hardik
September 17, 2024, 3:57am
2
Hello @Nirmal_Soni
Welcome to MadeForTrade community!
Can you tell me which API are you polling for this? And also the request JSON that you sent on this API?
Parameters for NIFTY 50 (Last Month’s Data)
symbol = ‘NIFTY’
exchange_segment = ‘NSE_EQ’
instrument_type = ‘INDEX’
expiry_code = 0
Fetch data for the last month
from_date = (datetime.now() - timedelta(days=30)).strftime(‘%Y-%m-%d’)
to_date = (datetime.now() - timedelta(days=1)).strftime(‘%Y-%m-%d’)
Fetch historical daily data for NIFTY 50
historical_data = dhan.historical_daily_data(
symbol=symbol,
exchange_segment=exchange_segment,
instrument_type=instrument_type,
expiry_code=expiry_code,
from_date=from_date,
to_date=to_date
)
When you get a chance are you able to please help.
1 Like
Hardik
September 18, 2024, 7:59am
5
Hello @Nirmal_Soni
Over here, you need to put exchange_segment
as IDX_I for index data.
Hi @Hardik
Please have a look at this. I can get historical data on Equities, but the same code with appropriate changes cannot retrieve Indices Data.
def get_historical_data(security_id,exchange_segment,instrument,access_token):
chart_url = "https://api.dhan.co/v2/charts/historical"
my_headers = {
'Content-Type':'application/json',
'access-token':access_token,
'Accept':'application/json'
}
data = {
"securityId": security_id,
"exchangeSegment": exchange_segment,
'expiryCode': 0,
"instrument": instrument,
"fromDate": "2024-09-17" ,
"toDate": "2024-09-18"
}
response = requests.post(url = chart_url, headers = my_headers, json = data)
if response.status_code != 200:
print("Error : Check the Output")
return response.json()
# ITC
security_id = '1660'
exchange_segment = 'NSE_EQ'
instrument = 'EQUITY'
get_historical_data(security_id,exchange_segment,instrument,access_token)
# NIFTY 50
security_id = '13'
exchange_segment = 'IDX_I'
instrument = 'INDEX'
get_historical_data(security_id,exchange_segment,instrument,access_token)
The Output is as Follows.
Please have a look into it.
Note : I have tried using the sandbox for the same and same thing Happened. The data was not retrieved even there for Indices.
I tried with this code and it worked, may be it can be of your any help, once try
BN=dhan.historical_daily_data(
symbol='BANKNIFTY',
exchange_segment='IDX_I',
instrument_type='INDEX',
expiry_code=0,
from_date='2024-09-01',
to_date='2024-09-18'
)
data= pd.DataFrame(BN['data'])
data['start_Time'] = data['start_Time'].astype(int)
data['start_Time'] = data['start_Time'].apply(dhan.convert_to_date_time)
data
1 Like
Hardik
September 19, 2024, 10:05am
10
Hello @Mitaanshu_Agarwal
@Sai_Chandra just suggested the parameters to be passed. Are you still facing the same issue?
Hello @Mitaanshu_Agarwal ,
Am not that keen into V2. , However please try to run the below code, you should be able to get , the index historical data.
from dhanhq import dhanhq
from datetime import datetime, timedelta
client_id =
access_token =
dhan = dhanhq(client_id,access_token)
BN=dhan.historical_daily_data(
symbol='BANKNIFTY',
exchange_segment='IDX_I',
instrument_type='INDEX',
expiry_code=0,
from_date='2024-09-01',
to_date='2024-09-18'
)
# This is optional , just to convert time stamps properly
data= pd.DataFrame(BN['data'])
data['start_Time'] = data['start_Time'].astype(int)
data['start_Time'] = data['start_Time'].apply(dhan.convert_to_date_time)
data
You can replace BANKNIFTY with any of the index that you might want to.