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")
# ---------------for dhan login ----------------
client_code = "11047"
token_id = "eyJ0eXAiOiJK"
tsl = Tradehull(client_code,token_id)
traded = "no"
trade_info = {"options_name":None, "qty":None, "sl":None, "CE_PE":None}
reward_risk_ratio = 3
while True:
current_time = datetime.datetime.now()
index_chart = tsl.get_historical_data(tradingsymbol='NIFTY JAN FUT', exchange='NFO', timeframe="5")
time.sleep(5)
index_ltp = tsl.get_ltp_data(names = ['NIFTY JAN FUT'])['NIFTY JAN FUT']
# if (index_chart.empty):
# time.sleep(60)
# continue
# rsi ------------------------ apply indicators
index_chart['rsi'] = talib.RSI(index_chart['close'], timeperiod=14)
# vwap
index_chart.set_index(pd.DatetimeIndex(index_chart['timestamp']), inplace=True)
index_chart['vwap'] = pta.vwap(index_chart['high'] , index_chart['low'], index_chart['close'] , index_chart['volume'])
# Supertrend
indi = ta.supertrend(index_chart['high'], index_chart['low'], index_chart['close'], 10, 2)
index_chart = pd.concat([index_chart, indi], axis=1, join='inner')
# vwma
index_chart['pv'] = index_chart['close'] * index_chart['volume']
index_chart['vwma'] = index_chart['pv'].rolling(20).mean() / index_chart['volume'].rolling(20).mean()
# volume
volume = 50000
first_candle = index_chart.iloc[-3]
second_candle = index_chart.iloc[-2]
running_candle = index_chart.iloc[-1]
# ---------------------------- BUY ENTRY CONDITIONS ----------------------------
bc1 = first_candle['close'] > first_candle['vwap'] # First Candle close is above VWAP
bc2 = first_candle['close'] > first_candle['SUPERT_10_2.0'] # First Candle close is above Supertrend
bc3 = first_candle['close'] > first_candle['vwma'] # First Candle close is above VWMA
bc4 = first_candle['rsi'] < 80 # First candle RSI < 80
bc5 = second_candle['volume'] > 50000 # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
bc6 = traded == "no"
bc7 = index_ltp > first_candle['low']
print(f"BUY \t {current_time} \t {bc1} \t {bc2} \t {bc3} \t {bc4} \t {bc5} \t {bc6} \t {bc7} \t first_candle {str(first_candle['timestamp'].time())}")
# ---------------------------- SELL ENTRY CONDITIONS ----------------------------
sc1 = first_candle['close'] < first_candle['vwap'] # First Candle close is below VWAP
sc2 = first_candle['close'] < first_candle['SUPERT_10_2.0'] # First Candle close is below Supertrend
sc3 = first_candle['close'] < first_candle['vwma'] # First Candle close is below VWMA
sc4 = first_candle['rsi'] > 20 # First candle RSI < 80
sc5 = second_candle['volume'] > 50000 # Second candle Volume should be greater than 50,000 for Nifty and above 125,000 for Bank Nifty
sc6 = traded == "no"
sc7 = index_ltp < first_candle['high']
print(f"SELL \t {current_time} \t {sc1} \t {sc2} \t {sc3} \t {sc4} \t {sc5} \t {sc6} \t {sc7} \t first_candle {str(first_candle['timestamp'].time())} \n")
if bc1 and bc2 and bc3 and bc4 and bc5 and bc6 and bc7:
print("Buy Signal Formed")
ce_name, pe_name, ce_strike, pe_strike = tsl.OTM_Strike_Selection('NIFTY','23-1-2025',2)
ce_ltp = tsl.get_ltp_data(names = [ce_name])[ce_name]
lot_size = tsl.get_lot_size(ce_name)*1
entry_orderid = tsl.order_placement(ce_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = ce_name
trade_info['qty'] = lot_size
sl_percentage = 15 / 100 # 15%
target_percentage = 30 / 100 # 30%
trade_info['sl'] = ce_ltp * (1 - sl_percentage)
trade_info['target'] = ce_ltp * (1 + target_percentage)
trade_info['CE_PE'] = "CE"
if sc1 and sc2 and sc3 and sc4 and sc5 and sc6 and sc7:
print("Sell Signal Formed")
ce_name, pe_name, ce_strike, pe_strike = tsl.OTM_Strike_Selection('NIFTY','23-1-2025',2)
pe_ltp = tsl.get_ltp_data(names = [pe_name])[pe_name]
lot_size = tsl.get_lot_size(pe_name)*1
entry_orderid = tsl.order_placement(pe_name,'NFO', lot_size, 0, 0, 'MARKET', 'BUY', 'MIS')
traded = "yes"
trade_info['options_name'] = pe_name
trade_info['qty'] = lot_size
sl_percentage = 15 / 100 # 15%
target_percentage = 30 / 100 # 30%
trade_info['sl'] = ce_ltp * (1 - sl_percentage)
trade_info['target'] = ce_ltp * (1 + target_percentage)
trade_info['CE_PE'] = "PE"
# ---------------------------- check for exit SL/TG
if traded == "yes":
long_position = trade_info['CE_PE'] == "CE"
short_position = trade_info['CE_PE'] == "PE"
if long_position:
options_ltp = tsl.get_ltp_data(names = [trade_info['options_name']])[trade_info['options_name']]
sl_hit = options_ltp < trade_info['sl']
trailing_tg_hit = index_ltp < running_candle['SUPERT_10_2.0'] # this is a trailing target, by supertrend
tg_hit = options_ltp > trade_info['target']
if sl_hit or trailing_tg_hit or tg_hit:
print("Order Exited", trade_info)
exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
pdb.set_trace()
if short_position:
options_ltp = tsl.get_ltp_data(names = [trade_info['options_name']])[trade_info['options_name']]
sl_hit = options_ltp > trade_info['sl']
trailing_tg_hit = index_ltp > running_candle['SUPERT_10_2.0'] # this is a trailing target, by supertrend
tg_hit = options_ltp < trade_info['target']
if sl_hit or trailing_tg_hit or tg_hit:
print("Order Exited", trade_info)
exit_orderid = tsl.order_placement(trade_info['options_name'],'NFO', trade_info['qty'], 0, 0, 'MARKET', 'SELL', 'MIS')
pdb.set_trace()
Sir, in this code it shows this error
" Exception at calling ltp as {‘status’: ‘failure’, ‘remarks’: {‘error_code’: None, ‘error_type’: None, ‘error_message’: None}, ‘data’: {‘data’: {‘805’: ‘Too many requests. Further requests may result in the user being blocked.’}, ‘status’: ‘failed’}}
Traceback (most recent call last):
File “d:/Python/My Strategy/two candle theory.py”, line 134, in
options_ltp = tsl.get_ltp_data(names = [trade_info[‘options_name’]])[trade_info[‘options_name’]]
KeyError: ‘NIFTY 23 JAN 23200 CALL’
"
And then the code is stops, Please give me fix sir, why it is not tracking, Target and SL?
Thanks.