Learn Algo Trading with Python | Codes | Youtube Series

Hi @Lokesh_Patidar

Code seems to be working fine on my end,

Do try this code and send the complete output it sends

		chart_1 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="1", debug="yes")

@Tradehull_Imran
live order @Lokesh_Patidar aapke code se…

1 Like

Hi @Lokesh_Patidar

Check this case,

In error screenshot you are running “BOT.py” and the code screenshot you had sent was of “BOT 2 Live algo on multiple time frame.py”

So we are running different file and code is on different file.

Do close the CMD and run it for “BOT 2 Live algo on multiple time frame.py”

Let me know if it works.

@Tradehull_Imran

sir again same issue with me.

i have already send me to you.

Hi @Lokesh_Patidar

Check this case,

In error screenshot you are running “BOT.py” and the code screenshot you had sent was of “BOT 2 Live algo on multiple time frame.py”

So we are running different file and code is on different file.

Do close the CMD and run it for “BOT 2 Live algo on multiple time frame.py”

Let me know if it works.

sir in my all bot same code same script…is in side…

bcoz different people help me on different way so i will use all this but.

all my tutor are right from there side but .

my issue is the same again and again


this all files i send you on mail in zip.file

Hi @Kishore007

For the daily candle data, it does not changes through the day, so we don’t need to call it in loop.
we can call all daily data 1 time, then store it into a dictionary, and then use it when we want without calling the api

see below code

import pdb
import time
import datetime
import traceback
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
from pprint import pprint
import talib
import pandas_ta as pta
import pandas_ta as ta
import warnings



warnings.filterwarnings("ignore")
#-----------------------------------------------------------------------------------
client_code = "1102790337"
token_id    = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzM3NTIzMDg4LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwMjc5MDMzNyJ9.UWD34xX9VHFQ9ULmjhiufvqp-jzrDXFpKKOLVj0ix6wDVxOUZDmScAiQc-TBN_-TDT7wZl5AjLsFMFiuwrVciQ"
tsl = Tradehull(client_code,token_id)
#----------------------------------------------------------------------------------
available_balance = tsl.get_balance()
max_loss = available_balance * -0.03				#  max loss of 3%
#-----------------------------------------------------------------------------------------


watchlist = ['AARTIIND', 'ABB', 'ASIANPAINT', 'AUROPHARMA', 'AXISBANK', 'BAJAJ-AUTO']
traded_wathclist = []

day_data_dict = {}

for stock_name in watchlist:
	print(f"Storing daily data for {stock_name}")
	chart_d = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="DAY")
	day_data_dict[stock_name] = chart_d




