Learn Algo Trading with Python | Codes | Youtube Series

Hi @Subhajitpanja
This is not a installation issue,
Its simply , because the chart data was not received it was None, so code cannot subscribe to chart[‘close’]

Do check below thread to solve it

1 Like

Thank you so much @Tradehull_Imran sir
:smiling_face:

but sometimes I am getting

Then again started properly. @Tradehull_Imran sir
is that OK ?

@Tradehull_Imran Suggested code is working fine, but issue is it is not returning any stock. I can see many strokes is in the given time frame are meeting my scan criteria. But code not showing /returning.
i hope you understood the issue.

As this code was working fine till 12PM i believe. after that this issue came.

Hi @Khandu_Kshirsagar

I have attached some Skeleton-level code as solution
Main changes are as folows

  1. do not start from 1 when finding OTMS or ITMS
  2. Find day high and low as follows,
			otm_days_open  = otm_ce_chart.iloc[0]['open']
			otm_days_high = otm_ce_chart['high'].max()()
			otm_days_low = otm_ce_chart['low'].min()

Main code starts from here

import pdb
from Dhan_Tradehull import Tradehull
import pandas as pd
import talib
import time

client_code = "1102790337"
token_id = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzMxNDc5MTg1LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwMjc5MDMzNyJ9.cJFmav3LOCQqz9Tp-KRJFPEYR-1Ds3G9YiZqXxcTfnQ3Nqgi4JJNd-y4XRbhfQD5RFAVLooTfZzpUGGstaLYLw"



tsl = Tradehull(client_code, token_id)  # tradehull_support_library
watchlist = ['TATACONSUM', 'ULTRACEMCO', 'ICICIBANK', 'POWERGRID', 'ONGC', 'APOLLOHOSP', 'DRREDDY', 'HEROMOTOCO', 'TRENT', 'TITAN', 'AXISBANK', 'MARUTI', 'WIPRO', 'INFY', 'BAJFINANCE', 'M&M', 'TATAMOTORS', 'BRITANNIA', 'ASIANPAINT', 'KOTAKBANK', 'TECHM', 'BPCL', 'HDFCBANK', 'INDUSINDBK', 'ADANIENT', 'BAJAJFINSV', 'SBIN', 'SHRIRAMFIN', 'BAJAJ-AUTO', 'EICHERMOT', 'RELIANCE', 'ITC', 'GRASIM', 'NESTLEIND', 'HDFCLIFE', 'LT', 'BHARTIARTL', 'JSWSTEEL', 'CIPLA', 'ADANIPORTS', 'COALINDIA', 'TATASTEEL', 'HCLTECH', 'BEL', 'SUNPHARMA', 'HINDUNILVR', 'TCS', 'NTPC', 'SBILIFE', 'HINDALCO', 'ULTRACEMCO']
for stock_name in watchlist:


	print(f"Fetching data for: {stock_name}")
	# Fetch previous historical data and intraday data
	previous_hist_data = tsl.get_historical_data(stock_name, 'NSE', 12)
	time.sleep(2)  # Delay after historical data fetch
	intraday_hist_data = tsl.get_intraday_data(stock_name, 'NSE', 5)
	time.sleep(2)  # Delay after intraday data fetch

	# Get ATM strike prices
	ce_name, pe_name, atm_strike = tsl.ATM_Strike_Selection(stock_name, '31-10-2024')
	print(f"ATM Strike for {stock_name}: {atm_strike}")

	# Loop for OTM strikes
	for distance_from_atm in range(2, 6):  # Adjust range as needed
		try:
			otm_ce_name, pe_name, ce_otm_strike, pe_otm_strike = tsl.OTM_Strike_Selection(stock_name, '31-10-2024',2)
			print(f"OTM CE Strike for {stock_name} at distance {distance_from_atm}: {ce_otm_strike}, OTM PE Strike: {pe_otm_strike}, OTM CE Name: {otm_ce_name}, OTM PE Name: {pe_name}")
			pdb.set_trace()


			otm_ce_chart = tsl.get_intraday_data(otm_ce_name, 'NFO', 5)
			otm_ce_chart['rsi'] = talib.RSI(otm_ce_chart['close'], timeperiod=14)  # aplying rsi on otm chart

			otm_days_open  = otm_ce_chart.iloc[0]['open']
			otm_days_high = otm_ce_chart['high'].max()()
			otm_days_low = otm_ce_chart['low'].min()


			if otm_days_open == otm_days_high:
				print("your next code here")



			if otm_days_open == otm_days_low:
				print("your next code here")



			# cc = otm_ce_name.iloc[-1] #Last completed candle
			# bc = otm_ce_name.iloc[-2] # pandas breackout candle
			# inside_oh = (ba_c['high'] > ic['high']) and (ba_c['low'] < ic['low'])

		except ValueError as e:
			print(f"Getting Error at OTM strike Selection for {stock_name} as {e}")

		time.sleep(2)  # Delay after fetching OTM strikes

	# Loop for ITM strikes
	for distance_from_atm in range(2, 6):  # Adjust range as needed
		try:
			ce_name, pe_name, ce_itm_strike, pe_itm_strike = tsl.ITM_Strike_Selection(stock_name, '31-10-2024', distance_from_atm)
			print(f"ITM CE Strike for {stock_name} at distance {distance_from_atm}: {ce_itm_strike}, ITM PE Strike: {pe_itm_strike}, OTM CE Name: {otm_ce_name}, OTM PE Name: {pe_name}")
		except ValueError as e:
			print(f"Getting Error at ITM strike Selection for {stock_name} as {e}")

		time.sleep(2)  # Delay after fetching ITM strikes

	# Print LTP and historical data
	print(f"LTP for {stock_name}: {tsl.get_ltp(stock_name)}")
	print(f"Historical Data for {stock_name}:\n{previous_hist_data}")
	print(f"Intraday Data for {stock_name}:\n{intraday_hist_data}")
	print("\n" + "="*50 + "\n")
		





