Historical daily data imports without date

while downloading historical daily data, only OHLC data is downloading no dates.

Data is there but it is in EPOCH time format.

You have to convert it. Please refer to video here for historical Data.( Skip it to 3:30)

2 Likes

hi,

Thank you very much I got it resolved.

1 Like

Can you please provide your solution here? Thanks in advance.

hi, greetings of the day.

I imported the historical daily data and filltered only epoc column used ,for loop to convert to the date using the dhan function got from the document and stored in the variable.

def get_intraday_data():
try:
data = dhan.intraday_minute_data(
security_id=‘1660’,
exchange_segment=‘NSE_EQ’,
instrument_type=‘EQUITY’
)
df = pd.DataFrame(data[‘data’])
df[‘timestamp’] = df[‘date’]
df[‘date’] = pd.to_datetime(df[‘timestamp’], unit=‘s’)
df.set_index(‘date’, inplace=True)
last_hour_data = df.loc[df.index > df.index.max() - pd.Timedelta(hours=1)]
return last_hour_data
except Exception as e:
print(f"Error occurred while fetching intraday data: {e}")
return pd.DataFrame()
i am not able to fetch ITC stock data this is the program i have written to fetch the data can anyone help me with this i have the subscription. what i am trying to do is my program first look if the market is open if yes then it fetches the data and implements my strategy on the data and take orders accordingly first my program randomly took order then i realised because my code is not fetching the data and so my strategy in not getting implemented. can anyone help with a solution ?

For anyone who lands here in future and wondering which video is being referred to, please see below youtube link and solution provided in the video below the youtube link.

# fetch end of day data for TCS
df_dict = dhan.historical_daily_data(
                                symbol='TCS',
                                exchange_segment='NSE_EQ',
                                instrument_type='EQUITY',
                                expiry_code=0,
                                from_date='1980-01-01',
                                to_date='2024-04-01'
                                )
# convert dict to dataframe
if df_dict['status'] == 'success':
    tdf = pd.DataFrame.from_dict(df_dict['data'])

temp_list = []
# loop thru start_time column to convert from epoch time to human readable time.
for i in tdf['start_Time']:
    temp = dhan.convert_to_date_time(i)
    temp_list.append(temp)
# finally assign the date back into dataframe
tdf['Date'] = temp_list

Having said that, I couldnt figure out which video is being referred to when I was trying to find a solution for this, so I came up with my own solution and code is as below. This is an alternate solution and can run much faster if you are using it on a large dataframe.

# fetch end of day data for TCS
df_dict = dhan.historical_daily_data(
                                symbol='TCS',
                                exchange_segment='NSE_EQ',
                                instrument_type='EQUITY',
                                expiry_code=0,
                                from_date='1980-01-01',
                                to_date='2024-04-01'
                                )
# avoiding for loop and doing it the pandas way to convert epoch date into human readable date. 
df['date'] = (pd.to_datetime('1980-01-01 09:00 +0530')   + pd.to_timedelta(df['start_Time'], 's')).dt.date

Appreciate your response. I couldnt sleep without finding a solution, so did my research and found it.

2 Likes

Very inspiring attitude @ekveer , it will be of help to a lot of members.

1 Like