Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating ib.reqPnL method to avoid AssertionError in case of repetitive calls #56

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

praditik
Copy link

@praditik praditik commented Aug 1, 2024

Earlier, an assert statement assert key not in self.wrapper.pnlKey2ReqId was causing the AssertionError when reqPnL is called second time for same set of inputs. This update avoids the error and returns existing PnL class object if already subscribed.

Minimum reproducible example

from ib_async import IB
import time
ib = IB()
account = "DUXXXXXXX"
ib.connect(clientId=5)
ib.sleep()
data=ib.reqPnL(account)
ib.sleep()
#Do something with data and for whatever reason, call ib.reqPnL again
data = ib.reqPnL(account) #This line causes the error
ib.sleep()

This creates error as follows

Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\algoBot\pnlTrial.py", line 11, in <module>
    data = ib.reqPnL(account)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\ib_async\ib.py", line 951, in reqPnL
    assert key not in self.wrapper.pnlKey2ReqId
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Even though calling ib.reqPnL() multiple times is not a sensible choice, this can be done accidentally by someone assuming ib.reqPnL to have same behavior as reqMktData or other requests. These other request methods generate no error and hence the reqPnL should also be consistent with other methods.

Hence, proposing the change.

…ve calls

Earlier, an assert statement "assert key not in self.wrapper.pnlKey2ReqId" was causing the `AssertionError` when `reqPnL` is called second time for same set of inputs. This update avoids the error and returns existing `PnL` class object if already subscribed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant