Learn Algo Trading with Python | Codes | Youtube Series

@Tradehull_Imran , till friday it was running fine, now Getting below error:

Traceback (most recent call last):
File “MACD_strategy_Nifty_1min.py”, line 73, in
all_ltp = tsl.get_ltp_data(names = watchlist)
AttributeError: ‘numpy.bool_’ object has no attribute ‘get_ltp_data’

can u help me fix this issue..?

Hi @Sahil_Malhotra ,

Can you share your whole code to review the same?

Hi @Tradehull_Imran sir,

trying to fetch option chain data for ‘CRUDEOIL’

ATM_Strike, option_chain = tsl.get_option_chain(Underlying= ‘CRUDEOIL’, exchange = “MCX”, expiry= 0, num_strikes= 20)

getting this error :
Getting Error at Option Chain as No option chain data available for the CRUDEOIL

Hi, has there been any update on bracket order placement pls ? This is a much needed feature to ensure trades are well risk managed !

Hii @Tradehull_Imran sir,

Sir, can we get order book data in algo trading? Specifically, how many orders are placed at different levels for limit orders and market orders? If so, could you please provide more details?

By the way, sir, this has been an incredibly interesting journey—from staring at the laptop screen for 4–5 hours, dealing with losses, to transitioning into algo trading, which is more accurate, fast, disciplined, and efficient. Your guidance has truly made a difference, and we’re grateful for everything you’ve taught us.

With algo trading, it feels like we’ve moved from emotional decision-making to a structured, rule-based approach that eliminates impulsive trades and optimizes execution speed. The reliability of data-driven strategies has made the process smoother, reducing stress and increasing consistency.

Thank you for sharing your knowledge and helping us improve our trading approach!

1 Like

Hi, Siddhesh, from where to download corresponding file

  1. TA_Lib-0.4.24-cp38-cp38-win_amd64.whl
  2. Twisted-20.3.0-cp38-cp38-win_amd64.whl
    for python-3.13.3-amd64
    please suggest.
    Thanks

Hi, Very Good Afternoon, Can you please confirm your code of Trailing_stop_loss, is functional.
I am a Non-Coder, trying to learn , Can you please help me out with some Functional Codes, especially Options trading, And Intraday trading of Equity segment, with your terms please.
Also please let me know about your success in implementing the Trailing stop loss code (Dt.07/02/2025)
VBR Prasad, 9949324446

i an using trading vie pro version i am not able to fire order on condition meet. Alert message i am receving on condition meet.

Hi - @Tradehull_Imran

how to measure the efficiency of the code. Even my 300 line codes take roung 10 secs to complete one loop. With the kinda of market movement - there is high chance that I will miss the entry window (my code has self-developed indicator - with a small price range for entry). Also, SL trailing will also be concern.