1 Like

Hi @Subhajitpanja
I think this is the error under exception. thus it can be ignored.

1 Like

Thank you sir @Tradehull_Imran

@dinild
Do send code

read,

Hi Everyone

Important:

It seems that for some of the students the code is reaching to a good logical state, but note we are still in learning phase of algo trading, so we need to take some precautions.

  1. So do not make your algo live or main capital
  2. Do not fire big orders as of now
  3. Do not make your algo more complex as of now, try to build it version by version.
3 Likes

hi please find the details

from Dhan_Tradehull import Tradehull
import pdb
import pandas as pd
import talib
import xlwings as xw
import time

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

watchlist = [“HDFCBANK”,“TCS”,“JKCEMENT”,“MPHASIS”,“POWERGRID”,“TATAMOTORS”,“RECLTD”,“NAVINFLUOR”,“HINDUNILVR”,“POLYCAB”,“DRREDDY”,“SBILIFE”,“ESCORTS”,“HINDALCO”,“APOLLOTYRE”,“MARUTI”,“BOSCHLTD”,“PEL”,“HCLTECH”,“INDUSINDBK”,“RELIANCE”,“PNB”,“ICICIPRULI”,“MFSL”,“BIOCON”,“ITC”,“HAVELLS”,“ULTRACEMCO”,“LTIM”,“ONGC”,“BAJFINANCE”,“KOTAKBANK”,“ASIANPAINT”,“BRITANNIA”,“SBIN”,“TATACONSUM”,“DIXON”,“BERGEPAINT”,“MOTHERSON”,“EXIDEIND”,“NESTLEIND”,“RAMCOCEM”,“COLPAL”,“DALBHARAT”,“COFORGE”,“SUNPHARMA”,“AUROPHARMA”,“TITAN”,“INFY”,“DABUR”,“IDFCFIRSTB”,“ASTRAL”,“GODREJCP”,“LTF”,“ATUL”,“MRF”,“INDHOTEL”,“SBICARD”,“GUJGASLTD”,“INDIGO”,“ASHOKLEY”,“DIVISLAB”,“SHREECEM”,“AMBUJACEM”,“BANKBARODA”,“ALKEM”,“CHOLAFIN”,“GNFC”,“COALINDIA”,“WIPRO”,“BEL”,“MARICO”,“BATAINDIA”,“IGL”,“TATASTEEL”,“SHRIRAMFIN”,“SYNGENE”,“LUPIN”,“IRCTC”,“NAUKRI”,“BANDHANBNK”,“SAIL”,“PFC”,“BALRAMCHIN”,“ADANIENT”,“ABCAPITAL”,“ACC”,“INDUSTOWER”,“CUMMINSIND”,“GRANULES”,“MCX”,“JSWSTEEL”,“IOC”,“CANBK”,“TECHM”,“ZYDUSLIFE”,“PVRINOX”,“CROMPTON”,“ADANIPORTS”,“HINDPETRO”,“ABB”,“ABBOTINDIA”,“NTPC”,“GLENMARK”,“EICHERMOT”,“BPCL”,“BHARATFORG”,“APOLLOHOSP”,“BHARTIARTL”,“PERSISTENT”,“GAIL”,“HAL”,“HEROMOTOCO”,“TORNTPHARM”,“AXISBANK”,“CIPLA”,“PAGEIND”,“DLF”,“INDIAMART”,“CHAMBLFERT”,“OFSS”,“TATACOMM”,“UPL”,“NATIONALUM”,“METROPOLIS”,“LAURUSLABS”,“CUB”,“PIDILITIND”,“GODREJPROP”,“TATAPOWER”,“HDFCLIFE”,“AUBANK”,“TATACHEM”,“DEEPAKNTR”,“GMRINFRA”,“VEDL”,“CONCOR”,“LT”,“PIIND”,“JUBLFOOD”,“SUNTV”,“AARTIIND”,“ICICIBANK”,“COROMANDEL”,“ABFRL”,“RBLBANK”,“VOLTAS”,“IEX”,“HDFCAMC”,“UNITDSPR”,“JINDALSTEL”,“CANFINHOME”,“BAJAJ-AUTO”,“GRASIM”,“BHEL”,“ICICIGI”,“SRF”,“MUTHOOTFIN”,“BSOFT”,“LICHSGFIN”,“TRENT”,“PETRONET”,“NMDC”,“IPCALAB”,“BAJAJFINSV”,“MGL”,“SIEMENS”,“LTTS”,“UBL”,“LALPATHLAB”,“BALKRISIND”,“IDEA”,“OBEROIRLTY”,“FEDERALBNK”,“TVSMOTOR”,“MANAPPURAM”,“HINDCOPPER”]

