Skip to content

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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions ib_async/ib.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,8 @@ def reqPositions(self) -> List[Position]:

def reqPnL(self, account: str, modelCode: str = "") -> PnL:
"""
Start a subscription for profit and loss events.
Start a subscription for profit and loss events if
not subscribed already.

Returns a :class:`.PnL` object that is kept live updated.
The result can also be queried from :meth:`.pnl`.
Expand All @@ -985,15 +986,17 @@ def reqPnL(self, account: str, modelCode: str = "") -> PnL:
modelCode: If specified, filter for this account model.
"""
key = (account, modelCode)
assert key not in self.wrapper.pnlKey2ReqId

reqId = self.client.getReqId()
self.wrapper.pnlKey2ReqId[key] = reqId
pnl = PnL(account, modelCode)
self.wrapper.reqId2PnL[reqId] = pnl
self.client.reqPnL(reqId, account, modelCode)

return pnl
if key not in self.wrapper.pnlKey2ReqId:
reqId = self.client.getReqId()
self.wrapper.pnlKey2ReqId[key] = reqId
pnl = PnL(account, modelCode)
self.wrapper.reqId2PnL[reqId] = pnl
self.client.reqPnL(reqId, account, modelCode)
return pnl
else:
reqId = self.wrapper.pnlKey2ReqId[key]
pnl = self.wrapper.reqId2PnL[reqId]
return pnl

def cancelPnL(self, account, modelCode: str = ""):
"""
Expand Down