Dhan_websocket no more required
Watch latest Algo series videos
Hi,
I tried executing multiple times the libraries it was installed successfully. also installed separately to see if it works, but i still don’t see in my list of library even after successful installation. is there any thing i am missing
Regards,
narayan
Hi @SEBI.RA.Samir.Sir ,
Do update to our latest codebase version 3.0.6:
- Open Command Prompt: Press Win, type cmd, and press Enter.
- Install Dhan-Tradehull: Run
pip install Dhan-Tradehull - Confirm the installation by running
pip show Dhan-Tradehull
-
Sometimes the API would not work when the Market is closed.
-
Guide to use the updated codebase:
Refer the below pypi link for more details:
Dhan-Tradehull · PyPI
Video reference :
On high level ideally you can call option chain thrice in a min.
Hi @Msk92 ,
Do update to our latest codebase version 3.0.6:
- Open Command Prompt: Press Win, type cmd, and press Enter.
- Install Dhan-Tradehull: Run
pip install Dhan-Tradehull - Confirm the installation by running
pip show Dhan-Tradehull
Guide to use the updated codebase:
Refer the below pypi link for more details:
Video reference :
Hi @Vasili_Prasad ,
Make sure you have loaded the csv file and the below condition is true.
ATM_time_data = index_data[index_data['timestamp']==start_time]
if not ATM_time_data.empty:
ATM close = ATM_time_data.iloc[-1]['close]
Hi @Vindhyawasini ,
tsl.get_intraday_data was a old method to get historical data… it gives data only for today, which makes problem if we want to create indicators on it.
Dhanhq was upgraded and now we can use get_historical_data which gives data for last 5 working days.
Retrieve historical or intraday data:
Get Historical Data
- tsl.get_historical_data(tradingsymbol: str, exchange: str, timeframe: str, debug: str = “NO”)
- Arguments:
- tradingsymbol (str): The trading symbol for the instrument you want to fetch data for (e.g., ‘NIFTY’, ‘ACC’).
- exchange (str): The exchange where the instrument is traded (e.g., ‘NSE’, ‘INDEX’).
- timeframe (str): The timeframe for the data. It can be:
- ‘1’ for 1-minute candles
- ‘5’ for 5-minute candles
- ‘15’ for 15-minute candles
- ‘25’ for 25-minute candles
- ‘60’ for 60-minute candles
- ‘DAY’ for daily candles
- debug (optional, str): Set to “YES” to enable detailed API response logging. Default is “NO”.
- Sample Code:
- Arguments:
data = tsl.get_historical_data(tradingsymbol='NIFTY', exchange='INDEX', timeframe="DAY")
data = tsl.get_historical_data(tradingsymbol='ACC', exchange='NSE', timeframe="1")
Also for expiry parameter you have to select 0 for current expiry, 1 for next expiry and so on.
ce_name, pe_name, strike= tsl.ATM_Strike_Selection(‘NIFTY’,‘1)
Do update to our latest codebase version 3.0.6:
- Open Command Prompt: Press Win, type cmd, and press Enter.
- Install Dhan-Tradehull: Run
pip install Dhan-Tradehull - Confirm the installation by running
pip show Dhan-Tradehull
Guide to use the updated codebase:
Refer the below pypi link for more details:
Video reference :
Hi @Vasili_Prasad ,
Thanks for sharing the notes to students. Onething to note is that, do use the ‘Preformatted Text’ option to send code in a proper format.
Websocket is no longer required. We have switched our calls to Rest API calls. You can follow the below steps:
Do update to our latest codebase version 3.0.6:
- Open Command Prompt: Press Win, type cmd, and press Enter.
- Install Dhan-Tradehull: Run
pip install Dhan-Tradehull - Confirm the installation by running
pip show Dhan-Tradehull
Guide to use the updated codebase:
Refer the below pypi link for more details:
Video reference :
Hi @narayan_uppu ,
Cross check the version in which you are installing the packages. In cmd enter the below
where python
If there is more then 1 version of python installed in your system, then uninstall other versions and try to install .
Thank you very much for your correction. I will check and report to you sir.
VBR Prasad
Sure sir, I will try to follow. In my old laptop I am not getting that </> symbol to follow your suggestion sir.
Is there any way to post the ‘Sublime Text’ file in this group sir. If so please suggest.
VBR Prasad
Sir, Still I am getting error as I have enclosed the ‘cmd’ file sir.C:\Users\LENOVO PC\Desktop\RP\Dhan Algo\DHAN Advanced Algo Trading Series\Session 9\Session 9\Dhan Option Chain\Dhan Option Chain>py "1. Download_Historical_data.py" Codebase Version 3 -----Logged into Dhan----- reading existing file all_instrument 2025-04-16.csv Got the instrument file Exception in Getting OHLC data as {'status': 'failure', 'remarks': {'error_code': 'DH-905', 'error_type': 'Input_Exception', 'error_message': 'System is unable to fetch data due to incorrect parameters or no data present'}, 'data': {'errorType': 'Input_Exception', 'errorCode': 'DH-905', 'errorMessage': 'System is unable to fetch data due to incorrect parameters or no data present'}} Traceback (most recent call last): File "1. Download_Historical_data.py", line 49, in <module> option_df.to_csv(f'data/{index_name}/{today_date}/{strike}.csv',index=False) AttributeError: 'NoneType' object has no attribute 'to_csv'
And my Code is :
from Dhan_Tradehull import Tradehull
import datetime
import pdb
import os
client_code = "1101092420"
token_id = "eeeeeeee"
tsl = Tradehull(client_code, token_id)
index_name = 'NIFTY'
no_strikes_to_replay = 20
expiry_date = "17 APR"
start_time ='2025-04-16 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)
the codes written by Imram,
Will first complete the calculation of indicators (4 or more) and all for watchlist stock for 1st name in the list and then move to next one ?
What if i have 50 stocks in the watch list, and when it is calculating indicators of say 37th stock, the buy condition for 1st stock was met.
will that trade have to wait for the loop to finish ?
isnt this an inefficient way ?
Hi,
I think he has shown a way how it can be done. Now, it depends on the number of stocks in your basket/watchlist.
Definitely, as you said, one will miss the opportunity to buy. The loop will check calculate for all the indicators and based on ones buy/sell conditions execute the orders, but if the number of stocks are more, it will take time, so best idea would to reduce the number of stocks in the basket/watchlist.
Though this scenario have ways to be solved. But, every one has its own pros and cons.
Note:- I haven’t tried any. But these solutions are logical and are do-able.
- Using multi- threading / multi-processing
cons-but this approach can face rate-limit problem and heavy system resources - batch/segmented scanning/processing (say a batch of 10 stks is processed)
cons - complex architecture
HTH
Reagrds
It doesn’t make sense to call option_call three times within a single minute, especially since it’s being used for both entry and exit logic.
If I’m trading across three indices, that means I can only trigger it once per index and then have to wait — which introduces unnecessary delays. In fast markets, that delay can cost me the trade. It’s a loss waiting to happen… ![]()
Any thoughts welcome
@Dhan @RahulDeshpande
FYI…
Getting Error at Option Chain as {'status': 'failure', 'remarks': {'error_code': None, 'error_type': None, 'error_message': None}, 'data': {'data': {'805': 'Too many requests. Further requests may result in the user being blocked.'}, 'status': 'failed'}}
Hi @narayan_uppu ,
Try installing using the below code:
C:\Users\Admin\AppData\Local\Programs\Python\Python38\python.exe -m pip install dhanhq
Also when you are running the code confirm the python version:
python --version
Hi @Vasili_Prasad ,
You can upload the file to drive and share the link here.


