Urgent: Positions Miscalculated dhan.getpositions()

Hi @Hardik @Dhan_Cares @Dhan @PravinJ

Good Morning!

i went through the documentation in detail at
Portfolio - DhanHQ Ver 1.0 / API Document

On fetching position from dhan using dhan.getpositions()

When you open and close a position for a particular security Id, and then again open a new position for a that security id, The data received for positions is considering the closed positions too and as a result ‘buyAvg’: , ‘costPrice’: , ‘buyQty’: , ‘sellAvg’: , ‘sellQty’: for current open position are miscalculated.

Whereas, The closed positions should only be considered for calculating ‘dayBuyQty’: , ‘daySellQty’: , ‘dayBuyValue’: , ‘daySellValue’:

The ‘net qty’ and ‘positionType’ are working fine and displayed correctly.

Request to deploy a fix on priority.

Quick fix can be deployed at your end using ‘net qty’ , but i think the more accurate approach would be to use tradebook.

If you any need any further info feel free to contact.

Awaiting quick resolution.

@Hardik @Dhan_Cares @Dhan @PravinJ

This is a very serious issue, and it has been more than 2 days, i am still awaiting an update.

Hello @Sarita77

This is the intended way of positions API, if my understanding of your highlighted scenario is taken. Can you confirm if this is an issue that you faced recently or earlier this behaviour didn’t exist.

Hi @Hardik

I faced this issue on 28/08/2024

I would politely differ with you here, this should not be intended way of positions API as per documentation and nomenclature.

Once the position has been squared off/closed 'buyAvg’: , ‘costPrice’: , ‘buyQty’: , ‘sellAvg’: , ‘sellQty’, ‘net qty’ should be reset to zero and ‘positionType’ to CLOSED.

And the ‘dayBuyQty’: , ‘daySellQty’: , ‘dayBuyValue’: , ‘daySellValue’: should be updated as per CLOSED position.

This can be comprehended from the nomenclature as well as the documentation

Now, the net qty is correctly reset to 0 once the position is closed and the positionType is also correctly set to CLOSED .

If it would have not been the case that why would we need ‘dayBuyQty’: , ‘daySellQty’: , ‘dayBuyValue’: , ‘daySellValue’:

And as per documentation too is clearly stated that

‘dayBuyQty’: Quantities bought today

‘daySellQty’: Quantities sold today

‘dayBuyValue’: Value of quantities bought today

‘daySellValue’: Value of quantities sold today

Now, going as per your reply the values/calculations both ‘buyQty‘ and ‘dayBuyQty’ would be same.

and also the values/calculations for ‘sellQty’ and ‘daySellQty’ would be same.

Which should not be as per documentation snippet attached above and as per nomenclature.

Let me give you an example from a trading perspective:

Suppose you buy the 51000 CE option at 10:30 AM for Rs. 100 with a quantity of 50, and then again at 10:31 AM for Rs. 120 with a quantity of 50. The ‘buyAvg’ should be 110, and the ‘net qty’ should be 100, which is correct.

If you close the entire position at 10:35 AM by selling all 100 quantities at Rs. 130, the ‘net qty’ becomes 0, and the ‘positionType’ changes to CLOSED.

Now, if you buy the same 51000 CE option again at 12:10 PM for Rs. 200 with a quantity of 100, the correct ‘buyAvg’ should be 200, and the ‘net qty’ should be 100. for this current open position

But instead, the ‘buyAvg’ when fetched by using dhan.getpositions shows as 155, which is wrong (as it is including the previous closed position), the correct and true avgprice for current open position is 200 .

This is why we have separate fields like ‘dayBuyQty,’ ‘daySellQty,’ ‘dayBuyValue,’ and ‘daySellValue.’

Awaiting your response

Adding to this by Edit:

i went through other brokers api too specifically zerodh

there too they have similar nomenclature as yours and have very clearly stated that

average_price float64 -----> Average price at which the net position quantity was acquired

Important to note here the word NET POSITION

and also they have similar nomenclature for the day

day_buy_quantity": , “day_buy_price”: ,“day_buy_value”: , “day_sell_quantity”:, “day_sell_price”: ,“day_sell_value”:

There is a reason to have a separate field for Day

Looking forward to your response.