-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add avg_px_open field to PositionStatusReport for IB adapter #2925
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
Add avg_px_open field to PositionStatusReport for IB adapter #2925
Conversation
- Add avg_px_open field to PositionStatusReport class to track average open price - Extract and convert avg_cost from IB positions to avg_px_open Price object - Use avg_px_open in position reconciliation for accurate P&L calculations - Support position status reports with average open price from IB adapter This enhancement allows the IB adapter to properly report the average cost basis of positions, enabling accurate P&L calculations and position reconciliation. Previously, the target_position_avg_px was always None, preventing accurate reconciliation when positions changed externally (e.g., option exercises).
Looks good |
Hi @dinana Many thanks for the contribution, I was meaning to add this 👌 |
Looks like the pre-commit failed, but this is easy to fix. Just run the following and push any changes to the same PR:
or (if you're on Windows):
Also consider running the following to install the git hook which will run the pre-commit automatically on commit:
|
Pre-commit fixes applied:
All pre-commit checks now pass successfully. |
Hi @dinana Thanks for the update. Just a couple of tests failing in CI, here's some output if it helps:
|
- Remove trailing whitespace from execution.py files - Apply black formatting to reports.py
Makes avg_px_open field conditional in __repr__ method - only includes it when not None to maintain backward compatibility with existing tests.
85016f6
to
cb55ade
Compare
Summary
This PR adds support for the
avg_px_open
field inPositionStatusReport
to properly track the average open price (cost basis) of positions when using the Interactive Brokers adapter.Motivation
Previously, when the IB adapter generated position status reports, it did not include the average open price information even though this data is available from IB's API (
avg_cost
field). This caused issues with:The
target_position_avg_px
parameter in position reconciliation was alwaysNone
, preventing accurate reconciliation when positions changed externally (e.g., through option exercises or assignments).Changes
1. Extended PositionStatusReport class (
nautilus_trader/execution/reports.py
)avg_px_open: Price | None
parameter to constructor__repr__
method to include avg_px_open in string representationto_dict()
method to serialize avg_px_openfrom_dict()
classmethod to deserialize avg_px_open2. Enhanced IB adapter position reporting (
nautilus_trader/adapters/interactive_brokers/execution.py
)avg_cost
from IB position data ingenerate_position_status_reports()
3. Improved position reconciliation (
nautilus_trader/live/execution_engine.py
)avg_px_open
from PositionStatusReport if availablecalculate_reconciliation_price()
for accurate position reconciliationTesting
The changes have been tested with:
Breaking Changes
None. The
avg_px_open
field is optional and defaults toNone
, maintaining backward compatibility.Checklist