watchlist = [‘ADANIENT’, ‘ADANIPORTS’, ‘APOLLOHOSP’, ‘ASIANPAINT’, ‘AXISBANK’, ‘BAJAJ-AUTO’, ‘BAJFINANCE’,‘Manappuram’,‘SUPREMEIND’,‘MAZDOCK’]
for name in watchlist:
chart = tsl.get_historical_data(tradingsymbol=name, exchange=‘NSE’, timeframe=“1”)
Ensure datetime format and date column
chart[‘datetime’] = pd.to_datetime(chart[‘datetime’])
chart[‘date’] = chart[‘datetime’].dt.date
Get unique trading days
unique_dates = chart[‘date’].unique()
if len(unique_dates) < 2:
print(“ Not enough data to compare”)
else:
# Previous day’s last close
prev_day = unique_dates[-2]
prev_close = chart[chart[‘date’] == prev_day].iloc[-1][‘close’]
# Today's first candle open
today = unique_dates[-1]
today_open = chart[chart['date'] == today].iloc[0]['open']
print(name)
print(chart)
pdb.set_trace()
Codebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-06-17.csv
Got the instrument file
Traceback (most recent call last):
File “C:\Users\stock\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py”, line 3653, in get_loc
return self._engine.get_loc(casted_key)
File “pandas_libs\index.pyx”, line 147, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\index.pyx”, line 176, in pandas._libs.index.IndexEngine.get_loc
File “pandas_libs\hashtable_class_helper.pxi”, line 7080, in pandas._libs.hashtable.PyObjectHashTable.get_item
File “pandas_libs\hashtable_class_helper.pxi”, line 7088, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ‘datetime’
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “rs.py”, line 21, in
chart[‘datetime’] = pd.to_datetime(chart[‘datetime’])
File “C:\Users\stock\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py”, line 3761, in getitem
indexer = self.columns.get_loc(key)
File “C:\Users\stock\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\indexes\base.py”, line 3655, in get_loc
raise KeyError(key) from err
KeyError: ‘datetime’