#-----------------------------------------------------------------------------------------------------
while True:
	live_pnl = tsl.get_live_pnl()
	current_time = datetime.datetime.now().time()

	if current_time < datetime.time(0, 15):
		print("Wait for market to start", current_time)
		time.sleep(1)
		continue

	if (current_time > datetime.time(23, 10)):
		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(0.1)
		current_time = datetime.datetime.now().time()
		print(f"Scanning {stock_name}, Time: {current_time}")

		# ORB Conditions -------------------------------------------
		chart_5 = tsl.get_historical_data(tradingsymbol=stock_name, exchange='NSE', timeframe="5")
		chart_d = day_data_dict[stock_name]
		pv_5    = chart_5.iloc[-2]			# pervious_candle_5
		pv_d    = chart_d.iloc[-1]			# pervious_candle_d
		no_repeat_order  = stock_name not in traded_wathclist
		
		# 1) Price Action    ------------------------------------------------------------
		pv_5_close = pv_5['close']  # Close price of previous 5-min candle
		pv_d_high  = pv_d['high']    # High price of previous day candle
		pv_d_low   = pv_d['low']    # Low price of previous day candle

		# Calculate percentage increase from previous day's high to previous 5-min candle close
		percentage_increase = ((pv_5_close - pv_d_high) / pv_d_high) * 100
		percentage_decrease = ((pv_5_close - pv_d_low) / pv_d_low) * 100

		ut_1 = 0.3 <= percentage_increase <= 0.98				# Check if the increase is between range
		dt_1 = -0.98 <= percentage_decrease <= -0.3 			# Check if the decrease is between range

		# 2) Linear Regression Slope -------------------------- apply indicators-----------------------------------
		chart_5['LINEARREG_SLOPE']     = talib.LINEARREG_SLOPE(chart_5['close'], timeperiod=9)
		cc_1            = chart_5.iloc[-2]  
		ut_2			= cc_1['LINEARREG_SLOPE'] >= 0
		dt_2			= cc_1['LINEARREG_SLOPE'] <= 0

		# BUY conditions ------------------------------------------------------------------------------
		if ut_1 and ut_2 and no_repeat_order:
			print(stock_name, "is in uptrend, Buy this script")
			qty 		= 1
			pv_5    	= chart_5.iloc[-2]
			entry_price = pv_5
			entry_orderid = tsl.order_placement(stock_name, 'NSE', qty, 0, 0, 'MARKET', 'BUY', 'MIS')
			traded_wathclist.append(stock_name)

		# SELL conditions -----------------------------------------------------------------------------
		if dt_1 and dt_2 and no_repeat_order:
			print(stock_name, "is in downtrend, Sell this script")
			qty = 1
			pv_5    	= chart_5.iloc[-2]
			entry_price = pv_5
			entry_orderid = tsl.order_placement(stock_name, 'NSE', qty, 0, 0, 'MARKET', 'SELL', 'MIS')
			traded_wathclist.append(stock_name)
1 Like

Hi @Lokesh_Patidar

Your issue is a curious one,

can you record on screensharing your complete attempt to run the file and wait for error to come,
Also do display all the files being used, such as BOT 2 Live algo on multiple time frame and codebase file as well.

you can use zoom to record it.

do share recoding on : tradehull_mentorship@tradehull.com

Hi @Lokesh_Patidar

Also send me the complete output for below code

		chart_1 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="1", debug="yes")

Hi @Lokesh_Patidar

Now we got the exact issue, the instrument file seems to be wrong .

can you delete all_instrument 2024-12-30.csv inside Dependencies folder
and restart the code again, it will then download new instrument file and thus will give the data as well,

Let me know if it worked. This solution worked on my end.

2 Likes

it was not working in morning. it worked after market went live.
so some issue at dhan end.

i have not change anything and it is working the same code.

1 Like

yes sir this instrument file delete is worked now…

buy data is showing this format is so this thing work bcoz i don’t understand ltp open close rsi and all in this so.

Hi @Lokesh_Patidar

Codebase prints the complete response from dhanhq when we use debug=“yes”, so the big text is the completed historical data that was fetched
debug is only used when we face any issue and we don’t know root cause


chart_1 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="1", debug="yes")

now sice the issue has been solved we can call historical data without debug


chart_1 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="1")
1 Like

@Tradehull_Imran “Thank you so much, sir. Today, you helped me a lot in resolving all my concerns and issues.”

1 Like

Ok thank you … :+1:

@Tradehull_Imran Sir,
error is same when enabling if condiition.


when i’m disabling it then error is not coming.

D:\my_applications\my_algo\Strategies>py “2 candle theory Algo - Both Side Pseudocode.py”
Codebase Version 2.6 : Solved - Pnl Issue
-----Logged into Dhan-----
reading existing file all_instrument 2024-12-30.csv
Got the instrument file
Traceback (most recent call last):
File “2 candle theory Algo - Both Side Pseudocode.py”, line 37, in
if (index_chart == None):
File “D:\my_applications\python\lib\site-packages\pandas\core\generic.py”, line 1466, in nonzero
raise ValueError(
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

D:\my_applications\my_algo\Strategies>


Sir now i am facing

Sir, how to fetch top gainers and top loosers of f & o stocks …
Scanning all the 225 stocks taking too much time is there any other way?