Can we have an exit positions API which works according to params, (i) all → exits all positions, (ii) orderId → exits the position based on the orderId, (iii) tags → exits the position based on a tag supplied by the user during order placement ? That would make the squaring off the orders easier !
@Abhishek_Tiwari1 Just a curious question, how do you square off positions now if the order was placed from an algo running on a remote machine?
Hey @Abhishek_Tiwari1 ,
That’s a great suggestion! We’ll share it with the team and definitely look into implementing it in future updates.
@Shrutika_Poojari Can you tell me presently how an algo exit position when the order punching was done through APIs?
Hey @nitishbangera ,
Orders placed through API can be closed by placing the exit order through order API or manually.
Algo will not hinder your open position.
@Shrutika_Poojari Can you tell me how can I exit the position using the Order Api? I do not want to do it manually as the order exit will run via code. I couldn’t find how to exit from the documentation Orders - DhanHQ Ver 2.0 / API Document.
Hey @nitishbangera ,
As the payload would be same , you will have to make the changes in the parameter such as transaction type as SELL and you may also modify the order type to LIMIT MARKET STOP_LOSS STOP_LOSS_MARKET and modify the price accordingly.
@Shrutika_Poojari Thank you for the clarification. With transactionType as SELL, how does it know which order to exit if I have more than 1 order which was posted previously? If the exchange segment is NSE_FNO and I execute an Iron Condor, IronFly, Credit Spread; will all my legs go in as seperate orders or 1 order? How do I exit these positions?
Hey @nitishbangera ,
As the security ID varies for each symbol, you can execute the specific order.
you can maintain an array of active positions, or active strategies, each position created for a securityId will be pushed accordingly in the active array. Later use the same array to place reverse orders (if order placed for sell, reverse is buy and vice versa) to square of your positions. This is just something from the surface, you have to deep dive for the same.
@Shrutika_Poojari Thank you for the clarification and just by going through the flow, it looks unnecessarily complicated. An api to exit position using the orderId is an absolute must and it can be a very simple “DELETE /orders/exit/{order-id}” because expecting the user to send all the data which was previously sent during punching the order is not clean. The initial request is the need for a clean api implementation as the data sent for exit shouldn’t be the same data length as earlier as it makes no sense when the server has it. This expectation mimics how the UI works, where a click wouldn’t send the entire dataset but the specific order id.
@Abhishek_Tiwari1 Thank you for the response. I have gone through the details and my view was that the order api is complicated. Hence I was asking for clarifications from the Dhan Personnel.
@Hardik @Dhan Can you please make a note of this and provide an api to exit the order seamlessly using the order id.
Hey @nitishbangera ,
Yes, We’ve noted it and will surely try to incorporate the improvement in future updates.
@Shrutika_Poojari @Hardik Can you please share an ETA for this change?
Hey @nitishbangera
I understand your point on simplicity, but a position is created from multiple orders (and not just a single order). It may contain additional quantities, reduction and everything else. While creating such an API endpoint is not a big task, it is counterintuitive to how trading systems work, wherein Order book and Position book are separate.
We are looking into possibility of building an endpoint which can simplify exiting positions, but it might not be based on Order ID itself.
@Hardik If a position is created using multiple orders, the user can call multiple order ids. I am not sure I understand your point because if I want to exit an order, I want to exit the positions within that order only which would be the basic use of the “orderid”.
I understand that the Dhan APIs take one position and generate an order ID for it. If the scenario that I have is to deploy a Short Iron Condor, I will have 4 orders in which I have 4 positions for the strategy. If I want to exit, I will exit all 4 orders. Now, if I want to adjust 1 side, I will exit 2 orders and put 2 more orders for the adjustment. If I want to increase the quantity, I raise one more order for the same position and get another order id which I will run the exit later as its part of my deployed strategy. In short, I would want to exit orders as 1 strategy = m orders and I would never want to overlap it with another strategy. This is a practical scenario that I am explaining.
I understand that the relationship between orders and positions is m:n but can you explain the scenario where in I want to exit positions of different orders to justify your point as per the below quote.
@Hardik Can you share examples for your previous context? It would be good for understanding. I also request you to go through the example that I have shared in my previous comment.
@Hardik Still waiting for examples to clearly understand your context.