import time
import datetime
import traceback
import pandas as pd
import talib
from Dhan_Tradehull import Tradehull
client_code = βHiβ
token_id = βHelloβ
tsl = Tradehull(client_code, token_id)
Use only 5k from balance for this strategy
available_balance = 5000
leveraged_margin = available_balance * 5
max_trades = 1
per_trade_margin = leveraged_margin / max_trades
max_loss = (available_balance * 1) / 100 * -1
max_profit = (available_balance * 5) / 100
buy_traded = False
sell_traded = False
instrument = βNIFTYβ
while True:
current_time = datetime.datetime.now().time()
live_pnl = tsl.get_live_pnl()
if current_time < datetime.time(9, 30):
print("π Waiting for market to open...", current_time)
time.sleep(2)
continue
if current_time >= datetime.time(15, 15) or live_pnl <= max_loss or live_pnl >= max_profit or (buy_traded and sell_traded):
tsl.cancel_all_orders()
print("π Strategy stopped: Max limit/time reached.")
break
try:
chart = tsl.get_historical_data(tradingsymbol=instrument, exchange='NSE', timeframe="5")
# Ensure we have enough candles to calculate indicators and logic
if chart is None or chart.empty or len(chart) < 10:
print("π Not enough data yet...")
time.sleep(5)
continue
chart['ema_20'] = talib.EMA(chart['close'], timeperiod=20)
chart['ema_200'] = talib.EMA(chart['close'], timeperiod=200)
chart['rsi'] = talib.RSI(chart['close'], timeperiod=14)
cc = chart.iloc[-2]
# Buy Condition
ema_buy = cc['close'] > cc['ema_20'] and cc['close'] > cc['ema_200']
rsi_buy = cc['rsi'] > 55
breakout_buy = cc['close'] > chart['high'].iloc[-6:-1].max()
avg_vol = chart['volume'].iloc[-6:-1].mean()
volume_buy = cc['volume'] > avg_vol
# Sell Condition
ema_sell = cc['close'] < cc['ema_20'] and cc['close'] < cc['ema_200']
rsi_sell = cc['rsi'] < 45
breakdown_sell = cc['close'] < chart['low'].iloc[-6:-1].min()
volume_sell = cc['volume'] > avg_vol
if ema_buy and rsi_buy and breakout_buy and volume_buy and not buy_traded:
print("π BUY CALL - NIFTY 50 INDEX")
sl_price = round(cc['close'] * 0.985, 1)
qty = 1
tsl.order_placement(instrument, 'NSE', qty, 0, 0, 'MARKET', 'BUY', 'MIS')
tsl.order_placement(instrument, 'NSE', qty, 0, sl_price, 'STOPMARKET', 'SELL', 'MIS')
buy_traded = True
if ema_sell and rsi_sell and breakdown_sell and volume_sell and not sell_traded:
print("π BUY PUT - NIFTY 50 INDEX")
sl_price = round(cc['close'] * 1.015, 1)
qty = 1
tsl.order_placement(instrument, 'NSE', qty, 0, 0, 'MARKET', 'SELL', 'MIS')
tsl.order_placement(instrument, 'NSE', qty, 0, sl_price, 'STOPMARKET', 'BUY', 'MIS')
sell_traded = True
except Exception as e:
print("β Error:", e)
traceback.print_exc()
time.sleep(5)
continue
This is the code after running it, I am getting this>>>π Not enough data yetβ¦
Is it my trade condition, not hitting or something else that I donβt know?
And sorry, please help me, where can I put my doubts and queries?
Dhanyawaad,
Shashiprakash.