Hi @Kalpeshh_Patel
Also for the pseudocode, do carefully fill in any gaps, fix any errors, and implement it into a working program.
Also same changes needs to be made PE side as well.
import pdb
import traceback
import time
import datetime
from Dhan_Tradehull_V2 import Tradehull
import pandas as pd
from pprint import pprint
import pandas_ta as pta
import talib
import pandas_ta as ta
import warnings
warnings.filterwarnings("ignore")
# ---------------for dhan login ----------------
client_code = "xxxxxxxxx"
token_id = "xxxxxxxx"
tsl = Tradehull(client_code,token_id)
available_balance = tsl.get_balance()
print("available_balance", available_balance)
index_name = 'BANKNIFTY NOV FUT'
traded = "no"
trade_info = {"options_name":None, "qty":None, "sl":None, "CE_PE":None, "entry_price":None , "Trailed":None, "entry_orderid":None}
while True:
current_time = datetime.datetime.now().time()
if current_time < datetime.time(9,20):
print("wait for market to start", current_time)
continue
if (current_time > datetime.time(23, 25)):
print("Market is over, Bye Bye see you tomorrow", current_time)
break
index_chart = tsl.get_historical_data(tradingsymbol=index_name, exchange='NFO', timeframe="15")
time.sleep(3)
index_chart_5 = tsl.get_historical_data(tradingsymbol = index_name,exchange = 'NFO',timeframe="5")
index_ltp = tsl.get_ltp_data(names = ['NIFTY NOV FUT','BANKNIFTY NOV FUT'])
if index_chart.empty or index_chart_5.empty:
time.sleep(60)
continue
index_chart['rsi'] = talib.RSI(index_chart['close'], timeperiod=14) #pandas
supertrend = ta.supertrend(index_chart_5['high'], index_chart_5['low'], index_chart_5['close'], 10, 3)
index_chart_5 = pd.concat([index_chart_5, supertrend], axis=1, join='inner')
pdb.set_trace()
# time.sleep(60)
# continue
# print(index_chart,'scanning',)
cc_1 = index_chart.iloc[-1] #pandas completed candle of 15 min timeframe
cc_2 = index_chart.iloc[-2]
cc_3 = index_chart.iloc[-3]
cc5_2 = index_chart_5.iloc[-2]
# ---------------------------- BUY ENTRY CONDITIONS ----------------------------
up1 = cc_2['rsi'] > 60
up2 = cc_3['rsi'] < 60
up3 = cc_2['high'] < cc_1['close']
print(f"BUY\t {current_time} \t {up1} \t {up2} \t {up3} \t cc_1 {str(cc_1['timestamp'].time())}")
# pdb.set_trace()
# ---------------------------- SELL ENTRY CONDITIONS ----------------------------
dt1 = cc_2['rsi'] < 90 # This is for tral basis
dt2 = cc_3['rsi'] > 90 # This is for tral basis
dt3 = cc_2['low'] > cc_1['close']
print(f"SELL\t {current_time} \t {dt1} \t {dt2} \t {dt3} \t cc_1 {str(cc_1['timestamp'].time())} \n")
if up1 and up2 and up3:
print(index_name, "Buy CALL")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
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
trade_info['sl'] = cc_2['low']
trade_info['CE_PE'] = "CE"
trade_info['entry_orderid']= entry_orderid
time.sleep(1)
trade_info['entry_price'] = tsl.get_executed_price(orderid=trade_info['entry_orderid'])
if dt1 and dt2 and dt3:
print(index_name, "Buy PUT")
ce_name, pe_name, strike = tsl.ATM_Strike_Selection(Underlying ='NIFTY',Expiry ='28-11-2024')
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
trade_info['sl'] = cc_2['high']
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:
price_has_moved_20_pct = index_ltp > (trade_info['entry_price'])*1.2
position_has_not_been_trailed = trade_info['Trailed'] is None
if price_has_moved_20_pct and position_has_not_been_trailed:
trade_info['sl'] = trade_info['entry_price']
trade_info['Trailed'] = "yes_I_have_trailed"
sl_hit = index_ltp < trade_info['sl']
tg_hit = index_ltp < cc5_2['SUPERT_10_3.0']
if sl_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:
price_has_moved_20_pct = index_ltp > (trade_info['entry_price'])*1.2
position_has_not_been_trailed = trade_info['Trailed'] is None
if price_has_moved_20_pct and position_has_not_been_trailed:
trade_info['sl'] = trade_info['entry_price']
trade_info['Trailed'] = "yes_I_have_trailed"
sl_hit = index_ltp < trade_info['sl']
tg_hit = index_ltp < cc5_2['SUPERT_10_3.0']
if sl_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()