joshuaulrich / IBrokers

R API to Interactive Brokers Trader Workstation
65 stars 54 forks source link

reqAccountUpdates portfolioValue returning wrong results #30

Closed fmair closed 2 years ago

fmair commented 3 years ago

Description

I have a position of 10 SPY shares in my paper trading portfolio. They were bought at 411.55 and now have a market value of 4173.40 (roughly). When I run reqAccountUpdates and twsPortfolioValue the results do not match. It seems like all entries are shifted by one row. (see entries below "portfolioValue" in the list below). Also the output for twsPortfolioValue is not correct.

Edit: I just tested it and the functions show the expected behavior when using the current CRAN version IBrokers_0.9-12.

Expected behavior

reqAccountUpdates should return the current positions of the portfolio including position sizes.

Minimal, reproducible example

x <- reqAccountUpdates(conn, subscribe = T, acctCode = "1")
> twsPortfolioValue(x, zero.pos=T)[2, ]
  local sectype marketValue averageCost return position realizedPNL unrealizedPNL
2   SPY     STK     417.344          NA     NA       NA       57.91      411.5534
> x[[2]][[2]]
List of 2
 $ contract      :List of 17
  ..$ conId          : chr "756733"
  ..$ symbol         : chr "SPY"
  ..$ sectype        : chr "STK"
  ..$ exch           : chr "ARCA"
  ..$ primary        : chr "ARCA"
  ..$ expiry         : chr ""
  ..$ strike         : chr "0"
  ..$ currency       : chr "USD"
  ..$ right          : chr "0"
  ..$ local          : chr "SPY"
  ..$ multiplier     : chr ""
  ..$ combo_legs_desc: chr ""
  ..$ comboleg       : chr ""
  ..$ include_expired: chr ""
  ..$ secIdType      : chr ""
  ..$ secId          : chr ""
  ..$ tradingClass   : chr ""
  ..- attr(*, "class")= chr "twsContract"
 $ portfolioValue:List of 7
  ..$ position     : num NA
  ..$ marketPrice  : num 10
  ..$ marketValue  : num 417
  ..$ averageCost  : num 4173
  ..$ unrealizedPNL: num 412
  ..$ realizedPNL  : num 57.9
  ..$ accountName  : chr "0.0"

Session Info

I've loaded some private packages, hence I'd prefer not to share my session info. However, these don't interfere with IBrokers. I'm using Windows 10 x64 and running IB TWS Build 981.2z, Apr 13 2021.

IBrokers_0.10-1
fmair commented 2 years ago

The problem persists with the current CRAN-version 0.10-1 and TWS Build 10.15.1d, Apr 5, 2022 and comes from the way reqAccountUpdates handles the messages (msg[12]).

joshuaulrich commented 2 years ago

Just pushed a commit that should fix the issue. Please test and let me know! You can install the version on this branch by using:

remotes::install_github("joshuaulrich/IBrokers@30-reqAccountUpdates-portfolioValue")
Camilo-Mora commented 2 years ago

Perfect. I just verified and the problem is fixed. Where was the bug?. Thank you Josh.

joshuaulrich commented 2 years ago

@Camilo-Mora Take a look at the commit diff and message: https://github.com/joshuaulrich/IBrokers/commit/970fca2299be92ae88ab1ac14fbd7c60fd26f6ae

fmair commented 2 years ago

Just pushed a commit that should fix the issue. Please test and let me know! You can install the version on this branch by using:

remotes::install_github("joshuaulrich/IBrokers@30-reqAccountUpdates-portfolioValue")

I can confirm that it is working again. Thanks for the fix!

joshuaulrich commented 2 years ago

Thanks for testing, @Camilo-Mora and @fmair!