from Dhan_Tradehull import Tradehull
import datetime
import pdb
import os
client_code = "000000 "
token_id = "000000"
tsl = Tradehull(client_code, token_id)
index_name = 'SENSEX'
no_strikes_to_replay = 10
expiry_date = "29 APR"
start_time ='2025-04-29 09:15:00+05:30'
today_date = datetime.datetime.now().date()
index_data = tsl.get_historical_data(tradingsymbol=index_name, exchange='INDEX', timeframe="1")
ATM_time_data = index_data[index_data['timestamp']==start_time]
if not ATM_time_data.empty:
ATM_close = ATM_time_data.iloc[-1]['close']
step = tsl.index_step_dict[index_name]
ATM_Strike = round(ATM_close/step)*step
all_strikes = [ATM_Strike+(step*i) for i in range(1,no_strikes_to_replay+1)] + [ATM_Strike-(step*i) for i in range(1,no_strikes_to_replay+1)] + [ATM_Strike]
call_and_put_Strikes = [f"{index_name} {expiry_date} {strike} CALL" for strike in all_strikes] + [f"{index_name} {expiry_date} {strike} PUT" for strike in all_strikes]
file_path = f'data/{index_name}/{today_date}/'
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
for strike in call_and_put_Strikes:
option_df = tsl.get_historical_data(tradingsymbol=strike, exchange='NFO', timeframe="1")
option_df.to_csv(f'data/{index_name}/{today_date}/{strike}.csv',index=False)
ERROR
.exe "c:/Dhan Option Chain/1. Download_Historical_data_SENSEX.pyCodebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR
2 2 BSE … NaN EURIR
3 3 BSE … NaN JPYIR
4 4 BSE … NaN EURUD
… … … … … …
177151 177151 NSE … 1 NATURALGS
177152 177152 NSE … 1 CRUDEOL
177153 177153 NSE … 1 CRUDEOL
177154 177154 NSE … 1 CRUDEOL
.exe "c:/Dhan Option Chain/1. Download_Historical_data_SENSEX.pyCodebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR
2 2 BSE … NaN EURIR
3 3 BSE … NaN JPYIR
4 4 BSE … NaN EURUD
… … … … … …
177151 177151 NSE … 1 NATURALGS
177152 177152 NSE … 1 CRUDEOL
177153 177153 NSE … 1 CRUDEOL
177154 177154 NSE … 1 CRUDEOL
.exe "c:/Dhan Option Chain/1. Download_Historical_data_SENSEX.pyCodebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR
2 2 BSE … NaN EURIR
3 3 BSE … NaN JPYIR
4 4 BSE … NaN EURUD
… … … … … …
177151 177151 NSE … 1 NATURALGS
177152 177152 NSE … 1 CRUDEOL
177153 177153 NSE … 1 CRUDEOL
177154 177154 NSE … 1 CRUDEOL
.exe "c:/Dhan Option Chain/1. Download_Historical_data_SENSEX.pyCodebase Version 3
-----Logged into Dhan-----
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR
2 2 BSE … NaN EURIR
3 3 BSE … NaN JPYIR
4 4 BSE … NaN EURUD
… … … … … …
177151 177151 NSE … 1 NATURALGS
177152 177152 NSE … 1 CRUDEOL
177153 177153 NSE … 1 CRUDEOL
177154 177154 NSE … 1 CRUDEOL
reading existing file all_instrument 2025-04-29.csv
Got the instrument file
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_s x 17 columns]
NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR
2 2 BSE … NaN EURIR
3 3 BSE … NaN JPYIR
4 4 BSE … NaN EURUD
… … … … … …
177151 177151 NSE … 1 NATURALGS
177152 177152 NSE … 1 CRUDEOL
177153 177153 NSE … 1 CRUDEOL
177154 177154 NSE … 1 CRUDEOL
177155 177155 NSE … 1 CRUDEOL
177155 177155 NSE … 1 CRUDEOLrom_date 2024-04-29
[177156 rows x 17 columns]
from_date 2024-04-29
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES 1 BSE … NaN GBPISM_SYMBOL_NAME
0 0 BSE … NaN USDIR
1 1 BSE … NaN GBPIR 4 BSE … NaN EURU
2 2 BSE … NaN EURI
[177156 rows x 17 columns]
from_date 2024-04-29
instrument_df Unnamed: 0 SEM_EXM_EXCH_ID … SEM_SERIES SM_SYMBOL_NAME
0 0 BSE … NaN USDINR
1 1 BSE … NaN GBPINR
2 2 BSE … NaN EURINR
3 3 BSE … NaN JPYINR
4 4 BSE … NaN EURUSD
… … … … … …
177151 177151 NSE … 1 NATURALGAS
177152 177152 NSE … 1 CRUDEOIL
177153 177153 NSE … 1 CRUDEOIL
177154 177154 NSE … 1 CRUDEOIL
177155 177155 NSE … 1 CRUDEOIL
[177156 rows x 17 columns]
from_date 2024-04-29
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Traceback (most recent call last):
File “c:/Dhan Option Chain/1. Download_Historical_data_SENSEX.py”, line 49, in
option_df.to_csv(f’data/{index_name}/{today_date}/{strike}.csv’,index=False)
AttributeError: ‘NoneType’ object has no attribute ‘to_csv’
PS C:\Users\LENOVO>
Folder create but empty
Same problem for other index also