api_response= api_instance.get_historical_candle_data(instrument_key, interval, to_date, api_version)
the above data is a nested Dictionary however when I tried to read the datatype of ‘api_response’ it simply shows some response json link, how can we make this response into a dataframe?
Can you provide the code you’re using to parse the API response? While we can look into issues related to pandas’ data frames, handling them should primarily be on your side.
Hi thank you for the response, I can handle everything once its converted to pandas dataframe, only the api response is not converting into a dataframe, or any other python object/data type will work. Right now when I check the type of response it is simply an upstox object. Please check the code below.
df = api_instance.get_historical_candle_data(instrument_key, interval, to_date, api_version)
df2= pd.DataFrame.from_dict(df[‘data’])
TypeError: ‘GetHistoricalCandleResponse’ object is not subscriptable
We’ll check this data frame issue internally and aim to develop a sample code snippet for GitHub to benefit others. Please note that we cannot guarantee a specific timeline for this, as the issue is not directly related to our core API platform.
okay thank you but at least can you tell me how to convert the historical candle data API response into any other python data object like dictionary or list or excel file
hi can you please reply to this?
Hi Is this issue resolved? I am also not able to convert to pandas dataFrame.
@Nagasai_Harsha and @Vartika_Singh it’s important to note that once the API returns the response, any application-specific logic, such as porting results to a pandas DataFrame or other post-processing on your side, is outside the scope of our support.
But I can share a working example for converting historical response data to pandas dataframe.
- Convert the api response to string format
- Replace single quotes with double quotes to make it a valid JSON string
- Convert the string to python dictionary
- Create a dataframe object from the given dictionary.
import upstox_client
import json
import pandas as pd
from upstox_client.rest import ApiException
api_version = '2.0'
api_instance = upstox_client.HistoryApi()
instrument_key = 'NSE_EQ|INE669E01016'
interval = '30minute'
to_date = '2023-11-13'
from_date = '2023-11-12'
try:
api_response = api_instance.get_historical_candle_data1(instrument_key, interval, to_date, from_date, api_version)
string = str(api_response) # Convert the api response to string
string = string.replace("'", "\"") # Replace single quotes with double quotes to make it a valid JSON string
json_dict = json.loads(string) # convert the string to python dictionary
df = pd.DataFrame(json_dict['data']['candles'],
columns=['time_stamp', 'open', 'high', 'low', 'close', 'volume', 'open_interest'])
print(df.describe())
print(df.shape)
except ApiException as e:
print("Exception when calling HistoryApi->get_historical_candle_data: %s\n" % e)
Please feel free to ask any queries regarding the above code.
Hope this helps you
Hi,
The Historical data I received has column volume but I can see all the rows have 0. so, volume carries only 0 value. Shouldn’t it show volume? Along with it, the historical data which you provide is only for 6 months? Is there any charge for getting historical data for longer time perios?