Learn Algo Trading with Python | Codes | Youtube Series

Hi @Vikas_Chaudhary ,

Do update codebase on your system

  • 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:
Video reference : https://www.youtube.com/watch?v=P9iPYShakbA

Refer the below link for more details:
https://pypi.org/project/Dhan-Tradehull/

Hi @anandc ,

We are also facing the same issue, we are in contact with Dhan and Hardik for the same.

Hi @Raju2 ,

Do share the code which you are trying to get the Market Depth data.

Hi @Dev_Goswami ,

From our side the PNL function seems to be working fine, can you send screenshot by running the below code:
pip show Dhan-Tradehull

@Tradehull_Imran

@Tradehull_Imran


Sir this is my code. It giving me the binary data as output. But need output in numbers format

With release of 3.1 video, do videos 5 onwards in series " Master Algo Trading with Python | Episode 5:" still hold good and the py scripts need to be run as is?
I am facing issue in running code in session 5. py i am trying to run is “Correct code for Ltp based Strike Selection.py” in folder “Ltp Based Strike Selection”. The code and error is pasted below. I have upgraded latest version of dhantradehull.

Blockquote
import pdb
from Dhan_Tradehull import Tradehull

client_code = “”
token_id = “”

‘’’
client_code = “”
token_id = “”
‘’’

tsl = Tradehull(client_code,token_id)

for distance_from_atm in range(1, 11):

otm_ce_name, otm_pe_name, ce_OTM_strike, pe_OTM_strike = tsl.OTM_Strike_Selection('FINNIFTY','22-10-2024',distance_from_atm)
ce_ltp = tsl.get_ltp(otm_ce_name)

bc1 = (ce_ltp < 5)
print(distance_from_atm, otm_ce_name, ce_ltp)

if bc1:
	print("Found the strike which has less than Rs 5 Ltp" ,otm_ce_name, ce_ltp)

Blockquote

error

