zerodha / pykiteconnect

The official Python client library for the Kite Connect trading APIs
MIT License
978 stars 467 forks source link

connecting position with order_id/tag #138

Closed akshaythakker closed 2 years ago

akshaythakker commented 2 years ago

Is there anyway for me to link order_id/ or tag passed during placing the order with the position. So lets say I place a sell order for Nifty 17000 PE from one strategy and then another order for the same security through another strategy, I would like to track the two positions independently and right now the only way to do so is through maintaining an extensive log of the traded price, current price, etc for each of the orders in each of the strategy. The problem with the logging system is that it doesn't allow a manual intervention or modification of some position (from outside the algo) which then throws the entire tracking & logging at the algo level into a tizzy.

vijaykz commented 2 years ago

Visit https://zerodha.com/z-connect/console/tagging-on-console-trading-journal-tracking-goals

Note that I have not personally tried this feature. And, I also don't find an option to tag existing open positions; seems it's only for holdings.

Let us know if this helps.

akshaythakker commented 2 years ago

Hi Vijaykz... thank you so much for writing back. Tags are a great, and when I am trading using the api - I CAN tag each order placed through the api. However, when i retrieve the current day position using kite.position.... the positions do not segregate as per the tags that created them. This defeats the purpose as I would like to track, modify, and exit positions in the same security placed as part of different strategies. The position command only sends a consolidated list of all positions for the security irrespective of the tag they carry. This is something that zerodha should work on if possible.

To elaborate lets say i shorted a call option for nifty 02 December 17100 CE as part of strategy1 and tagged it strategy1 and then again shorted 2 nifty 02 December 17100 CE as part of strateg2 and tag it as strategy2... i CANNOT retrieve current positions based on their tags, i only get a consolidated number of 3 nifty 02 December 17100 short!

vijaykz commented 2 years ago

In my view, Kite cannot (and should not) provide such features, in order to remain simple and cater to larger demography.

You can maintain a database of sorts and also create a custom (ex: HTML-based) UI and group your positions. And issue exit orders from within your app. You can tag and track orders effectively this way.

akshaythakker commented 2 years ago

hi vijay, are you part of kite development team?

vijaykz commented 2 years ago

Nope, I am not. I'm an independent developer.

rhnvrm commented 2 years ago

Hey @akshaythakker

We currently provide aggregate positions, so it is not possible to show a breakdown of the positions by tags at the moment. Splitting positions is tricky, as you said as well, there are multiple variables like qty, price etc to track.

For now, you will have to resort to maintaining the positions and splitting them according to your logic. For interactions outside your program, such as those made manually on Kite; you could maybe listen to on_order_update using the ticker connection and handle them appropriately.

akshaythakker commented 2 years ago

Hi Rohan, this on_order_update suggestion looks promising... will the order_update include the tag of the order that got updated? even that can solve my problem. Also where can i learn more about that feature?

Thanks!

rhnvrm commented 2 years ago

You can refer to the example here: https://github.com/zerodha/pykiteconnect/blob/master/examples/ticker.py#L31 and read more about it on the docs.

akshaythakker commented 2 years ago

The order update will post the tag for the new order from outside the algo...so that wont be useful for what I need to achieve. Looks like, there is nothing much that can be done for now!