chrism2671 / PyTrendFollow

PyTrendFollow - systematic futures trading using trend following
MIT License
362 stars 70 forks source link

Multi accounts dictionary IB API #30

Open jerreyz opened 6 years ago

jerreyz commented 6 years ago

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

chrism2671 commented 6 years ago

Hi Jeroen

The reason it’s happening is because it’s setup for an advisor account (see IB friends and family https://www.interactivebrokers.com/ff/en/main.php). It should only be a few lines to change it to normal accounts, and I would accept that as a pull request too if you wanted to contribute it.

On 12 May 2018, at 16:48, Jeroen De Ryck notifications@github.com wrote:

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

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

jerreyz commented 6 years ago

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?

ka-petrov commented 6 years ago

@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

jerreyz commented 6 years ago

@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

ka-petrov commented 6 years ago

@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.