Webhook Queries

I have a few questions regarding webhook feature.

  1. Why the 24 hours token validity is recommended for the Webhook URL?
    Is there any special advantage with 24 hours validity?
    or
    Is there any potential disadvantage or risk with longer validity of the Webhook
    URL?

  2. Do we need to log into the tradingview trading panel using Dhan credentials
    for using webhook feature?

  3. Will it still work even if we shut down our system after submitting the alerts with
    Dhan webhook URLs in Tradingview app?

Hello @trader7626

Great to know that you are exploring Dhan Webhooks.

  1. The reason for recommending 24 hour validity on Webhooks is to ensure that your alerts do not trigger orders post the 24 hour active window. This is applicable for people who use alerts and place order for one day, they might miss out some active alerts in their account which might get triggered the next day without their knowledge, no other reason for this.
  2. No. Webhooks and Trading Panel work independently. Although if you do want to use TradingView.com for monitoring your positions as well (even for orders placed via Webhooks), then this can work as a great add-on.
  3. Yes. TradingView has alerts based on clouds which work even when the platform is not opened on any devices. You can read more about it here - About TradingView alerts — TradingView India
  1. So does that mean the order placed in the Tradingview via Webhook will remain active in the system overnight also or until the validity of the Webhook URL?

  2. Is it really possible to submit multiple orders by creating multiple Webhook URLs and Alerts in the same script or multiple different scripts one after the other even before our earlier orders have executed?
    When we try to generate a new URL it flashes a message on the screen that ‘Generating new URL will disable all existing URLs’.
    So, the question is does the earlier Webhook URL and its associated Order with its Json remain active in the system or it gets deactivated and disabled when we try to create a new URL for a new order in the same or another script?

Hello @trader7626

Yes, the webhook will remain active in the system till the validity of the URL. Even your alerts set on tradingview.com work in the same way.

Yes, you can create multiple orders with a single Webhook URL itself.
Your orders can be across segments or exchanges and even multiple in number associated with a single webhook URL itself.

Do note that the message displayed when generating new URL clearly mentions that if you create a new URL, then existing alerts will get disabled. As your account is linked to one webhook URL which is in turn linked to many alert generated orders, if you generate a new URL, then existing alerts will not work.

Yes ,So my intention is to place different orders based on different alerts placed at different price points.
The alerts can be on different scripts and the corresponding orders also could be on different scripts.
e,.g I placed a conditional order in MRF Futures based on a particular alert at a particular price point on the MRF Stock. Then I want to place another order in Axisbank Futures based on a different alert on Axis bank stock at a particular price point. Then should I not change the webhook URL for Axis bank Alert for its corresponding order?
Does that mean I should not change the URL for placing every different order in the same script or even on the different script for different alerts?
I noticed that every time I click on manage button on the Webhook URL page the same URL gets populated but the validity always reverts back to 24 hours even if for the previous alert I had used it with 30 days validity.
Should I use the same URL and not try to change it?

@trader7626

Yes, you should use the same URL for all the alerts, no matter which segment or order type it is.

  1. So that means once a URL is pasted on the notifications page of the first alert that we create the next time we should only select a new script and create a new json for our next order?
    Is that how we should place multiple orders just by changing the script and the corresponding Json and not bothering with the Webhook URL?

  2. How to delete the webhook URL from the Alert Window?
    I also noticed that once we paste a webhook URL in the designated box on the Alert window, every new Alert window on the same script or even on any other script starts displaying the last used webhook URL by default and it does not allow us to submit a simple Alert without a webhook URL.
    If we try to delete the URL and create and submit the alert without the URL it displays a message that Alert cannot be submitted without Webhook URL.
    In future for some alerts if we do not want our alert to be associated with the webhook URL obtained from Dhan then what do we do?
    How do we dissociate our alerts on Tradingview from Dhan webhook URL?

  3. How do we keep track of the expiry of the URL?
    Is there any notification or message that flashes on the Web platform of Dhan that our last generated URL has expired and we need to create a new URL?

  4. How and where do we track the orders which we have submitted through a webhook?
    After submission of the Webhook URL and the Json in the Alert configuration window, when we change to another page on Dhan web portal and come back again on the Webhook page, we find it is clean and ready to accept another webhook order. The previously submitted webhook orders are not seen in pending orders page or anywhere else on the Dhan Web portal.
    How do we track those orders and the corresponding Jsons as the webhooks are likely to expire very soon based on their validity?
    After submitting multiple orders on various scripts how do we remember in which scripts we have placed the orders?
    How many total orders have we placed through the last WEbhook URL?
    If we want to review some of the orders say if we want to modify some of those orders any time before execution in terms of quantity or price then how do we do it? Or
    if we change our mind for some scripts and we want to cancel some of the orders how do we do it and also where and how do we get the record or a list of orders submitted through webhook?

