IBC 1.3.8 can report currency data from wrong info account

I have multiple accounts, and each account has balances in different currencies. The first account has a balance in CAD. The second does not have any CAD balance.

After changing the info account using SetInfoAccount() from the first account to the second, ibc continues to return the CAD value from the first account, even though there is no CAD balance in the second account.

A quick and dirty workaround is to ensure that you have at least one unit of every currency in every account.

Unfortunately this is due to the way how Interactive Brokers sends portfolio updates.
It sends updates item by item and only for items that exist.
They don't send update on something that you don't own. So you won't get "zero" for CAD balance. You will simply get NO update for CAD at all after changing account. So any information sent before stays.

Ok, thanks for letting me know. That makes it a bit more difficult to get the balances right.

I guess that I could wipe entire content of "Account" tab when you call SetInfoAccount to prevent old items from blurring the image, but that also means that content of Account tab would not be available for a few seconds after SetInfoAccount since it takes time for TWS to respond with account updates.

Thank you for thinking about it. I experimented with ClearLists(3) but it really did take too long to repopulate, so I wouldn't make that change.

I think there is a better way to get this data from TWS, because account values for all accounts and currencies are available. I'm not sure how ibc gets these values, but IB_insync can provide them, so the information must be available.

A related issue is the way that ibc reports FX. It only reports the cash balance and net liquidity, and does not report forex "positions" which are not the same as the forex "balance". I've been confused about the topic for a while, and today concluded that Forex positions should appear in ibc's Portfolio tab, because they are actual Positions, not Balances.

Here's the Python script to extract all the positions and cash balances by currency:

import pandas as pd
import ib_insync
from ib_insync import *

ib = IB()
ib.connect('127.0.0.1', 7496, clientId=10)

pd.set_option('display.max_rows', 100)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', 200)

##Data = ib_insync.__all__
##df = pd.DataFrame( Data )
##print( df )

print( 'Positions:' )
Data = ib.positions()
df = pd.DataFrame( Data )
print( df )

print()
print( 'Cash balances:' )
##Data = [v for v in ib.accountValues() if v.tag == 'NetLiquidationByCurrency' ] # and v.currency == 'BASE'
Data = [v for v in ib.accountValues() if v.tag == 'CashBalance' ]
df = pd.DataFrame( Data )
print( df )

The script produces this output, which shows both FX Positions and Balances for all accounts in the query:

Positions:
      account  \
0   DU1430479   
1   DU1430479   
2   DU1430479   
3   DU1430479   
4   DU1430478   
5   DU1430478   
6   DU1430478   
7   DU1430478   
8   DF1430477   
9   DF1430477   
10  DF1430477   
11  DF1430477   
12  DU1430480   
13  DU1430481   
14  DU1430482   

                                                                          contract  \
0   Forex('CHFUSD', conId=12087802, localSymbol='CHF.USD', tradingClass='CHF.USD')   
1   Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   
2   Forex('AUDUSD', conId=14433401, localSymbol='AUD.USD', tradingClass='AUD.USD')   
3   Forex('USDCHF', conId=12087820, localSymbol='USD.CHF', tradingClass='USD.CHF')   
4   Forex('CHFUSD', conId=12087802, localSymbol='CHF.USD', tradingClass='CHF.USD')   
5   Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   
6   Forex('AUDUSD', conId=14433401, localSymbol='AUD.USD', tradingClass='AUD.USD')   
7   Forex('USDCHF', conId=12087820, localSymbol='USD.CHF', tradingClass='USD.CHF')   
8   Forex('AUDUSD', conId=14433401, localSymbol='AUD.USD', tradingClass='AUD.USD')   
9   Forex('USDCHF', conId=12087820, localSymbol='USD.CHF', tradingClass='USD.CHF')   
10  Forex('CHFUSD', conId=12087802, localSymbol='CHF.USD', tradingClass='CHF.USD')   
11  Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   
12  Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   
13  Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   
14  Forex('EURUSD', conId=12087792, localSymbol='EUR.USD', tradingClass='EUR.USD')   

    position   avgCost  
0     441.00  1.138200  
1     205.51  1.084938  
2     -26.07  0.647500  
3     458.44  0.879146  
4     456.00  1.138200  
5     212.29  1.084938  
6     -26.93  0.647500  
7     473.56  0.879146  
8      53.00  0.647500  
9    -932.00  0.879146  
10   -897.00  1.138200  
11  -1232.00  1.084938  
12    253.40  1.088900  
13    274.69  1.088900  
14    286.11  1.088900  

Cash balances:
      account          tag         value currency modelCode
0   DF1430477  CashBalance          0.00      AUD          
1   DF1430477  CashBalance     262642.91     BASE          
2   DF1430477  CashBalance          0.00      CHF          
3   DF1430477  CashBalance          0.00      EUR          
4   DF1430477  CashBalance     262642.91      USD          
5   DU1430478  CashBalance          0.48      AUD          
6   DU1430478  CashBalance   773972.9264     BASE          
7   DU1430478  CashBalance        -26.90      CAD          
8   DU1430478  CashBalance        0.0417      CHF          
9   DU1430478  CashBalance          0.26      EUR          
10  DU1430478  CashBalance   773992.1345      USD          
11  DU1430479  CashBalance          0.34      AUD          
12  DU1430479  CashBalance   749268.8781     BASE          
13  DU1430479  CashBalance        -26.88      CAD          
14  DU1430479  CashBalance        0.3544      CHF          
15  DU1430479  CashBalance          0.47      EUR          
16  DU1430479  CashBalance   749287.5778      USD          
17  DU1430480  CashBalance          0.41      AUD          
18  DU1430480  CashBalance   925593.9974     BASE          
19  DU1430480  CashBalance          0.19      CHF          
20  DU1430480  CashBalance          0.27      EUR          
21  DU1430480  CashBalance   925593.2213      USD          
22  DU1430481  CashBalance          0.41      AUD          
23  DU1430481  CashBalance  1002100.8834     BASE          
24  DU1430481  CashBalance          0.19      CHF          
25  DU1430481  CashBalance         -0.08      EUR          
26  DU1430481  CashBalance  1002100.4883      USD          
27  DU1430482  CashBalance          0.41      AUD          
28  DU1430482  CashBalance   1043076.713     BASE          
29  DU1430482  CashBalance          0.19      CHF          
30  DU1430482  CashBalance          0.34      EUR          
31  DU1430482  CashBalance  1043075.8606      USD

Thank you for taking time to consider this issue, I know you're busy and I appreciate your attention on this one.

--Peter

Overnight, the FX Positions in my demo account vanished. Now I only have FX Balances. And when I place trades, TWS doesn't create new FX Positions. So I still have a lot to learn about how TWS reports FX.

This is how Forex works in Interactive Brokers. Check TWS itself. Currency positions are shown in the account Page as cash balances.