Unable to reverse positions using the general webhook & json for buy and sell with double qty

Hello dhan community,
I had developed a strategy which is givingme good returns. But the problem I am facing is that I am unable to reverse positions via. webhook. Initially I tried reversing the position using double quantity opposite side order, let’s say I was having a long position open with 30 qty, so in order to reverse it y algo was placing a sell order of 60 qty. But when this order gets triggered in dhan, app is showing me margin shortage. After this, I tried reversing the position by different lines of code for example I have an open long position with 30 qty and I want to reverse it I shall be placing first a close position command like strategy.exit(“Long”, qty = 30), then I shall be placing the new entry position by strategy.entry(“Short”, qty = 30, strategy.short). When I tried this both order reaches dhan almost at the same time. That means before dhan app could exit my position a new order for entry fires thus again ending up with margin shortage. Please hep, I need to ensure smooth reversal of my positions for my algo to work perfectly.

1 Like

Hello @elishablacksmith07

There wouldn’t be any extra margin requirement in case of selling double quantity. If this is options, then your sell margin is higher than the buy margin requirement, and hence this issue might arise. In which segment are you trying to place the order here?

I am trading in futures, but I have tried everything possible to avoid this but no result so far. For example a position is open already with 15 qty then to reverse it i place a sell order of -30 qty to make sure first to close the open position then to initiate a short position. But simply dhan rejects the order. I even tried adding delay to my strategy and placing separate orders for the open and close position yet it went in vain. What to do ?

Hello @elishablacksmith07

Can you help me with the delay timing between the orders? Also, what is the reason for rejection, if shown on Dhan Web?

delay_fxn(int i) =>
var int xx1 = i
var int target_h = hour(timenow)
var int target_m = minute(timenow)
var int target_s = second(timenow) + i
// Handle seconds overflow
if target_s >= 60
target_s := target_s - 60
target_m := target_m + 1
// Handle minutes overflow
if target_m >= 60
target_m := target_m - 60
target_h := target_h + 1
// Handle hours overflow
if target_h >= 24
target_h := target_h - 24
// Wait until the target timestamp is reached
while hour(timenow) != target_h or minute(timenow) != target_m or second(timenow) != target_s
xx1 := xx1 + 1

this is the delay function I am using. But the problem is margin requirement. For eg an order is fired to close long, by the time this gets executed before that another order is fired which gets rejected due to margin shortfall, I added delay expecting the order to be filled before a new order is fired but seems delay function do not work in strategy. i shall be trying converting my strategy to indicator as many have suggested me. But still there has got to be some json code to fire reverse position order directly via pinescript.

@elishablacksmith07

The JSON for reverse order is simple, which you already have tried before. The issue with the same is margin calculation logic. For reversing position, you will need to ensure that you have adequate margin for the newly created position as well (in case of single order with double quantity).

Indicators are simpler to integrate webhooks with, also reverse order can be easily placed as two alerts there.

That is the problem na sir, we need double margins to reverse positions when trying it from pine using double qty. While same can be easily executed with same margin if we can ensure first the open position is closed and then open the opposite position. Also, when we do reverse position directly from dhan app, no such issue happen. that is why i am asking is there any method to send directly json code of reversing positions

Hello @elishablacksmith07

Noted. Will try to replicate this scenario with double quantities. Also, will revisit how reverse order is done via app and implement similar module for Webhooks as well.

@elishablacksmith07 i have raised similar issues. You can find here; Request for allow to Trade with SL/SLM Order with 2X Qty in Future

As many users are facing this issue but from above link community Manager, have clarified that it’s will prevent to place reversal order with 2x qty.

Have you found out anything sirr? my code is lagging because of only this small issue and I am unable to make good profits in market because of this issue.

Hello @elishablacksmith07

Yes, checked it on our end, and it should block additional margin for 1 new sell position only.

Let’s take this scenario. The buy margin for an option contract is 100 and sell margin is 200.
Assuming you have a long position, Rs. 100 is already blocked in this case. Now, when you are going to reverse the position with double quantity i.e. 2 lots of contract, it will require margin of Rs. 200.

Do note that Rs. 100 will anyways be blocked till this order is executed and you will need to have additional Rs. 200 to execute the order. This implies you will need to have Margin available as Rs. 200 while reversing the order.

You can try this today and if the same issue exists, will check logs for your ID.

This is ofcourse known to me sir, i needed a mechanism that this double margin requirement do not happen. please if you found out anything in that regards please tell me. No resolution to my issue. I already know double margin is required to do as desired in dhan. if I am selling double quantity then first my open position should close and thus relieving my margins and then new position should be initiated but that is just not happening. You simply tell me how do i reverse position via pine without double quantity. Is there any json code for reversing position like the button available on the dhan app? need to reverse positions, this double margin do not come into picture when we click on reverse position which imply this has got nothing to do with exchange but only with the broker

basically you are trying to tell me try with some other broker right?

Hello @elishablacksmith07

No, there is no such requirement for any additional JSON to reverse orders specifically. You can use double quantity and it will block for single sell order only.

We tried replicating this and the orders went successfully without double quantity margin. Hence, asking you to kindly let us know in case you face this issue again, so that we can check the same realtime on server logs to identify exact issue.

sir stil facing trouble my head is burning out with this issue. Can you give a sample code for this?

I have modified my code with double qty order for reversing position. Let’s see if it works on next market hours i.e. on monday. my positions are in bank nifty futures and I try placing order for 2 lots. for reversing an open short position I need to buy 4 lots. I keep 2.1 lacs capital in my account for margin and this much is sufficient to short or long 2 lots. But when i fire order for 4 lots it asks for additional margins. On monday I shall re confirm. please help me in facilitating in this reverse position issue

@elishablacksmith07 at Dhan not support such features for Equity Derivatives even if you have 2x margin available. However, it’s working for Commodity.

Further, this issue cum features are requested by many users on platforms but Dhan representative have either or different reasons.

@elishablacksmith07 @Hardik this is very basic features.

@Hardik if this features doesn’t allow then you should stop allowing One click Reverse Position as well too. It’s doesn’t require 2x margin.

We need Reversal feature with just SL/SLM 2X qty Order.

@Dhan @Dhan_Help please please allow user to place Reversal 2X qty order with SL/SLM.

Hello @elishablacksmith07

Sure, do try this and if facing an error, help me with your Client ID on DM, will check this scenario. This should work fine.

@dvpandya90 The issue faced by you is separate. We are evaluating this and will implement if found viable, as it is not related to margin but the option to double quantity on Stop Loss orders itself.

Hello sir
I need JSON file for wabhook URL to reverse position

Hello @Mr_Billionaire_Ravi

Welcome to Dhan Community!

You can refer to below method to reverse position via Webhooks.