Learn Algo Trading with Python | Codes | Youtube Series

@Tradehull_Imran

please see image. i am not unable to work

https://madefortrade.in/t/learn-algo-trading-with-python-codes-youtube-series/32718/2460?u=subhajitpanja

Check this one if still you are facing issue Please share your code file

@Tradehull_Imran Sir already updated in the code file, for you can download again for fix this

import pdb
import time
import datetime
import traceback
from Dhan_Tradehull import Tradehull
import pandas as pd
from pprint import pprint
import talib

client_code = “”
token_id = “”
tsl = Tradehull(client_code,token_id)

all_ltp_data = tsl.get_ltp_data(names = [‘NIFTY 19 DEC 24000 akshdgasjhgdhgasjgdhjagsdCALL’, ‘NIFTY 19 DEC 24000 PUT’, “ACC”, “CIPLA”])
acc_ltp = all_ltp_data[‘ACC’]
pe_ltp = all_ltp_data[‘NIFTY 19 DEC 24000 PUT’]

stock_name = ‘NIFTY’
ltp = tsl.get_ltp_data(names = [stock_name])[stock_name]

chart = tsl.get_historical_data(tradingsymbol = ‘NIFTY’, exchange = ‘INDEX’,timeframe=“DAY”)
data = tsl.get_historical_data(tradingsymbol = ‘NIFTY 19 DEC 24000 CALL’ ,exchange = ‘NFO’ ,timeframe=“15”)

order_status = tsl.get_order_status(orderid=82241218256027)
order_price = tsl.get_executed_price(orderid=82241218256027)
order_time = tsl.get_exchange_time(orderid=82241218256027)

positions = tsl.get_positions()
orderbook = tsl.get_orderbook()
tradebook = tsl.get_trade_book()
holdings = tsl.get_holdings()

ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0)

ce_name, pe_name, ce_strike, pe_strike = tsl.OTM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0, OTM_count=3)

ce_name, pe_name, ce_strike, pe_strike = tsl.ITM_Strike_Selection(Underlying=‘NIFTY’, Expiry=0, ITM_count=5)

Equity

entry_orderid = tsl.order_placement(tradingsymbol=‘ACC’ ,exchange=‘NSE’, quantity=1, price=0, trigger_price=0, order_type=‘MARKET’, transaction_type=‘BUY’, trade_type=‘MIS’)
sl_orderid = tsl.order_placement(tradingsymbol=‘ACC’ ,exchange=‘NSE’, quantity=1, price=0, trigger_price=2200, order_type=‘STOPMARKET’, transaction_type =‘SELL’, trade_type=‘MIS’)

Options

entry_orderid = tsl.order_placement(tradingsymbol=‘NIFTY 19 DEC 24400 CALL’ ,exchange=‘NFO’, quantity=50, price=0, trigger_price=0, order_type=‘MARKET’, transaction_type=‘BUY’, trade_type=‘MIS’)
sl_orderid = tsl.order_placement(tradingsymbol=‘NIFTY 19 DEC 24400 CALL’ ,exchange=‘NFO’, quantity=25, price=29, trigger_price=30, order_type=‘STOPLIMIT’, transaction_type=‘SELL’, trade_type=‘MIS’)

modified_order = tsl.modify_order(order_id=sl_orderid,order_type=“STOPLIMIT”,quantity=50,price=price,trigger_price=trigger_price)

order_ids = tsl.place_slice_order(tradingsymbol=“NIFTY 19 DEC 24400 CALL”, exchange=“NFO”,quantity=5000, transaction_type=“BUY”,order_type=“LIMIT”,trade_type=“MIS”,price=0.05)

margin = tsl.margin_calculator(tradingsymbol=‘ACC’, exchange=‘NSE’, transaction_type=‘BUY’, quantity=2, trade_type=‘MIS’, price=2180, trigger_price=0)

margin = tsl.margin_calculator(tradingsymbol=‘NIFTY 19 DEC 24400 CALL’, exchange=‘NFO’, transaction_type=‘SELL’, quantity=25, trade_type=‘MARGIN’, price=43, trigger_price=0)

margin = tsl.margin_calculator(tradingsymbol=‘NIFTY 19 DEC 24400 CALL’, exchange=‘NFO’, transaction_type=‘BUY’, quantity=25, trade_type=‘MARGIN’, price=43, trigger_price=0)
margin = tsl.margin_calculator(tradingsymbol=‘NIFTY DEC FUT’, exchange=‘NFO’, transaction_type=‘BUY’, quantity=25, trade_type=‘MARGIN’, price=24350, trigger_price=0)