Blockquote
PS D:\DhanAlgo> d:; cd ‘d:\DhanAlgo’; & ‘c:\Users\abcwin\AppData\Local\Programs\Python\Python38\python.exe’ ‘c:\Users\abcwin.vscode\extensions\ms-python.debugpy-2025.0.1-win32-x64\bundled\libs\debugpy\launcher’ ‘49321’ ‘–’ ‘d:\DhanAlgo\5. Session5- Python Part 2\Ltp Based Strike Selection\Correct code for Ltp based Strike Selection.py’
-----Logged into Dhan-----
reading existing file all_instrument 2025-03-04.csv
Got the instrument file
No such file: ‘Websocket.xlsx’
Traceback (most recent call last):
File “d:\DhanAlgo\5. Session5- Python Part 2\Ltp Based Strike Selection\Dhan_Tradehull.py”, line 49, in init
self.wb = xw.Book(“Websocket.xlsx”)
File “c:\Users\saurabhwin\AppData\Local\Programs\Python\Python38\lib\site-packages\xlwings\main.py”, line 929, in init
impl = app.books.open(
File “c:\Users\saurabhwin\AppData\Local\Programs\Python\Python38\lib\site-packages\xlwings\main.py”, line 5153, in open
raise FileNotFoundError(“No such file: ‘%s’” % fullname)
FileNotFoundError: No such file: ‘Websocket.xlsx’
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
1 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
2 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
3 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
4 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
5 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
6 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
7 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
8 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
9 None 0
Found the strike which has less than Rs 5 Ltp None 0
‘Tradehull’ object has no attribute ‘sheet’
Getting Error at OTM strike Selection as ‘Tradehull’ object has no attribute ‘index_step_dict’
argument of type ‘NoneType’ is not iterable
10 None 0
Found the strike which has less than Rs 5 Ltp None 0
PS D:\DhanAlgo>
Blockquote

trade_hull = Tradehull(client_id, access_token)

stock = ['BSE', 'CDSL', 'INFY', "ANGELONE", "POONAWALLA", "HCLTECH", "INFY"]

for stock in stock:
    
    ATM_strike, chain, Expiry_date, exchange = trade_hull.get_option_chain(stock, sleep_time=2)
    print(f"ATM_strike: {ATM_strike}")
    print(f"chain:\n{chain}")

Hello Sir @Tradehull_Imran

I was optimizing, the get_option_chain function for least time possible to fetch an option chain, I tried 2 seconds,

in the experiment above, it worked fine.

but in live trades, it still does says, Too many requests

is it that, for some stocks it takes more than 2 sec, for some less than 2 sec ?
what would be an optimum time sleep, without slowing the code down further ?

Hi @m0rph1s ,

The quantity must be given as 65 instead of 1. Refer the below code:

order_id = tsl.order_placement(tradingsymbol='FINNIFTY 27 MAR 27000 CALL', exchange='NFO', quantity=65, price=0, trigger_price=0, order_type='MARKET', transaction_type='BUY', trade_type='MARGIN', disclosed_quantity=0)

Hi @Dev_Goswami ,

Use the below code to update the codebase to 3.0.6:
pip install --upgrade Dhan-Tradehull

There seems to be no issue in the pnl function:
image

Hi @Raju2 ,

To convert your given binary output, this function may help to convert to numbers.

import struct

def binary_to_readable(binary_data):

    readable_data = []
    index = 0
    data_length = len(binary_data)
    
    while index < data_length:
        try:
            int_value = struct.unpack_from('<I', binary_data, index)[0]
            index += 4
            float_value = struct.unpack_from('<f', binary_data, index)[0]
            index += 4
            readable_data.append((int_value, float_value))
        except struct.error:
            break

    return readable_data

binary_input = b'L\x01)\x015\x05\x00\x00\x00\x00\x00\x0033333\xb6\x9a@\x87\x00\x00\x00\x02\x00\x00\x00fffff\xb5\x9a@Y\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\xb5\x9a@\x03\x00\x00\x00\x01\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb4\x9a@\x1e\x01\x00\x00\x03\x00\x00\x00\x9a\x99\x99\x99\x99\xb4\x9a@\x1b\x00\x00\x00\x01\x00\x00\x00fffff\xb4\x9a@\xa9\x00\x00\x00\x03\x00\x00\x0033333\xb4\x9a@3\x01\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\xb4\x9a@\xbc\x00\x00\x00\x08\x00\x00\x00\x9a\x99\x99\x99\x99\xb3\x9a@\x16\x01\x00\x00\x07\x00\x00\x00fffff\xb3\x9a@M\x01\x00\x00\x04\x00\x00\x0033333\xb3\x9a@\xa6\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\xb3\x9a@!\x02\x00\x00\x08\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb2\x9a@\xa3\x00\x00\x00\x03\x00\x00\x00\x9a\x99\x99\x99\x99\xb2\x9a@\xd6\x00\x00\x00\x04\x00\x00\x00fffff\xb2\x9a@y\x00\x00\x00\x04\x00\x00\x0033333\xb2\x9a@\x8a\x04\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\xb2\x9a@Q\x02\x00\x00\x07\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb1\x9a@\x1a\x01\x00\x00\x05\x00\x00\x00\x9a\x99\x99\x99\x99\xb1\x9a@\xfd\x00\x00\x00\x05\x00\x00\x00fffff\xb1\x9a@\xcb\x00\x00\x00\x02\x00\x00\x00L\x013\x015\x05\x00\x00\x00\x00\x00\x00fffff\xb6\x9a@\x01\x00\x00\x00\x01\x00\x00\x00fffff\xb7\x9a@\x05\x00\x00\x00\x01\x00\x00\x00\x9a\x99\x99\x99\x99\xb7\x9a@\x0b\x00\x00\x00\x02\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb7\x9a@6\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\xb8\x9a@\xb8\x02\x00\x00\x05\x00\x00\x0033333\xb8\x9a@A\x06\x00\x00\x07\x00\x00\x00fffff\xb8\x9a@\x1a\t\x00\x00\n\x00\x00\x00\x9a\x99\x99\x99\x99\xb8\x9a@i\x03\x00\x00\x05\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb8\x9a@\x7f\x02\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\xb9\x9a@"\x06\x00\x00\n\x00\x00\x0033333\xb9\x9a@b\t\x00\x00\x08\x00\x00\x00fffff\xb9\x9a@\x05\x02\x00\x00\x04\x00\x00\x00\x9a\x99\x99\x99\x99\xb9\x9a@\x1a\x02\x00\x00\x06\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xb9\x9a@\xb6\n\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\xba\x9a@\xc5\x04\x00\x00\x08\x00\x00\x0033333\xba\x9a@\xae\x05\x00\x00\n\x00\x00\x00fffff\xba\x9a@\xe7\x00\x00\x00\x05\x00\x00\x00\x9a\x99\x99\x99\x99\xba\x9a@\xcc\t\x00\x00\n\x00\x00\x00\xcd\xcc\xcc\xcc\xcc\xba\x9a@r\x05\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\xbb\x9a@\x15\x02\x00\x00\x05\x00\x00\x00'
decoded_data = binary_to_readable(binary_input)

print(decoded_data)

Hi @saurabha213 ,

Since we have upgraded to rest api calls from websocket.
Now the files in session 5 does not hold true.

1 Like

Hello Sir, I don’t know coding, but I want to get my own algo made. Can you help me? What is the fee for making an algo, what can be made?

Hi @sonu_agarwal ,

Do go through this website for custom algo development:

(Pdb++) previous_hist_data = tsl.get_historical_data(‘TCS’,‘NSE’,12)
DataFrame constructor not properly called!
Traceback (most recent call last):
File “C:\Users\Ajay Singh Rajput\AppData\Local\Programs\Python\Python38\lib\cmd.py”, line 214, in onecmd
func = getattr(self, ‘do_’ + cmd)
AttributeError: ‘Pdb’ object has no attribute ‘do_previous_hist_data’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\Ajay Singh Rajput\Downloads\3. Session3 - Codebase\3. Session3 - Codebase\3. Session3 - Codebase\Dhan codebase\Dhan_Tradehull.py”, line 222, in get_historical_data
df = pd.DataFrame(ohlc[‘data’])
File “C:\Users\Ajay Singh Rajput\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py”, line 817, in init
raise ValueError(“DataFrame constructor not properly called!”)
ValueError: DataFrame constructor not properly called!

Have Successfully installed Dhan-Tradehull-3.0.6

Hi @Tradehull_Imran,

I got the strike of desired delta and want to buy it, but to buy that perticular strike I need full name of the strike like (NIFTY 06 Mar 22600 CE).

how do i get full name of option from only strike price?
tried below code.

atm_Strike, option_chain = tsl.get_option_chain(Underlying= "NIFTY", exchange= "INDEX", expiry= 0, num_strikes= 15)

CE_strikes_less_than_delta = option_chain[option_chain['CE Delta'] < 10]
index_of_CE_strike = CE_strikes_less_than_delta['CE Delta'].idxmax()
far_OTM_CE_strike = CE_strikes_less_than_delta.loc[index_of_CE_strike]['Strike Price']

far_OTM_CE_name   = ?? #CE_strikes_less_than_delta.loc[index_of_CE_strike]['Name']?

@rahulcse56 You require LTP only once or continously, if it’s once then you are already know the way, but if you require LTP continously then use “Live Market Feed” method which uses websocket to fetch the data in realtime without hitting rate limit. I used this method to scan for 400 stocks in realtime basis.

@Tradehull_Imran
sir, we can subscribe upto 50 instruments in a single connection.
How to send multiple json connection to subscribe to 150 instuments

And I’m getting a WebSocket error: “Connection to the remote host was lost.”
I subscribed to a total of 50 instruments, but the WebSocket is not sending data continuously.
Please help me understand how to send multiple requests and how to avoid this error.

Also getting error like
WebSocket Error: [Errno 104] Connection reset by peer

@Tradehull_Imran
Sir, I’m facing frequent WebSocket connection losses.
Even when requesting data for just two instruments, it’s not sending data properly.
Please help us, sir.

Hi @Ajay_Singh_Rajput ,

Dhan does not support retrieving historical data for the 12-timeframe. The available timeframes are 1, 5, 15, 25, 60, and Day.

Refer the below code:

historical_data = tsl.get_historical_data('NIFTY', 'INDEX', '1')
1 Like