//@version=5
strategy(“XXXX Strategy with Dhan Integration”, overlay=true)
//Webhook Settings
groupWebhook = “Webhook Settings”
secretCode = input.string(defval=“”, title=“Secret Code”, group=groupWebhook)
symbol = input.string(defval=“NIFTY”, title=“Symbol”, options=[“NIFTY”, “FINNIFTY”, “BANKNIFTY”, “MIDCPNIFTY”, “BANKEX”, “SENSEX”], group=groupWebhook)
quantity = input.int(defval=1, title=“Lots”, minval=1, group=groupWebhook)
expiryDate = input.string(defval=“2024-10-17”, title=“Expiry Date”, group=groupWebhook)
//Auto Strike Selection Settings
opt_level = input.int(1, title=“Option Depth Level”, minval=1)
// Define the strike price step based on the selected symbol
var float strikeStep = na
if (symbol == “BANKNIFTY” or symbol == “SENSEX” or symbol == “BANKEX”)
strikeStep := 100
else if (symbol == “NIFTY” or symbol == “FINNIFTY”)
strikeStep := 50
else if (symbol == “MIDCPNIFTY”)
strikeStep := 25
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 / strikeStep) * strikeStep - opt_level * strikeStep
callselltext = math.floor(upBound_sell / strikeStep) * strikeStep - opt_level * strikeStep
putbuytext = math.ceil(downBound_short / strikeStep) * strikeStep + opt_level * strikeStep
putselltext = math.ceil(downBound_cover / strikeStep) * strikeStep + opt_level * strikeStep
// 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)
// Logic Settings
XXXXXXXXXXX
//Risk Management Values
XXXXXXXXXXX
//Buy and Sell Strategy Conditions
XXXXXXXXXXXX
size = math.abs(strategy.position_size) // Find absolute value of position size to exit position properly
// Determine the exchange based on the selected symbol
var string exchange = “”
if (symbol == “NIFTY” or symbol == “BANKNIFTY” or symbol == “FINNIFTY” or symbol == “MIDCPNIFTY”)
exchange := “NSE”
else if (symbol == “BANKEX” or symbol == “SENSEX”)
exchange := “IDX”
//Dhan Webhook Messages - Dynamic Construct
long_msg = ‘{“secret”:"’ + secretCode + ‘“,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:”’ + str.tostring(quantity) + ‘“,“exchange”:”’ + exchange + ‘“,“symbol”:”’ + symbol + ‘“,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“CE”,“strike_price”:”’ + str.tostring(callbuytext) + ‘.0",“expiry_date”:"’ + expiryDate + ‘"}]}’
short_msg = ‘{“secret”:"’ + secretCode + ‘“,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:”’ + str.tostring(quantity) + ‘“,“exchange”:”’ + exchange + ‘“,“symbol”:”’ + symbol + ‘“,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“PE”,“strike_price”:”’ + str.tostring(putbuytext) + ‘.0",“expiry_date”:"’ + expiryDate + ‘"}]}’
long_exit_msg = ‘{“secret”:"’ + secretCode + ‘“,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“S”,“orderType”:“MKT”,“quantity”:”’ + str.tostring(quantity) + ‘“,“exchange”:”’ + exchange + ‘“,“symbol”:”’ + symbol + ‘“,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“CE”,“strike_price”:”’ + str.tostring(callselltext) + ‘.0",“expiry_date”:"’ + expiryDate + ‘"}]}’
short_exit_msg = ‘{“secret”:"’ + secretCode + ‘“,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“S”,“orderType”:“MKT”,“quantity”:”’ + str.tostring(quantity) + ‘“,“exchange”:”’ + exchange + ‘“,“symbol”:”’ + symbol + ‘“,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“PE”,“strike_price”:”’ + str.tostring(putselltext) + ‘.0",“expiry_date”:"’ + expiryDate + ‘"}]}’
//Date Range Settings
groupDateRange = “Date Range for Backtesting”
startDate = input.time(timestamp(“2023-01-01 00:00”), title=“Start Date”, group=groupDateRange)
endDate = input.time(timestamp(“2024-12-31 23:59”), title=“End Date”, group=groupDateRange)
// Only run the strategy if the current time is within the date range
inDateRange = (time >= startDate and time <= endDate)
//Submit Orders Based on Signals
if (inDateRange and strategy.position_size == 0) // No active position
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 (inDateRange and strategy.position_size > 0) // Long position active
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_message=long_exit_msg) // Trigger alert for long exit
if (inDateRange and strategy.position_size < 0) // Short position active
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_message=short_exit_msg) // Trigger alert for short exit
//Risk Management Exits (Outside Alerts)
if (inDateRange)
strategy.exit(“Exit Long”, from_entry=“Long”, profit=useTakeProfit, loss=useStopLoss, trail_points=useTrailStop, trail_offset=useTrailOffset, alert_message=long_exit_msg)
strategy.exit(“Exit Short”, from_entry=“Short”, profit=useTakeProfit, loss=useStopLoss, trail_points=useTrailStop, trail_offset=useTrailOffset, alert_message=short_exit_msg)
// Alert message - {{strategy.order.alert_message}}
I have checked for extra quotes in the webhook construct, but i couldn’t find the issue, It would be so helpful if you could point it out
And I hope other traders find this structure helpful, all they need to do is to enter there core logic in.
Thank You!!