softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

reqPnL for individual positions #18

Closed manishranjan99 closed 6 years ago

manishranjan99 commented 6 years ago

Hi Abel,

Happy new year! I am just wondering if you can suggest ways to get realized/unrealized real-time PnL for each positions through IB tws? reqPositions seem to be working but it doesn't have all the details. Greatly appreciate any help on this. This is not an issue, just request.

Kind Regards, Manish

softwarespartan commented 6 years ago

I think these are just derived quantities using a quick historical data api call for 1 bar (open price for the day). Once you have the open bar you can calculate all unrealized profits for the day based on the positions api call.

On Jan 7, 2018, at 2:25 AM, manishranjan99 notifications@github.com wrote:

Hi Abel,

Happy new year! I am just wondering if you can suggest ways to get realized/unrealized real-time PnL for each positions through IB tws? reqPositions seem to be working but it doesn't have all the details. Greatly appreciate any help on this. This is not an issue, just request.

Kind Regards, Manish

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

manishranjan99 commented 6 years ago

Thanks for your response. I think what you suggest would work. It requires keeping track of fill prices and MKT data to calculate this. I was looking for some shortcut function to do this work :)

Manish

softwarespartan commented 6 years ago

Hi Manish

No problem.

Let me see if i have time this week, I can code up an example class that could do this. It would make a nice example for working with multiple API calls and events.

Happy New Year!

-abel

On Jan 8, 2018, at 1:11 PM, manishranjan99 notifications@github.com wrote:

Thanks for your response. I think what you suggest would work. It requires keeping track of fill prices and MKT data to calculate this. I was looking for some shortcut function to do this work :)

Manish

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/18#issuecomment-356047323, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1LTla3DMDpWpPbtvHxNFReoKrXQwks5tIlpmgaJpZM4RVkNX.

manishranjan99 commented 6 years ago

Hi Abel,

I believe IB TWS already does that on their end. I may just be a matter to sending the right account number and conIDs. It may require including the jar function that does that.

https://interactivebrokers.github.io/tws-api/pnl.html#gsc.tab=0 Example in IB TWS API - Subscribe using the IBApi::EClient::reqPnL function: client.reqPnL(17001, "DUD00029", "");

I hope it helps. Didn't want to take lot of your time.

Regards, Manish

softwarespartan commented 6 years ago

Hi Manish

You’re right.

However, this PnL call is only in new(ish) version of the interactive brokers API (referred to as +9.72) which (for various reasons) I haven’t pushed to the GitHub repository.

Can you try “reqAccountUpdates” instead

https://interactivebrokers.github.io/tws-api/account_updates.html#gsc.tab=0 <https://interactivebrokers.github.io/tws-api/account_updates.html#gsc.tab=0>

This should activate the “updatePortfolio” callback which actually does include realized and unrealized PnL for positions.

The associated event in IB4m is “TWS.Events.PORTFOLIOUPDATE”

There is a portfolioUpdate example included in the docs:

https://github.com/softwarespartan/IB4m/blob/master/docs/PortfolioUpdateExample.m <https://github.com/softwarespartan/IB4m/blob/master/docs/PortfolioUpdateExample.m>

Let me know if this works for you

Cheers -abel

On Jan 8, 2018, at 6:14 PM, manishranjan99 notifications@github.com wrote:

Hi Abel,

I believe IB TWS already does that on their end. I may just be a matter to sending the right account number and conIDs. It may require including the jar function that does that.

https://interactivebrokers.github.io/tws-api/pnl.html#gsc.tab=0 https://interactivebrokers.github.io/tws-api/pnl.html#gsc.tab=0 Example in IB TWS API - Subscribe using the IBApi::EClient::reqPnL function: client.reqPnL(17001, "DUD00029", "");

I hope it helps. Didn't want to take lot of your time.

Regards, Manish

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/18#issuecomment-356127040, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1Lz95kjPyGMIYmLJihnaZHHEbNeuks5tIqFkgaJpZM4RVkNX.

manishranjan99 commented 6 years ago

Hi Abel,

This looks great! It's working. Its showing the portfolio updates including the unrealized and realized values. Thanks so much for showing it to me.

Kind Regards, Manish