More on SL trailing. in you SL trailign videos, ATR based trailing seems to be good but even if I keep the conservative 1.5*ATR (it’s coming around roughly 25 to 30 points on 170-200 rupees options (100 OTM) like today and when the price movement is fast, ATR increases & the SL slowly trails. We might be missing out big on this. We rarely see very big move to get the benefit of this. Success ratio is very less. Also, I want to do ATR trailing based backtesting - can you help me with any books, codes, resources. Or - can you suggest any other method of SL trailing → or diret me to right direction. (btw - I do option buying)

thank you imran sir

Hi Prasad,

Yes, my code is functional, but it’s too complex to explain.

Trailing stop loss alone is 1800 line code.

I have implemented different kinds of stop losses like dynamic trailing, fixed trailing, fixed trailing with profit, and safety_net, High oi strikes, paper trading, buy dip feature and API lag fallbacks.

if you are interested in above mentioned feature or similar kinds of features, I can guide you. and also share my code.

if you are looking for a normal trailing stop loss, better stick with Dhan Super Trader function, which would be the easiest and has no maintenance code, which is also available in API, it’s 2-3 lines of code added to the original order placement function.

With that, you can input trailing jump, take profit, and take loss altogether.

sir i have subcribed to dhanapi and i am able to fetch data but their are certain discrepancies as close value obv value these are not matching.

tsl.modify_order() is giving constatnt error of an unexpected keyword argument ‘order_id’. Here is my simple code just to check order placement. Can anyone help here. Are you also getting this? @Tradehull_Imran @RahulDeshpande

import pdb
import time
import datetime
import traceback
import pandas as pd
from pprint import pprint
from Dhan_Tradehull_V2 import Tradehull

client_code = “CCode”
token_id = “Tiken here”
tsl = Tradehull(client_code, token_id)

if True:
orderid = tsl.order_placement(
tradingsymbol=‘NIFTY 22 MAY 25700 CALL’,
exchange=‘NFO’,
quantity=75,
price=0,
trigger_price=0,
order_type=‘MARKET’,
transaction_type=‘BUY’,
trade_type=‘MIS’
)
order_details = tsl.get_order_detail(orderid=orderid)
print("Success: OrderId- ", orderid)
sl_orderid = tsl.order_placement(
tradingsymbol=‘NIFTY 22 MAY 25700 CALL’,
exchange=‘NFO’,
quantity=75,
price=3.5,
trigger_price=4.0,
order_type=‘STOPLIMIT’,
transaction_type=‘SELL’,
trade_type=‘MIS’
)
sl_order_details = tsl.get_order_detail(orderid=sl_orderid)
print("Success: SL OrderId- ", sl_orderid)

tsl.modify_order(
    order_id=sl_orderid,
    order_type="STOPLIMIT",
    quantity=75,
    price=3.7,
    trigger_price=4.2
)

Here is the error after runningg the code:

TypeError: modify_order() got an unexpected keyword argument ‘order_id’

Thanks for your Prompt Reply Sir. AS a learner, I am interested to have your code with Different Nomenclatures. My Mail ID is: vbrajprasad@gmail.com, Mob:9949324446
Thank you very much.

VBR Prasad

Okay, Dm would be easy.

I have Dm you!

Thank you very much

is there any issue in API websocket connection
my error code

Validating Access Token...
Watchlist changed. Reconnecting the feed...
Disconnected from WebSocket feed.
WebSocket connection error: server rejected WebSocket connection: HTTP 429
Reconnecting Again...

@Tradehull_Imran

Hi @Tradehull_Imran @RahulDeshpande @Dhan sir,

trying to fetch option chain data or ATM strike selection for ‘CRUDEOIL’, ‘NATURALGASM’(Commodity) just doesnt work

ATM_Strike, option_chain = tsl.get_option_chain(Underlying= ‘CRUDEOIL’, exchange = “MCX”, expiry= 0, num_strikes= 20)

ATM_CE_name, ATM_PE_name, ATM_Strike = tsl.ATM_Strike_Selection(Underlying=‘GOLD’, Expiry=0)

also, is there a way to find out unit of a lot for commodity?
like CRUDEOIL = 100 unit, CRUDEOILM = 10 unit

i’m also seeing similar issue. the same code was working couple of days back. could some1 provide a solution for this.

Hello @Tradehull_Imran Sir

I have a code that scans stocks whose current price > prev day high, the code is as below, in order to achieve the results can i replace get_historical_data() with get_ltp_data() ??

import pdb
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
import talib
import time
import datetime

Client details

client_code = “19”
token_id = “iR1w”

Initialize Tradehull

tsl = Tradehull(client_code, token_id)

available_balance = tsl.get_balance()
leveraged_margin = available_balance5
max_trades = 2
per_trade_margin = (leveraged_margin/max_trades)
max_loss = (available_balance
1)/100*-1

watchlist = [‘ITC’]
traded_wathclist =

while True:

live_pnl = tsl.get_live_pnl()
current_time = datetime.datetime.now().time()

if current_time < datetime.time(9, 15):
	print("wait for market to start", current_time)
	continue

if (current_time > datetime.time(14, 30)) or (live_pnl < max_loss):
	order_details = tsl.cancel_all_orders()
	print("Market is over, Bye Bye see you tomorrow", current_time)
	break


for stock_name in watchlist:
	time.sleep(1)
	print(stock_name)

	chart_1         = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'NSE',timeframe="1")
	chart_5min      = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'NSE',timeframe="5") # this call has been updated to get_historical_data call, 
	chart_15        = tsl.get_historical_data(tradingsymbol = stock_name,exchange = 'NSE',timeframe="15") # this call has been updated to get_historical_data call,
    chart_daily 	= tsl.get_historical_data(tradingsymbol = stock_name,exchange ='NSE', timeframe="DAY")
    

	if (chart_1 is None) or (chart_5min is None) :
		continue

	if (chart_1.empty) or (chart_5min.empty):
		continue

	if (chart_15.empty) or (chart_15.empty):
		continue

	if (chart_15 is None) or (chart_15 is None) :
		continue

	if (chart_daily.empty) or (chart_daily is None):
		continue


	# Conditions that are on 5 minute timeframe
	
	chart_5min['ema20'] = talib.EMA(chart_5min['close'], timeperiod=20)
    chart_5min['ema50'] = talib.EMA(chart_5min['close'], timeperiod=50)
    chart_5min['ema100'] = talib.EMA(chart_5min['close'], timeperiod=100)
    chart_5min['ema200'] = talib.EMA(chart_5min['close'], timeperiod=200)
    

    # Get previous and current day's data
    prev_day = chart_daily.iloc[-2]
    curr_day = chart_daily.iloc[-1]
    cc_5     = chart_5min.iloc[-1]  #pandas  completed candle of 5 min timeframe


    ema_condition = (cc_5['ema20'] > cc_5['ema50']) and (cc_5['ema20'] > cc_5['ema100']) and (cc_5['ema20'] > cc_5['ema200'])
    high_breakout = curr_day['high'] > prev_day['high']
	
	

	

	no_repeat_order = stock_name not in traded_wathclist
	max_order_limit = len(traded_wathclist) <= max_trades


	if high_breakout and ema_condition and no_repeat_order and max_order_limit:
		print(stock_name, "is in uptrend, Buy this script")