I can share you the complete script, Today i gave another shot, but exit didn’t work, entry got fired.
Script:
//@version=5
strategy(“Strategy with Dhan Integration”, overlay=true)
//Risk Management Settings
groupRisk = “Risk Management”
inpTakeProfit = input.int(defval=15000, title=“Take Profit”, minval=0, group=groupRisk)
inpStopLoss = input.int(defval=3000, title=“Stop Loss”, minval=0, group=groupRisk)
inpTrailStop = input.int(defval=100, title=“Trailing Stop Loss”, minval=0, group=groupRisk)
inpTrailOffset = input.int(defval=1, title=“Trailing Stop Loss Offset”, minval=0, group=groupRisk)
// Core Strategy Logic
//Can’t disclose it//
//Risk Management Values
useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na
//Auto Strike Selection Settings
opt_level = input.int(2, title=“Option Depth Level”, minval=1)
upBound_buy = ta.highest(high, 20)
upBound_sell = ta.highest(high, 20)
downBound_short = ta.lowest(low, 20)
downBound_cover = ta.lowest(low, 20)
callbuytext = math.floor(upBound_buy / 100) * 100 - opt_level * 100
callselltext = math.floor(upBound_sell / 100) * 100 - opt_level * 100
putbuytext = math.ceil(downBound_short / 100) * 100 + opt_level * 100
putselltext = math.ceil(downBound_cover / 100) * 100 + opt_level * 100
// Buy and Sell Strategy Conditions//
longEntryCondition = LOGIC
shortEntryCondition = LOGIC
// Position size to exit position
size = math.abs(strategy.position_size)
//Dhan Webhook Integration
long_msg = ‘{“secret”:“XXXX”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“BANKNIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“CE”,“strike_price”:"’ + str.tostring(callbuytext) + ‘.0",“expiry_date”:“2024-10-16”}]}’
short_msg = ‘{“secret”:“XXXX”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“BANKNIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“PE”,“strike_price”:"’ + str.tostring(putbuytext) + ‘.0",“expiry_date”:“2024-10-16”}]}’
long_exit_msg = ‘{“secret”:“XXXX”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“S”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“BANKNIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“CE”,“strike_price”:"’ + str.tostring(callselltext) + ‘.0",“expiry_date”:“2024-10-16”}]}’
short_exit_msg = ‘{“secret”:“XXXX”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“S”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“BANKNIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“PE”,“strike_price”:"’ + str.tostring(putselltext) + ‘.0",“expiry_date”:“2024-10-16”}]}’
// Submit Orders Based on Signals//
if (strategy.position_size == 0)
if (longEntryCondition)
strategy.order(“Long”, strategy.long)
alert(long_msg, freq=alert.freq_all) // Trigger alert for long entry
if (shortEntryCondition)
strategy.order(“Short”, strategy.short)
alert(short_msg, freq=alert.freq_all) // Trigger alert for short entry
if (strategy.position_size > 0)
if (shortEntryCondition)
strategy.order(“Short”, strategy.short, qty=size)
alert(short_msg, freq=alert.freq_all) // Trigger alert for short entry
else
strategy.exit(“Long Exit”, from_entry=“Long”, qty=size, stop=useStopLoss, limit=useTakeProfit)
alert(long_exit_msg, freq=alert.freq_all) // Trigger alert for long exit
if (strategy.position_size < 0)
if (longEntryCondition)
strategy.order(“Long”, strategy.long, qty=size)
alert(long_msg, freq=alert.freq_all) // Trigger alert for long entry
else
strategy.exit(“Short Exit”, from_entry=“Short”, qty=size, stop=useStopLoss, limit=useTakeProfit)
alert(short_exit_msg, freq=alert.freq_all) // Trigger alert for short exit
// Risk Management Exits
strategy.exit(“Exit Long”, from_entry=“Long”, profit=useTakeProfit, loss=useStopLoss, trail_points=useTrailStop, trail_offset=useTrailOffset)
strategy.exit(“Exit Short”, from_entry=“Short”, profit=useTakeProfit, loss=useStopLoss, trail_points=useTrailStop, trail_offset=useTrailOffset)
//END//
and in the Alert settings under the message placeholder i have pasted {{strategy.order.alert_message}} and alert() function calls only
and in the entry and exit strategy i have used, alert(freq=alert.freq_all)
Please help me resolve this, that would be of great help.
and i have used few trading platform but i would like to appreciate the Dhan platform and support team for the fabulous experience.
Thank You!!