Learn Algo Trading with Python | Codes | Youtube Series

:saluting_face: :+1: :smiling_face_with_three_hearts: :smiling_face_with_three_hearts:

1 Like

Hi Imran …
I am working with ATM_Strike_Selection.

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

The above command works fine for all instruments except the below ones …
‘BANKBARODA’, ‘COALINDIA’, ‘IOC’, ‘MOTHERSON’, ‘UNITDSPR’

I am not able to figure out the reason for this behaviour …
plz guide me in this regard

@Tradehull_Imran

version 2 is also giving 905 error. it was working well last night.
did something change at dhan end?

from Dhan_Tradehull_V2 import Tradehull

data = tsl.get_historical_data(tradingsymbol = ‘BANKNIFTY’,exchange = ‘INDEX’,timeframe=“DAY”)

Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
(Pdb++) data = tsl.get_historical_data(tradingsymbol = ‘BANKNIFTY’,exchange = ‘INDEX’,timeframe=“DAY”)
Exception in Getting OHLC data as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: ‘DH-905’, ‘error_type’: ‘Input_Exception’, ‘error_message’: ‘Missing required fields, bad values for parameters etc.’}, ‘data’: {‘errorType’: ‘Input_Exception’, ‘errorCode’: ‘DH-905’, ‘errorMessage’: ‘Missing required fields, bad values for parameters etc.’}}

@virender_singh G
Use this :point_down:

	datat  = tsl.get_historical_data(tradingsymbol='BANKNIFTY JAN FUT', exchange='NFO', timeframe="DAY")

1 Like


i’m running this session 8th a but showing error of this below snap…please help me how to solve.

showing Got the instrument file
ADANIPORTS
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
ADANIENT
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
SBIN
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
TATASTEEL
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
BAJAJFINSV
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
Exception in Getting OHLC data as Check the Tradingsymbol or Exchange
RELIANCE
Exception in Getting OHLC data

@Lokesh_Patidar G
use this :point_down:

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

2 Likes

@vinay_kumaar Sir,
Again same thing is showing.

@Tradehull_Imran sir please help me for solve this issue.

@Lokesh_Patidar G
update Dhan_Tradehull_v2 :point_down:

@vinay_kumaar yhi use kr rha hu sir.

@Lokesh_Patidar
sahi work kar rha… :point_down:

Hi @sreetd14

I will check on this, till then do manage the exception

while True:
	
	for name in watchlist:

		try:
			ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying=name, Expiry=0)

			if (ce_name == None) or (pe_name == None):
				print("No ATM strike found for MOTHERSON")
				continue
			
		except Exception as e:
			print(e)


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

---------------for dhan login ---------------- #referance class-6

client_code = “0000000”
token_id = “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzM3NDU3NDQ5LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwMTA2NzUxMSJ9.zccB6K5tYkXUwp30jqOJqNvALbxT-mx5uBDBdNDNVcPOu4nS-JxaeQxuyfzYALw0g-Bdl034_hJ3iZ3jbcl_0w”

tsl = Tradehull(client_code,token_id)

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

watchlist = [‘ADANIPORTS’, ‘ADANIENT’, ‘SBIN’, ‘TATASTEEL’, ‘BAJAJFINSV’, ‘RELIANCE’, ‘TCS’, ‘JSWSTEEL’, ‘HCLTECH’, ‘TECHM’, ‘NTPC’, ‘BHARTIARTL’, ‘WIPRO’, ‘BAJFINANCE’, ‘INDUSINDBK’, ‘KOTAKBANK’, ‘HINDALCO’, ‘ULTRACEMCO’, ‘AXISBANK’, ‘M&M’, ‘MARUTI’, ‘HEROMOTOCO’, ‘EICHERMOT’, ‘COALINDIA’, ‘TITAN’, ‘UPL’, ‘HINDUNILVR’, ‘ITC’, ‘NESTLEIND’, ‘APOLLOHOSP’, ‘ICICIBANK’, ‘GRASIM’, ‘BRITANNIA’, ‘ASIANPAINT’, ‘POWERGRID’, ‘SBILIFE’, ‘ONGC’]
traded_wathclist =

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):
	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.upper(),exchange = 'NSE',timeframe="1")
	chart_5 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="5") # this call has been updated to get_historical_data call, 

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

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


	# Conditions that are on 1 minute timeframe
	chart_1['rsi'] = talib.RSI(chart_1['close'], timeperiod=14) #pandas
	cc_1           = chart_1.iloc[-2]  #pandas  completed candle of 1 min timeframe
	uptrend        = cc_1['rsi'] > 50
	downtrend      = cc_1['rsi'] < 49


	# Conditions that are on 5 minute timeframe
	chart_5['upperband'], chart_5['middleband'], chart_5['lowerband'] = talib.BBANDS(chart_5['close'], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
	cc_5           = chart_5.iloc[-1]   # pandas
	ub_breakout    = cc_5['high'] > cc_5['upperband']
	lb_breakout    = cc_5['low'] < cc_5['lowerband']

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


	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)
		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')
		traded_wathclist.append(stock_name)

	if downtrend and lb_breakout and no_repeat_order and max_order_limit:
		print(stock_name, "is in downtrend, Sell this script")

		sl_price          = round((cc_1['close']*1.02),1)
		qty               = int(per_trade_margin/cc_1['close'])

		buy_entry_orderid = tsl.order_placement(stock_name,'NSE', 1, 0, 0, 'MARKET', 'SELL', 'MIS')
		sl_orderid        = tsl.order_placement(stock_name,'NSE', 1, 0, sl_price, 'STOPMARKET', 'BUY', 'MIS')
		traded_wathclist.append(stock_name)

