H @babji3
use below pseudocode, to maintain reward_risk_ratio
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 = "1102790337"
token_id = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJkaGFuIiwicGFydG5lcklkIjoiIiwiZXhwIjoxNzMyOTQzNzg5LCJ0b2tlbkNvbnN1bWVyVHlwZSI6IlNFTEYiLCJ3ZWJob29rVXJsIjoiIiwiZGhhbkNsaWVudElkIjoiMTEwMjc5MDMzNyJ9.hk6DAzd9YWb06hgFX9u7_Omq0srX2ofsgOp1qf2V7DIMT5OfVGN2IJds3MumsMh3wVon--TJSB9dmm5P6T3KWQ"
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 DEC FUT', exchange='NFO', timeframe="5")
time.sleep(5)
index_ltp = tsl.get_ltp_data(names = ['NIFTY DEC FUT'])['NIFTY DEC 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','05-12-2024',4)
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'] = first_candle['low']
sl_points = first_candle['close'] - first_candle['low']
trade_info['target'] = first_candle['close'] + reward_risk_ratio*sl_points
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','05-12-2024',4)
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'] = first_candle['high']
sl_points = first_candle['high'] - first_candle['close']
trade_info['target'] = first_candle['close'] - reward_risk_ratio*sl_points
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:
sl_hit = index_ltp < trade_info['sl']
trailing_tg_hit = index_ltp < running_candle['SUPERT_10_2.0'] # this is a trailing target, by supertrend
tg_hit = index_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:
sl_hit = index_ltp > trade_info['sl']
trailing_tg_hit = index_ltp > running_candle['SUPERT_10_2.0'] # this is a trailing target, by supertrend
tg_hit = index_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()
# trade_info = {
# 'CE_PE': 'PE',
# 'options_name': 'NIFTY 21 DEC 23350 PUT',
# 'qty': 25,
# 'sl': 23357.95
# }