After successfully making a trade how to close a position using dhahq python api?
Hello @trading.sahu059
To close a position via APIs or any platform, you just need to take a reverse trade i.e. if you have long position of x quantity, you need to place a sell order (via Order Placement API) for the same x quantity.
This will essentially close your long position. Hope this helps!
on any strike price?
If my buy order is for strike x how can close this position via API?
Hello @Channa_Mangalore
Welcome to Dhan Community!
You can do so by first fetching your positions and checking which instrument do you have a position in. Then you can execute sell order (in case you have long position) or vice versa in the same Security ID.
@Hardik whats wrong with the below json
Below buy order is succesfull
{“secret”:“xxxxx”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“B”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“NIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“CE”,“strike_price”:“22950”,“expiry_date”:“2024-05-30”}]}
But I am not able to trigger a sell order on the sell alert
Here is what I am generating for sell. Whats wrong I am doing?
{“secret”:“xxxxxxxx”,“alertType”:“multi_leg_order”,“order_legs”:[{“transactionType”:“S”,“orderType”:“MKT”,“quantity”:“1”,“exchange”:“NSE”,“symbol”:“NIFTY”,“instrument”:“OPT”,“productType”:“I”,“sort_order”:“1”,“price”:“0”,“option_type”:“None”,“strike_price”:“None”,“expiry_date”:“2024-05-30”}]}
Hello @Channa_Mangalore
Looks like you are not adding the strike price and the option type in case of sell order. Do check that and pass the same as the one in Buy JSON to close the position on sell alert.
@Hardik how do I get the strike price of open order? I read somewhere I need to send none in strike_price but it is not reaching dhan.
Hello @Channa_Mangalore
In case of webhooks, you will have to define the strike price that you are trying to place order. You can use the JSON generator from Dhan Webhook Order page to get the JSON and check how strike price is added in the JSON.