ranaroussi / ezibpy

ezIBpy, a Pythonic Client for Interactive Brokers API
Apache License 2.0
324 stars 116 forks source link

ValueError: Account not found in account list #30

Closed 3rock618 closed 5 years ago

3rock618 commented 5 years ago

1.12.67 was working fine, error with 1.12.68:

When I place an a basic market order I get the following error:

>>> tws.placeOrder(contract, tws.createOrder(1))
1550304349
ERROR:ibpy:Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/ib/opt/dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 332, in handleServerEvents
    self.handleOrders(msg)
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 762, in handleOrders
    positions = self.getPositions(order['account'])
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 616, in getPositions
    raise ValueError("Account %s not found in account list" % account)
ValueError: Account  not found in account list
ERROR:ibpy:Exception in message dispatch.  Handler 'handleServerEvents' for 'orderStatus'
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/ib/opt/dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 332, in handleServerEvents
    self.handleOrders(msg)
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 762, in handleOrders
    positions = self.getPositions(order['account'])
  File "/usr/local/lib/python3.7/site-packages/ezibpy/ezibpy.py", line 616, in getPositions
    raise ValueError("Account %s not found in account list" % account)
ValueError: Account  not found in account list

Using TWS I can see that the order completes successfully. Also when I type tws.getPositions() I get my positions back as normal.

3rock618 commented 5 years ago

I've found the problem. The order object in handleOrders comes back where order['account'] = ''. However tws.getPositions(None) is not the same as tws.getPositions(''), hence the error.

Since I'd rather not mess with the whole order object pipeline (it's a bit hairy), I can monkeypatch getPositions with if account == '': account = None


import ezibpy

# MONKEY PATCH
def getPositions(self, account=None):
    if len(self._positions) == 0: 
        return {}
    if account == '': 
        account = None
    account = self._get_default_account_if_none(account)
    if account is None:
        if len(self._positions) > 1:
            raise ValueError("Must specify account number as multiple accounts exists.")
        return self._positions[list(self._positions.keys())[0]]
    if account in self._positions:
        return self._positions[account]
    raise ValueError("Account %s not found in account list" % account)

ezibpy.ezIBpy.getPositions = getPositions
ranaroussi commented 5 years ago

Hi,

Thanks for pointing that out!

I've updated the library to use a more fool-proof_get_active_account() (foremarly _get_default_account_if_none()) to handle this. I've also made sure that handleOrders uses None as the default account value instead of "". This should do the trick :)

Please update to 1.12.69 to get the changes and let me know if the problem is solved.

Thanks, Ran

3rock618 commented 5 years ago

Problem still persists in .68, when I debug getPositions it tells me that account = ''

ranaroussi commented 5 years ago

Thanks!

When calling getPositions() - are you not getting the positions, or are you just not getting the account code?

Also - do you call getPositions() with or without specifying the account?

ranaroussi commented 5 years ago

?

3rock618 commented 5 years ago

Working from the github version, this is solved now. 👍🏼🙏🏼 Thanks Ran for all your great work!