Learn Algo Trading with Python | Codes | Youtube Series

Hmm makes sense. But the reason I wasn’t going into with the tradehull files was cause it was fetching ltp only via excel files, had to write to excel and read from it. But now i see there is a direct function call for getting ltp. okay thank you for the reply. Also the get_live_pnl function and all is not working consistently. suddenly it doesnt and then doesnt.

Hi @Msk92

  1. Yes, now fetching LTP is now a easy process.

  2. For the get_live_pnl , see
    Learn Algo Trading with Python | Codes | Youtube Series - #689 by Tradehull_Imran

Hi @Ajay_Yadav

You can use Dhan_Tradehul_V2 for fetching Historical data easily.

see : https://www.youtube.com/watch?v=HLiEpNZSD80

Hello sir,
while writing code i am unable to get intraday and previous historical data on any stock or options. when implementing the code the following error is displayed as shown in the image.
how do i solve this problem?
@Tradehull_Imran @RahulDeshpande

Sir, i have changed the tp_order id, My code is

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

			sl_price          = round((cc_1['close']*0.98),1)
			tp_price 			= round((cc_1['close'] * 1.003), 1)  # Take Profit price (0.3% above buy price)
			qty               = int(per_trade_margin/cc_1['close'])

			buy_entry_orderid = tsl.order_placement(stock_name,'NSE', 1, 0, 0, 'MARKET', 'BUY', 'MIS')
			sl_orderid        = tsl.order_placement(stock_name,'NSE', 1, 0, sl_price, 'STOPMARKET', 'SELL', 'MIS')
			tp_orderid 		  = tsl.order_placement(stock_name,'NSE', 1, 0, tp_price, 'LIMIT', 'SELL', 'MIS')  # Take Profit Limit Order
			traded_wathclist.append(stock_name)

Error is coming, sir… Please check and guide me, sir…

AXISBANK is in uptrend, Buy this script
Traceback (most recent call last):
  File "C:\Users\Admin\OneDrive\Documents\Dhan\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo\Dhan_Tradehull_V2.py", line 138, in order_placement
    orderid = order["data"]["orderId"]
KeyError: 'orderId'
LT
ICICIGI
EXIDEIND
TATACOMM
RELIANCE
ICICIPRULI
IPCALAB
AUBANK
AUBANK is in downtrend, Sell this script

get_live_pnl() call is repeatedly giving this error:

func call:
def getPnl(tsl):
pnl = tsl.get_live_pnl()

output:
Traceback (most recent call last):
File “c:\Users\rando\OneDrive\Documents\dhanTrading\dhanApiSetup\Dhan_Tradehull_V2.py”, line 178, in get_live_pnl
closePrice = ltp_data[underlying]
~~~~~~~~^^^^^^^^^^^^
KeyError: ‘BANKNIFTY 13 NOV 50500 PUT’

Hello @Tradehull_Imran

The algo runs for few times but then suddenly gives this error… have revoked the API but no luck.

D:\ALgo Programming\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo>py “Multi timeframe Algo.py”
-----Logged into Dhan-----
reading existing file all_instrument 2024-11-13.csv
Got the instrument file
SALASAR
If using all scalar values, you must pass an index
Traceback (most recent call last):
File “D:\ALgo Programming\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo\Dhan_Tradehull_V2.py”, line 298, in get_intraday_data
df = pd.DataFrame(ohlc[‘data’])
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py”, line 709, in init
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 481, in dict_to_mgr
return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 115, in arrays_to_mgr
index = _extract_index(arrays)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 645, in _extract_index
raise ValueError(“If using all scalar values, you must pass an index”)
ValueError: If using all scalar values, you must pass an index
If using all scalar values, you must pass an index
Traceback (most recent call last):
File “D:\ALgo Programming\8. Session8- 2nd Live Algo\8. Session8- 2nd Live Algo\2nd live Algo\Dhan_Tradehull_V2.py”, line 259, in get_historical_data
df = pd.DataFrame(ohlc[‘data’])
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py”, line 709, in init
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 481, in dict_to_mgr
return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 115, in arrays_to_mgr
index = _extract_index(arrays)
File “C:\Users\priye\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py”, line 645, in _extract_index
raise ValueError(“If using all scalar values, you must pass an index”)
ValueError: If using all scalar values, you must pass an index
Traceback (most recent call last):
File “Multi timeframe Algo.py”, line 57, in
chart_1[‘rsi’] = talib.RSI(chart_1[‘close’], timeperiod=14) #pandas
TypeError: ‘NoneType’ object is not subscriptable

