import pdb
from Dhan_Tradehull import Tradehull
import pandas as pd
import talib
Trading API client setup
client_code = “********”
token_id = “token_id_used”
tsl = Tradehull(client_code, token_id)
Intraday strategy parameters
available_balance = tsl.get_balance()
leveraged_margin = available_balance * 5
max_trades = 3
per_trade_margin = leveraged_margin / max_trades
Watchlist
watchlist = [
‘MOTHERSON’, ‘OFSS’, ‘MANAPPURAM’, ‘BSOFT’, ‘CHAMBLFERT’, ‘DIXON’, ‘NATIONALUM’,
‘DLF’, ‘IDEA’, ‘ADANIPORTS’, ‘SAIL’, ‘HINDCOPPER’, ‘INDIGO’, ‘RECLTD’, ‘PNB’,
‘HINDUNILVR’, ‘TATAPOWER’, ‘BPCL’, ‘HCLTECH’, ‘WIPRO’
]
traded_watchlist =
Main loop
while True:
for stock_name in watchlist:
print(f"Processing {stock_name}…")
# Fetch intraday data
chart = tsl.get_intraday_data(stock_name, 'NSE', 1)
if chart is None or chart.empty:
print(f"No data retrieved for {stock_name}. Skipping...")
continue
# Calculate RSI
try:
chart['rsi'] = talib.RSI(chart['close'], timeperiod=14)
except TypeError:
print(f"Data is missing or incorrect for {stock_name}. Skipping...")
continue
# Ensure sufficient data for analysis
if len(chart) < 4:
print(f"Not enough data for {stock_name}. Skipping...")
continue
# Define candles for analysis
bc = chart.iloc[-2] # Breakout candle
ic = chart.iloc[-3] # Inside candle
ba_c = chart.iloc[-4] # Base candle
# Determine trend and conditions
uptrend = bc['rsi'] > 50
downtrend = bc['rsi'] < 49
inside_candle_formed = (ba_c['high'] > ic['high']) and (ba_c['low'] < ic['low'])
upper_side_breakout = bc['high'] > ba_c['high']
down_side_breakout = bc['low'] < ba_c['low']
# Print values for debugging
print(f"{stock_name} - RSI: {bc['rsi']}, uptrend: {uptrend}, downtrend: {downtrend}")
print(f"Inside candle formed: {inside_candle_formed}, Upper breakout: {upper_side_breakout}")
no_repeat_order = stock_name not in traded_watchlist
max_order_limit = len(traded_watchlist) < max_trades # Changed <= to < for limit enforcement
# Execute buy order
if uptrend and inside_candle_formed and upper_side_breakout and no_repeat_order and max_order_limit:
print(f"{stock_name} is in uptrend. Attempting to place a buy order.")
qty = int(per_trade_margin / bc['close'])
try:
buy_entry_orderid = tsl.order_placement(stock_name, 'NSE', qty, 1, 0, 0, 'MARKET', 'BUY', 'MIS')
traded_watchlist.append(stock_name)
print(f"Buy order placed for {stock_name}, order ID: {buy_entry_orderid}")
except Exception as e:
print(f"Error placing buy order for {stock_name}: {e}")
# Execute sell order
elif downtrend and inside_candle_formed and down_side_breakout and no_repeat_order and max_order_limit:
print(f"{stock_name} is in downtrend. Attempting to place a sell order.")
qty = int(per_trade_margin / bc['close'])
try:
sell_entry_orderid = tsl.order_placement(stock_name, 'NSE', qty, 1, 0, 0, 'MARKET', 'SELL', 'MIS')
traded_watchlist.append(stock_name)
print(f"Sell order placed for {stock_name}, order ID: {sell_entry_orderid}")
use this code ,make sure above stock price updated in your webshockt list.