exit_all = tsl.cancel_all_orders()

hello @Tradehull_Imran whenever im trying to plot close why that line is linear what is the error in code

2 Likes

Hi @prajak_amrute

If you go to the below link I already explained why the error is coming.

https://madefortrade.in/t/learn-algo-trading-with-python-codes-youtube-series/32718/2460?u=subhajitpanja

Once again I am sending you the updated code. Please let us know still if you are getting error

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


client_code = ""
token_id    = ""
tsl         = Tradehull(client_code,token_id)


all_ltp_data   = tsl.get_ltp_data(names = ['NIFTY 27 FEB 23000 CALL', 'NIFTY 27 FEB 23000 PUT', "ACC", "CIPLA"])
acc_ltp = all_ltp_data['ACC']
pe_ltp  = all_ltp_data['NIFTY 27 FEB 23000 PUT']



stock_name = 'NIFTY'
ltp   = tsl.get_ltp_data(names = [stock_name])[stock_name]




chart = tsl.get_historical_data(tradingsymbol = 'NIFTY',  exchange = 'INDEX',timeframe="DAY")
data  = tsl.get_historical_data(tradingsymbol = 'NIFTY 27 FEB 23000 PUT'     ,exchange = 'NFO'  ,timeframe="15")



order_status  = tsl.get_order_status(orderid=82241218256027)
order_price   = tsl.get_executed_price(orderid=82241218256027)
order_time    = tsl.get_exchange_time(orderid=82241218256027)


positions = tsl.get_positions()
orderbook = tsl.get_orderbook()
tradebook = tsl.get_trade_book()
holdings = tsl.get_holdings()


ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying='NIFTY', Expiry=0)


ce_name, pe_name, ce_strike, pe_strike = tsl.OTM_Strike_Selection(Underlying='NIFTY', Expiry=0, OTM_count=3)

ce_name, pe_name, ce_strike, pe_strike = tsl.ITM_Strike_Selection(Underlying='NIFTY', Expiry=0, OTM_count=5)

# Equity
entry_orderid  = tsl.order_placement(tradingsymbol='ACC' ,exchange='NSE', quantity=1, price=0, trigger_price=0,    order_type='MARKET',     transaction_type='BUY',   trade_type='MIS')
sl_orderid     = tsl.order_placement(tradingsymbol='ACC' ,exchange='NSE', quantity=1, price=0, trigger_price=2200, order_type='STOPMARKET', transaction_type ='SELL', trade_type='MIS')

# Options
entry_orderid  = tsl.order_placement(tradingsymbol='NIFTY 27 FEB 23000 CALL' ,exchange='NFO', quantity=50, price=0, trigger_price=0, order_type='MARKET', transaction_type='BUY', trade_type='MIS')
sl_orderid     = tsl.order_placement(tradingsymbol='NIFTY 27 FEB 23000 CALL' ,exchange='NFO', quantity=25, price=29, trigger_price=30, order_type='STOPLIMIT', transaction_type='SELL', trade_type='MIS')

modified_order = tsl.modify_order(order_id=sl_orderid,order_type="STOPLIMIT",quantity=50,price=price,trigger_price=trigger_price)

order_ids      = tsl.place_slice_order(tradingsymbol="NIFTY 27 FEB 23000 CALL",   exchange="NFO",quantity=5000, transaction_type="BUY",order_type="LIMIT",trade_type="MIS",price=0.05)



margin = tsl.margin_calculator(tradingsymbol='ACC', exchange='NSE', transaction_type='BUY', quantity=2, trade_type='MIS', price=2180, trigger_price=0)

margin = tsl.margin_calculator(tradingsymbol='NIFTY 27 FEB 23000 CALL', exchange='NFO', transaction_type='SELL', quantity=25, trade_type='MARGIN', price=43, trigger_price=0)

margin = tsl.margin_calculator(tradingsymbol='NIFTY 27 FEB 23000 CALL', exchange='NFO', transaction_type='BUY', quantity=25, trade_type='MARGIN', price=43, trigger_price=0)
margin = tsl.margin_calculator(tradingsymbol='NIFT FEB PUT', exchange='NFO', transaction_type='BUY', quantity=25, trade_type='MARGIN', price=24350, trigger_price=0)


exit_all       = tsl.cancel_all_orders()

Hi @Hardik
Is dhanhq have ability get future data ( from where I can create future data scanner[heatmap] like long buildup, Short buildup etc. )
Please reply

