joshuaulrich/IBrokers

reqAccountUpdates portfolioValue returning wrong results

Closed this issue · 6 comments

fmair commented

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

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]).

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")

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

@Camilo-Mora Take a look at the commit diff and message: 970fca2

fmair commented

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!

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