please check this is correct or clint code is removed by me.so

please me if any error or i have do any changes in this.

1 Like

Hi @virender_singh

This code seems to be working fine,

Codebase used : Dhan_Tradehull_V2.py - Google Drive

Hi @Lokesh_Patidar

code seems to be fine, most probably its a codebase version issue

use this file : Dhan_Tradehull_V2.py - Google Drive

check when the code starts it should print “print(“Codebase Version 2.6 : Solved - Pnl Issue”)”

also if it does not work, then send the complete zip of files to tradehull_mentorship@tradehull.com

PLEASE CHECK YOUR MAIL.AND HELP ME PLEASE

@Lokesh_Patidar G
use this :point_down:
copy and paste

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
import ssl
import requests

warnings.filterwarnings("ignore") #, message="Parsing dates in.*"

# Disable SSL verification globally
ssl._create_default_https_context = ssl._create_unverified_context

# ---------------for dhan login ----------------
client_code = "123456789"
token_id    = "asdadfdfdfgdsfgsxfdggggggggggggggggggggggggggggggggggggyyyyyyyyyyyyyyyyyyyyyy"

tsl = Tradehull(client_code,token_id)

available_balance = tsl.get_balance()
leveraged_margin  = available_balance*5
max_trades = 3
per_trade_margin = (leveraged_margin/max_trades)

trade_info = {"traded_stock":None,"qty":None,"buy_sell":None,"entry_price":None,"sl":None,"take_profit_price":None}

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

while True:
	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):
		print("market is close exiting the bot",current_time)
		break


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

		chart_1 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="1")
		chart_5 = tsl.get_historical_data(tradingsymbol = stock_name.upper(),exchange = 'NSE',timeframe="5") # this call has been updated to get_historical_data call, 

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

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


		# Conditions that are on 1 minute timeframe
		chart_1['rsi'] = talib.RSI(chart_1['close'], timeperiod=14) #pandas
		cc_1           = chart_1.iloc[-2]  #pandas  completed candle of 1 min timeframe
		uptrend        = cc_1['rsi'] > 50
		downtrend      = cc_1['rsi'] < 49


		# Conditions that are on 5 minute timeframe
		chart_5['upperband'], chart_5['middleband'], chart_5['lowerband'] = talib.BBANDS(chart_5['close'], timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
		cc_5           = chart_5.iloc[-1]   # pandas
		ub_breakout    = cc_5['high'] > cc_5['upperband']
		lb_breakout    = cc_5['low'] < cc_5['lowerband']

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


		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)
			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')
			traded_wathclist.append(stock_name)

		if downtrend and lb_breakout and no_repeat_order and max_order_limit:
			print(stock_name, "is in downtrend, Sell this script")

			sl_price          = round((cc_1['close']*1.02),1)
			qty               = int(per_trade_margin/cc_1['close'])

			buy_entry_orderid = tsl.order_placement(stock_name,'NSE', 1, 0, 0, 'MARKET', 'SELL', 'MIS')
			sl_orderid        = tsl.order_placement(stock_name,'NSE', 1, 0, sl_price, 'STOPMARKET', 'BUY', 'MIS')
			traded_wathclist.append(stock_name)

@vinay_kumaar thanks sir but again same issue with me…

@Lokesh_Patidar G
Screenshot bhejiye…