Hello @trader7626

  1. Yes, this is how the intended flow is. In fact you can even have multiple orders for same script (instrument) as well.
  2. If it is a simple alert, then as long as you do not place order JSON in the message box, your order will not get executed on Dhan. Meanwhile, this error is from TradingView end. You can actually unclick checkbox for webhook URL on TradingView alert window to proceed without Webhook URL and yet create an alert.

    Unclick this checkbox to create simple alert.
  3. No, this is not present right now. However, it is a great feedback, will try to implement this.
  4. All of this has to be managed from TradingView alert window. Once alert is generated and we receive order at Dhan, then you can manage the orders just like any other order on Dhan.

You have mentioned that all this has to be managed from the Tradingview Alert window but even on Tradingview Alert window how to keep track of Expiry of the Webhook URL ?
I mean on the alerts window we do not get to see the expiry date of the URL anywhere.

a) When we revisit the charts on the Tradingview and open those alert configuration windows how do we know that the URL pasted in that notification page is still valid and not expired?

b) How do we know that the Json that is visible in the message window is also valid, active and correct for the order?

c) Is there any connection between the Webhook URL and the Json also?
I mean if we change the webhook URL in the notification page of the Alert window then is it necessary to create a new Json also for the same order that we had previously submitted?

Hello @trader7626

  1. Agreed. The validity of URL cannot be checked on TradingView panel. But since, at any given point of time, only one Webhook URL will be active, you can always confirm whether only active Webhook URL is pasted on TradingView alerts or not.
  2. This has to be ensured at your end. It will be difficult for us to store all created order JSONs at scale, also this beats the purpose of Webhook orders which can be placed without even coming to Dhan Platform.
  3. Yes. There is a secret code that links Webhook URL to JSON, which also needs to be changed when URL is changed.

Following is an example of a Json code.

{
“secret”: “JY97a”,
“alertType”: “multi_leg_order”,
“order_legs”: [
{
“transactionType”: “S”,
“orderType”: “MKT”,
“quantity”: “1”,
“exchange”: “NSE”,
“symbol”: “TVSMOTOR1!”,
“instrument”: “FUT”,
“productType”: “M”,
“sort_order”: “1”,
“price”: “0”
}
]
}

Can somebody explain what is the meaning of following components of Json.

secret : JY97a

productType : M

sort_order : 1

Hello @trader7626

Here are the JSON keys explained:

  1. secret - this acts as a security check for receiving message, relating to Webhook URL and your account
  2. productType - this informs system whether the trade is Intraday or Carryforward
  3. sort_order - internal tag to check sequence of order execution (in case of multi-leg orders)

When we create an order for options trade we get a chance to select the expiry of the contract and the same expiry is also mentioned in the Json code also.

Similarly if for any order in a Futures contract if we add the expiry of the next month futures contract then is it possible to execute the order for the next month futures contract using the Webhook and Json?

Hello @trader7626

No, currently not. We have only mapped current month futures for execution via Webhooks. Hence, next month futures cannot be traded via this method directly as of now.

Hi team,

Are the webhooks only recognised when shot by trading view alerts or can we map the webhook from some other avenues to place position

Hello @Vivekgowda

Currently, webhooks are configured only to work with alerts from TradingView.com. We are looking into extending it to other platforms as well. Can you confirm which particular platform are you trying to integrate/connect with?

I need a suggestion regarding webhook integration,
I have integrated webhook into the strategy indicator, When strategy is generating a Buy/Sell signal, orders are placed in Dhan, but the issue is when strategy is generating an Exit signal, Open order in Dhan is not Exiting/Closing.
Please check the below logic and webhook Json script.

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”}]}’

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”}]}’

Below is the Strategy logic:

if (strategy.position_size == 0)
if (longEntryCondition)
strategy.order(“Long”, strategy.long, alert_message=long_msg)
if (shortEntryCondition)
strategy.order(“Short”, strategy.short, alert_message=short_msg)

if (strategy.position_size > 0)
if (shortEntryCondition)
strategy.order(“Short”, strategy.short, qty=size, alert_message=short_msg)
else
strategy.exit(“Long Exit”, from_entry=“Long”, qty=size, stop=useStopLoss, limit=useTakeProfit, alert_message=long_exit_msg)

if (strategy.position_size < 0)
if (longEntryCondition)
strategy.order(“Long”, strategy.long, qty=size, alert_message=long_msg)
else
strategy.exit(“Short Exit”, from_entry=“Short”, qty=size, stop=useStopLoss, limit=useTakeProfit, alert_message=short_exit_msg)

// 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)

@Prudhvi_Prasanth

U have exit conditions inside the if and outside it. The outside exit conditions don’t have the alert message JSON. So orders may not be firing. Am not sure based on the limited data u shared what else the issue can be.

Also check in chart and trade list whether your entry and exit is happening correctly before looking to fire orders in Dhan.

Hope this helps.

1 Like

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!!

@Prudhvi_Prasanth please check these :point_up_2:

Also I have a sample code frame work here. Try to follow this structure.

1 Like