1 Like

Hi @Tradehull_Imran i am actually looking for live sample algos based on few strategies like MA 20-50 Crossovers , MACD cross overs or RSI can you provide live sample templates which would work with out Data API i think its not possible to my understanding correct me if im wrong even looking such basic strategies in code format to learn initially to check the code live deployments then wanted to understand deep & try the pro level is it possible ? im asking for basic very few strategies where i can check with my account credentials to understand the system how it works. im asking for it bcz unable to understand anything from your 8 sessions yt videos so if sample templates could make us understand, well do u have any suggestions upon it ?

Thanks for the reply but it wont work on get_historical_data function, as it is returning last 6 day’s data… iloc[0] will give first candle of previous 6th day… not today’s one.

Bro, generate new token and then try it

Hi sir,

when i run the previous_hist_data = tsl.get_historical_data(‘NIFTY’,‘NSE’,12) line it giving the error like “DataFrame constructor not properly called!” and when i run this otm_ce_name, pe_name, ce_OTM_strike, pe_OTM_strike = tsl.OTM_Strike_Selection(‘NIFTY’,‘14-11-2024’,3) i’m getting the names and strikes but after this i run this intraday_hist_data = tsl.get_intraday_data(otm_ce_name,‘NFO’,1)
but i got this error “intraday_minute_data() missing 2 required positional arguments: ‘from_date’ and ‘to_date’” Please hep me to solve this.

@Tradehull_Imran
Both Code & Error are attached…
Please advice how to sort this out…

Done

import pdb

import time

import datetime

import traceback

import talib

from Dhan_Tradehull_V2 import Tradehull

import pandas as pd

Initialize Tradehull with client_code and token_id

client_code = “********”

token_id = “**********”

tsl = Tradehull(client_code, token_id)

After this i want to take one symbol and add the super trend indicator and Dhan open interest indicator. How can i do that?

hi @Tradehull_Imran,

My algo is working fine now… only i am not able to place Target orders along with buy/sell and SL orders. Please help

I am also getting this err

Hi everyone

  1. If you are getting issues with order placement,
    example of issue : Learn Algo Trading with Python | Codes | Youtube Series - #711 by thakurmhn

    so we need to comment out kill switch, as it may get triggered during testing and further will not allow order placement

solution below


while True:

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

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

	if (current_time > datetime.time(15, 15)) or (live_pnl < max_loss):
		# I_want_to_trade_no_more = tsl.kill_switch('ON')    ... comment this out, its blocking orders if accidently triggered in testing
		order_details = tsl.cancel_all_orders()
		print("Market is over, Bye Bye see you tomorrow", current_time)
		break


Hi everyone

  1. If you are facing issues in running algo continuously
    example of issue : Learn Algo Trading with Python | Codes | Youtube Series - #715 by rejithkoroth

then its because we are exceeding Rate limits OF historical data api call.
same happened for my account today.
I will send a updated code for Session 8 > Multi timeframe Algo.py tomorrow,

2 Likes

Hello, @Tradehull_Imran After watching your Master Algo Trading series, I was running code: import pdb import time import datetime import traceback from dhanhq import dhanhq from Dhan_Tradehull import Tradehull import pandas as pd pdb.set_trace() is giving output of error as: Traceback (most recent call last):
File “Dhan_codebase usage.py”, line 5, in
from dhanhq import dhanhq
ModuleNotFoundError: No module named ‘dhanhq’. However dhanhq has been installed in the directory.

Hello @Tradehull_Imran,

Please look into this issue as i am also not able to place target limit order… please include this into next video.