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

feat: Add SignlePnL and OrderId support #131

Merged
merged 7 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 29 additions & 2 deletions src/deephaven_ib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ class IbSessionTws:
* **accounts_summary**: account summary. Automatically populated.
* **accounts_positions**: account positions. Automatically populated.
* **accounts_pnl**: account PNL. Automatically populated.

* **accounts_pnl_single**: single PNL. populated by calling request_single_pnl() on a specific contract.
####
Da5hes marked this conversation as resolved.
Show resolved Hide resolved
# News
####
Expand Down Expand Up @@ -608,6 +608,15 @@ def deephaven_ib_parse_note(note:str, key:str) -> Optional[str]:
.move_columns_up(["RequestId", "ReceiveTime", "Account", "ModelCode"]) \
.drop_columns("Note") \
.last_by("RequestId"),
"accounts_pnl_single": tables_raw["raw_accounts_pnl_single"] \
.natural_join(tables_raw["raw_requests"], on="RequestId", joins="Note") \
.update([
"Account=(String)deephaven_ib_parse_note(Note,`account`)",
"ModelCode=(String)deephaven_ib_parse_note(Note,`model_code`)",
"ConId=(String)deephaven_ib_parse_note(Note,`conid`)"]) \
.move_columns_up(["RequestId", "ReceiveTime", "Account", "ModelCode"]) \
Da5hes marked this conversation as resolved.
Show resolved Hide resolved
.drop_columns("Note") \
.last_by("RequestId"),
"contracts_matching": tables_raw["raw_contracts_matching"] \
.natural_join(tables_raw["raw_requests"], on="RequestId", joins="Pattern=Note") \
.move_columns_up(["RequestId", "ReceiveTime", "Pattern"]) \
Expand Down Expand Up @@ -776,6 +785,24 @@ def request_account_positions(self, account: str, model_code: str = "") -> Reque
req_id = self._client.request_account_positions(account, model_code)
return Request(request_id=req_id)

def request_single_pnl(self, account: str, model_code: str = "", conid: int = 0) -> Request:
"""Request PNL updates for a single position. Results are returned in the ``accounts_pnl_single`` table.

Args:
account (str): Account to request PNL for.
model_code (str): Model portfolio code to request PNL for.
conid (int): Contract ID to request PNL for.

Returns:
A Request.

Raises:
Exception: problem executing action.
"""
self._assert_connected()
req_id = self._client.request_single_pnl(account, model_code, conid)
return Request(request_id=req_id)
Da5hes marked this conversation as resolved.
Show resolved Hide resolved


####################################################################################################################
####################################################################################################################
Expand Down Expand Up @@ -1161,7 +1188,7 @@ def order_place(self, contract: RegisteredContract, order: Order) -> Request:
order.orderId = req_id
else:
req_id = order.orderId

cd = contract.contract_details[0]
self._client.log_request(req_id, "PlaceOrder", cd.contract, {"order": f"Order({order})"})
self._client.placeOrder(req_id, cd.contract, order)
Expand Down
4 changes: 1 addition & 3 deletions src/deephaven_ib/_tws/tws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,7 @@ def managedAccounts(self, accountsList: str):
self.request_account_pnl(account)
self.request_account_overview(account)
self.request_account_positions(account)

# TODO: Implement PnL requests for individual positions



####
# reqFamilyCodes
Expand Down
Loading