If not please bring it
and gentle reminder please increase daily data limit which is require for

  1. Scanning
  2. Equity
  3. Commodity
  4. to hit Target / Stop-loss ( I need frequent market scanning to get proper entry and exit )

@RahulDeshpande @Tradehull_Imran sir

Hello @Subhajitpanja

We have already increased the Daily Rate limits, update on the same coming soon.

We already have futures data available on DhanHQ APIs. For building a scanner based on buildups, you will need to combine OI as well as price data, both of which you can get from the APIs itself.

2 Likes

Thank you so much @Hardik

1 Like

@RahulDeshpande same order i can place as “MIS”
but NRML and CNC not able to place

(Pdb++) order_id = tsl.order_placement(tradingsymbol=‘NIFTY 27 FEB 22900 CALL’, exchange=‘NFO’, quantity=75, price=0, trigger_price=0, order_type=‘MARKET’, transaction_type=‘BUY’, trade_type=‘CNC’, disclosed_quantity=0, after_market_order=False, amo_time=‘OPEN’)
'Got exception in place_order as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘DH-906’, ‘error_type’: ‘Order_Error’, ‘error_message’: ‘Trades are not allowed for this Product / Scrip’}, ‘data’: {‘errorType’: ‘Order_Error’, ‘errorCode’: ‘DH-906’, ‘errorMessage’: ‘Trades are not allowed for this Product / Scrip’}}
(Pdb++)

Does anybody has the working sample for trailing? As of now, I have a fixed target. I want to extend my algo for better profit :moneybag:

Hi @prajak_amrute ,

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

1 Like

Hi @Krunal_Darji ,

Use the below code:

nifty = 'NIFTY 5 MINS.csv' # File path
index_data = pd.read_csv(nifty)
plt.plot(index_data['close'])
plt.savefig("nifty_plot.png")
plt.show()

Hi @Manish1 ,

To place a NORMAL order in the algo, we need to set the tradetype parameter to 'MARGIN'.
refer the below code:

order_id = tsl.order_placement(tradingsymbol='NIFTY 27 FEB 26000 CALL', exchange='NFO', quantity=75, price=0, trigger_price=0, order_type='MARKET', transaction_type='BUY', trade_type='MARGIN', disclosed_quantity=0)

Hi @Rajashekhar_Rangappa ,

Refer the below code for trailing the target points for BUY side:

entry_price = 100
initial_target = 110  
trail_points = 2  
target_points = initial_target + trail_points  

while True:
    ltp = tsl.get_ltp_data([name])  
    
    if ltp > target_points:  
        target_points += trail_points  
        print(f"New Target: {target_points}")

Refer the below for trailing stoploss:

ATM Strike Selection get wrongly select

i run my code in NIFTY but as today got an error From Dhan_Tradehull_V2
instead selection NIFTY is select FINNIFTY its first time in 2 mounts
Exception for instrument name FINNIFTY-FEB2025-22750-PE as Check the Trading symbol
got exception in pnl as ‘FINNIFTY-Feb2025-22750-PE’
2025-02-21 16:52:37,630 - ERROR - got exception in pnl as ‘FINNIFTY-Feb2025-22750-PE’
Traceback (most recent call last):
File “c:\Users\Jay\OneDrive\Apps\Index_data\Dhan_Tradehull_V2.py”, line 346, in get_live_pnl
closePrice = ltp_data[underlying]
KeyError: ‘FINNIFTY-Feb2025-22750-PE’
Scaning NIFTY
atm_ce_name: NIFTY 27 FEB 22800 CALL, atm_pe_name: NIFTY 27 FEB 22800 PUT, strike: 22800
@Tradehull_Imran

@Tradehull_Imran Sir,

Yes today it happened with me too… for at least 2 rounds of 5 min data
instead of NIFTY options FINNIFTY options were displaced… I pinched myself twice, when saw this… but now seeing this post… I am replying…

1 Like

Hi Sir,
Can u please share the new code base which we are using in Advance course

Hi @Tradehull_Imran ,
I want to print last 500 candles data of 5 min timeframes,
what can be the issue in this case?
and I can export all the data as well.

chart = tsl.get_historical_data(tradingsymbol="NIFTY", exchange="INDEX", timeframe='5')
chart.tail(500)

Hi @Tradehull_Imran Sir,

After buying ATM Options, if the Target-1 level gets Hit in only one candle, I want to move my SL to target-1 level and new target level will be TARGET-2… how to check this i.e; how to check the condition of reaching T-1 in only one candle… Also gimme the code to modify the SL and Target… pls…