chrism2671/PyTrendFollow

Multi accounts dictionary IB API

jerreyz opened this issue · 5 comments

Hi there , Im having a bug when connecting to the IB api.

it's relating to the multi IB accounts
More specifically I get an error in the method _account_summary_handler of ibstate
acc = self.accounts[msg.account] KeyError: 'DU15195'

The problem is that I never see accounts initialised anywhere.
It is therefore trying to slice an empty dictionary.
Should the fix not be ?
acc=msg.account

Kind regards

I'm working on a fix now. I'm just a bit confused as to where the Acc objects are being initialised. It seems to me that this is happening in _managed_accounts_handler, however because I will only be using only one account ,can I just initialise in _account_summary_handler?

@jerreyz You're right, it's in _managed_accounts_handler and I don't think it depends on whether you're using one or multiple accounts. In the case of single account you should just have a single-key dictionary in self.accounts

@imaginary-unit thanks for your comment.
I still kept getting errors because _account_summary_handler is being registered first and then tries to slice on an empty dictionary. I simply create the dictionary in _account_summary_handler and then ReqPositions from there. It's a quick and dirty fix, would be cleaner to just remove the accounts object alltogether as you don't really use it in a single account system

@jerreyz Well it depends on whether the multi-account support is needed to be maintained in this project or not, which I'm not really aware of :) If yes, then it's probably better to check if msg.account is in self.accounts inside _account_summary_handler and if it's not, just add it (and do the same thing in _managed_accounts_handler to avoid duplicates). If multi-account support is not needed, then sure accounts dictionary should be removed.