Introducing: Webhooks as Order Type on Dhan. Now do Options Trading on TradingView.com

Hi Everyone, I have been successfully using Banknifty chart in TV, to trade in Options in Dhan. My strategy is a simple one, when buy signal comes it should exit from any previous long in put option and place a long in call option, and when sell signal comes, the reverse happens.
But there’s one issue, since the expiry and strike price are calculated dynamically in Pinescript, the exit position order doesn’t always go through, since my code just stores the last alert message and before placing a new order, changes the transaction type from ‘B’ to ‘S’. This is because, sometimes I update my alert after changing parameters which also includes quantity, or I might have managed my positions directly in Dhan.
To achieve full automation, i want all existing position for BankNifty options to be exited before placing new order. What would be the json message to achieve this exit requirement?
Kindly help

hi @Hardik ,
Does the sort_order input will ensure the sequence of execution in a multileg order ? E.g I have few hedge buy legs up the sequence followed by SELL legs . Want to know if SELL legs will be executed post hedge leg execution . Kindly respond please.

{‘secret’: ‘xxxx’, ‘alertType’: ‘multi_leg_order’, ‘order_legs’: [{‘transactionType’: ‘B’, ‘orderType’: ‘LMT’, ‘quantity’: ‘10’, ‘exchange’: ‘NSE’, ‘symbol’: ‘NIFTY’, ‘instrument’: ‘OPT’, ‘productType’: ‘M’, ‘sort_order’: ‘1’, ‘price’: ‘38.43’, ‘option_type’: ‘CE’, ‘strike_price’: ‘24600’, ‘expiry_date’: ‘2024-12-19’}, {‘transactionType’: ‘B’, ‘orderType’: ‘LMT’, ‘quantity’: ‘10’, ‘exchange’: ‘NSE’, ‘symbol’: ‘NIFTY’, ‘instrument’: ‘OPT’, ‘productType’: ‘M’, ‘sort_order’: ‘2’, ‘price’: ‘23.99’, ‘option_type’: ‘PE’, ‘strike_price’: ‘24000’, ‘expiry_date’: ‘2024-12-19’}, {‘transactionType’: ‘S’, ‘orderType’: ‘LMT’, ‘quantity’: ‘10’, ‘exchange’: ‘NSE’, ‘symbol’: ‘NIFTY’, ‘instrument’: ‘OPT’, ‘productType’: ‘M’, ‘sort_order’: ‘3’, ‘price’: ‘101.56’, ‘option_type’: ‘PE’, ‘strike_price’: ‘24300’, ‘expiry_date’: ‘2024-12-19’}, {‘transactionType’: ‘S’, ‘orderType’: ‘LMT’, ‘quantity’: ‘10’, ‘exchange’: ‘NSE’, ‘symbol’: ‘NIFTY’, ‘instrument’: ‘OPT’, ‘productType’: ‘M’, ‘sort_order’: ‘4’, ‘price’: ‘115.81’, ‘option_type’: ‘CE’, ‘strike_price’: ‘24300’, ‘expiry_date’: ‘2024-12-19’}]}

Hello @jacharjya1978

Yes, it is for sequential order execution. But do note that this does not guarantee execution if e.g. BUY leg itself stays in pending state, due to time taken in order matching.

Thanks for responding , so there could be a possibility BUY order pending up in the sequence , SELL leg will be tried to execute meanwhile . It could lead to margin issue.
May be I will opt MKT order for buy legs …what do you suggest ?

@Hardik Further more , can it be possible to add a wait (which may take few seconds as input) field in the json for a specific leg , so that SELL legs can be delayed for few seconds. It would help to get rid of margin issues .

Hello @jacharjya1978

This is certainly better, as LIMIT orders have a higher chance of creating this scenario.

Right now, this is not. Essentially, we will have to implement delayed order execution for legs, wherein the second leg is only sent if the first leg is successfully executed. Will have to check feasibility of this, as this will keep threads busy at our end for longer period of time.

@Hardik understood …as through webhooks we dont have much control to delay the specific legs on client side , appreciate if you consider this. Otherwise sequential execution of orders (sort_order) does not have any significance.

1 Like