Api bug unrealizedProfit is always zero

https://api.dhan.co/positions
response
{“dhanClientId”:"**********",“tradingSymbol”:“RELIANCE”,“securityId”:“2885”,“positionType”:“LONG”,“exchangeSegment”:“NSE_EQ”,“productType”:“INTRADAY”,“buyAvg”:2512.4,“buyQty”:1,“sellAvg”:0.0,“sellQty”:0,“netQty”:1,“realizedProfit”:0.0,“unrealizedProfit”:0.0,“rbiReferenceRate”:1.0,“multiplier”:1,“carryForwardBuyQty”:0,“carryForwardSellQty”:0,“carryForwardBuyValue”:0.0,“carryForwardSellValue”:0.0,“dayBuyQty”:1,“daySellQty”:0,“dayBuyValue”:2512.4,“daySellValue”:0.0,“drvExpiryDate”:“0001-01-01”,“drvOptionType”:“NA”,“drvStrikePrice”:0.0,“crossCurrency”:false}

1 Like

Hi @vive1101011

hey @vive1101011, this is not a bug. We don’t provide this data on APIs, most users who consume APIs can simply add P&L on all individual positions and get this with a simple query. We do the same and render combined P&L on the fly on Dhan platforms - you will see this is much much faster than all other platforms out there.

Why we don’t provide this - to get collective P&L from all positions on LTP that is changing dynamically and then streaming it - will result in lag & performance issues if we start streaming this.

Sir,
Actually the problem is if quote api(getting LTP) not available to the individual investors then the only option left to calculate p&l is using /charts/intraday 1min candle close, which is delayed.

Hey @PravinJ,

My two cents:

  1. Having an unrealised P&L (or even realised P&L) seem like a first class entity for any Position, and hence even at cost of some performance, the preference would be to have it coming from source rather than the clients getting LTP separately for each stock having a position and computing it. Majority of clients can bear a lag of ~1 second as they won’t be having an HFT use-case. Those having one would anyways fetch LTP themselves.
  2. I would feel the central source would be more optimised in terms of lag because of multiple requests coming in which can be pooled. It might be suboptimal if each of us client does it individually.

(Btw, if the proposal is to not provide unrealised or realised profits in the Positions API, why even keep those empty fields there, and leave room for confusion?)

Hoping the team would accommodate the above request :slight_smile:

2 Likes

Hey @ms25

Fair point you have on this… and we did speak with many users while building APIs. Most, and when I say most its very high calculate this by themselves.

If you will see our implementation of P&L in any of Dhan’s platforms - App or Web, we render P&L much much faster than anyone else would, and we do it on the fly there itself. When we give P&L to users via APIs, it comes live from our trading systems and that basically will take it down (by load and hamper performance) as we will have to calculate this and transmit.

Additionally, many API users we have interacted with have their own source of LTP / Live Prices. So their use-case of trading is just resolved with trading APIs. Also our infrastructure cost of trading APIs is significantly lower than data broadcast APIs (live P&L). That helps us give Trading APIs for free.

We recently made Trading APIs of Dhan free: DhanHQ Superfast Trading APIs Update: Trading APIs are now FREE for all

Thank you @PravinJ for the prompt response :slight_smile:
Sure, I can understand. Anyways, its not a burning problem for me, and can live without it.

I do feel that if any fields are not being populated, why to pass them unnecessary :slight_smile:
Increases the payload and processing on client end.
Maybe the fields not being populated/used can be removed, also to remove any confusion and expectations :slight_smile:

Hi @vive1101011 , were u able to calculate live pnl using positions and candles data?

Hi stanly_thomas,
I was not able to implement instant pnl as tick data is still not available on Dhan.

1 Like

Even i am not getting the live PnL when my positions are open.I get the PnL when a position is closed.

unrealizedProfit and realizedProfit is always zero for open positions . i.e

[{‘dhanClientId’: ‘100056XXXX’, ‘tradingSymbol’: ‘BANKNIFTY-Apr2023-41500-PE’, ‘securityId’: ‘48811’, ‘positionType’: ‘LONG’, ‘exchangeSegment’: ‘NSE_FNO’, ‘productTy
pe’: ‘INTRADAY’, ‘buyAvg’: 186.65, ‘costPrice’: 186.65, ‘buyQty’: 25, ‘sellAvg’: 0.0, ‘sellQty’: 0, ‘netQty’: 25, ‘realizedProfit’: 0.0, ‘unrealizedProfit’: 0.0, ‘rbi
ReferenceRate’: 1.0, ‘multiplier’: 1000, ‘carryForwardBuyQty’: 0, ‘carryForwardSellQty’: 0, ‘carryForwardBuyValue’: 0.0, ‘carryForwardSellValue’: 0.0, ‘dayBuyQty’: 25
, ‘daySellQty’: 0, ‘dayBuyValue’: 4666.25, ‘daySellValue’: 0.0, ‘drvExpiryDate’: ‘2023-04-13’, ‘drvOptionType’: ‘PUT’, ‘drvStrikePrice’: 41500.0, ‘crossCurrency’: Fal
se}]

Any idea how to calculate live pnl ?

@PravinJ This is really not helping. Do we have websocket available? Then your excuse for not providing unrealized Profit is totally baseless. Every there is no way to get a streaming LTP. If the value is zero then you should not send it at all and remove it from json. It creates a lot of confusion. Your document also given info about it.

unrealizedProfit
number

Profit or loss standing for open position

Please provide a solution on how we can do it in python code? I want to exit all positions once my MTM is greater than say 1.5 percent of the total margin utilized for the intraday trades.
Anybody else, having solution please let me know. All other brokers provide this information in positions.

1 Like

Hello @rahulmr

Good to see you around after a long time.

Websocket/Market Feed is still in development. There has been delay in releasing the same, but rest assured, we will deliver it soon.

On P&L in positions, you will have to calculate the same at your end. This will be simpler when we have websocket available on Dhan. Meanwhile, a lot of our users do it on other data subscription. We will consider removing this field, but again, we will have to do this when we start versioning APIs, as removing/editing any parameter directly might impact other users who have integrated this API.

Hi @Hardik , thanks for reply , could you please provide any links where this feature is implemented in python? I will check out other options for getting streaming LTP. The P&L calculation also depends on the leverage provided in intraday trades and it can vary depending on the stock. That is the reason, getting unrealized profit is important. Please do consider or give a second thought on implementing the same. I would to have the unrealized profit details from the JSON. It can be very helpful. I agree removing or editing API is difficult but I think providing the details might be easy.

Hello @rahulmr

Currently, I don’t have access to someone’s code repo or a reference point for you here. But maybe someone from the community can help you here.

We will definitely evaluate adding this from our side as well. As already discussed, we do not store any of this values on our servers, otherwise would have been easier for us to give this data as part of API response in the first place, but will definitely look into adding value in this parameter.

1 Like

Thanks man! Hope someone from community replies. Meanwhile let me try other options. Please bring in websocket ASAP

Hi Rahul, can you please share how have implemented exiting all positions once your required MTM is achieved wrt to total margin utilised please? are/ were you using the unrealized margin part of the apis response n all or data apis? is it accurate and no lag ? please share