Historical API returns error 400!!!! - Python
-
Historical API keeps returning Http Status 400 even with the EXAMPLE GIVEN!!!!! why ?
here is my code...
http = urllib3.PoolManager() API_ENDPOINT = "https://apiconnect.angelbroking.com/rest/secure/angelbroking/historical/v1/getCandleData" #thirty_days_ago = datetime.now() - timedelta(days=30) #from_date = thirty_days_ago.strftime("%Y-%m-%d %H:%M") #to_date = datetime.now().strftime("%Y-%m-%d %H:%M") from_date = "2021-07-01 09:41" to_date = "2021-07-29 10:41" print(from_date) print(to_date) payload = { "exchange": "NSE", "symboltoken": "3045", "interval": "ONE_HOUR", "fromdate": from_date, "todate": to_date } encoded_data = json.dumps(payload).encode('utf-8') r = http.request('POST', API_ENDPOINT, headers=headers, body=encoded_data) print(r.status) print('DRAW:',r.data) resp_body = r.data.decode('utf-8') print(f'DATA:{resp_body}')
-
@Moderator_2 I have tried smartApi.getCandleData, I get the same http 400 error....http response has no error message either to debug!!!!
Is there something additional to be added to the HEADER data for Historical API ? I am successfully sing the same header to retrieve quote data.
can you try my code and see whats wrong ?
-
@Moderator_2 I am posting the code again.....
http = urllib3.PoolManager()
API_ENDPOINT = "https://apiconnect.angelbroking.com/rest/secure/angelbroking/historical/v1/getCandleData"#thirty_days_ago = datetime.now() - timedelta(days=30)
#from_date = thirty_days_ago.strftime("%Y-%m-%d %H:%M")
#to_date = datetime.now().strftime("%Y-%m-%d %H:%M")from_date = "2021-07-01 09:41"
to_date = "2021-07-29 10:41"print(from_date)
print(to_date)payload = { "exchange": "NSE",
"symboltoken": "3045",
"interval": "ONE_HOUR",
"fromdate": from_date,
"todate": to_date
}encoded_data = json.dumps(payload).encode('utf-8')
r = http.request('POST', API_ENDPOINT,
headers=headers,body=encoded_data)
print(r.status)
print('DRAW:',r.data)resp_body = r.data.decode('utf-8')
print(f'DATA:{resp_body}') -
Hi @SKExplorer
As verified on the API request params from the shared code snippet, we are able to retrieve the response without facing 400 status code. Please refer our working python SDK getCandleData() function
https://github.com/angel-one/smartapi-python/blob/main/SmartApi/smartConnect.pyRegards,
SmartAPI team -
@Moderator_2 below is the simple code that I am testing... I am trying off market hours not live...
http = urllib3.PoolManager()
API_ENDPOINT = "https://apiconnect.angelbroking.com/rest/secure/angelbroking/historical/v1/getCandleData"#thirty_days_ago = datetime.now() - timedelta(days=30)
#from_date = thirty_days_ago.strftime("%Y-%m-%d %H:%M")
#to_date = datetime.now().strftime("%Y-%m-%d %H:%M")from_date = "2021-07-01 09:41"
to_date = "2021-07-29 10:41"print(from_date)
print(to_date)payload = { "exchange": "NSE",
"symboltoken": "3045",
"interval": "ONE_HOUR",
"fromdate": from_date,
"todate": to_date
}encoded_data = json.dumps(payload).encode('utf-8')
r = http.request('POST', API_ENDPOINT,
headers=headers,body=encoded_data)
print(r.status)
print('DRAW:',r.data)resp_body = r.data.decode('utf-8')
print(f'DATA:{resp_body}') -
Hi @SKExplorer
Kindly share us the client code and issue occurred date and time to analyze on this matter
Regards,
SmartAPI team -
I am using python sdk for authentication..
I have checked the code (for getting candle data again...(given in this post) ... It still returns http 404 error... can you see anything wrong with the code ???
I am successfully using market data API...
-
Hi @SKExplorer
Apologies for the delayed response.
We are not facing any issue on Historical API at our end.
Please refer
getCandleData func on our SDK.
https://github.com/angel-one/smartapi-python/blob/main/SmartApi/smartConnect.pyRegards,
SmartAPi team