while True:

print ('\n' 'Wating for Next Run')
for stoke_name in watchlist:
	chart = tsl.get_intraday_data(stoke_name, 'NSE',1)
	if chart is None:
		continue
	lcc = chart.iloc[-2] 														# last complete candele
	b_out=chart.iloc[-2]																			# Barkeout cabdle
	ic=chart.iloc[-3]																				# inside candle
	ba_c=chart.iloc[-4]																				# Base Candle

	inside_candle_formed = (ba_c['high']>ic['high'] and ba_c['low']<ic['low'])
	upside_brake_out= (b_out['close']>ba_c['high'])
	downside_brake_out= (b_out['close']<ba_c['low'])

	if inside_candle_formed & upside_brake_out:
		print(stoke_name, "is in uptrend")

	if inside_candle_formed & downside_brake_out:
		print(stoke_name, "is in downtrend")

print ('\n' 'Scan Completed')
time.sleep(10)  # Short delay before reconnecting

Dear Rahul Sir,

I am facing Error PFA…SNAP SHOT

Dear Sir,

PFA…Snap shot

Sir maximum time intraday data is not responding.
So rsi is come how it solved.

as explained in videos we dont have to subscribe to data api for algo trading so dont need data just run algo as per the video

WebSocket connection error: ‘NoneType’ object has no attribute ‘recv’
Reconnecting Again…
File “Dhan_websocket.py”, line 130 in run_feed
this kind of error show…please help

@Tradehull_Imran
Hi, After updating the dhan_webscoket file, I am able to fetch LTP data into Excel. Thanks!!

1 Like

Hi @Tradehull_Imran ,
Thanks for quick replies. i am getting below error now before hitting pdb

Hi @dinild
The code appears fine to me

Do send me complete zip of your folder, and share on the google drive link
share it with: tradehull_mentorship@tradehull.com

I will check it on Monday on my end

Hi @SachinYadav
Do check this link for solution

Hi @satntosh_kumar

The Historical data issues seems to be intermittent , I tried same on my end its working fine
Lets try the same on Monday

why is BANK NIFTY historical data i am getting empty

code :
previous_hist_data = tsl.get_historical_data(‘ACC’,‘NSE’,1)
print(“ACC:”)
print(previous_hist_data)
previous_hist_data = tsl.get_historical_data(‘BANKNIFTY’,‘NSE’,1)
print(“BANK NIFTY”)
print(previous_hist_data)
intraday_hist_data =tsl.get_intraday_data(‘BANKNIFTY’,‘NSE’,1)
print(“BANK NIFTY”)
print(intraday_hist_data)

out put :

This BOT Is Picking New File From Dhan
Got the instrument file
available_balance 11.18
ACC:
open high low close volume start_Time
0 2230.0 2294.1 2204.0 2270.2 1092773.0 2024-10-24
BANK NIFTY
Empty DataFrame
Columns:
Index:
BANK NIFTY
open high low close volume start_Time
0 2696.0 2696.45 2691.05 2691.5 7202.0 2024-10-25 15:29:00