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

Fix realized pnl bug in Portfolio #2243

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion nautilus_core/portfolio/src/portfolio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ impl Portfolio {
continue; // Nothing to calculate
}

if position.side == PositionSide::Flat {
if position.realized_pnl.is_none() {
continue; // Nothing to calculate
}

Expand Down
2 changes: 1 addition & 1 deletion nautilus_trader/portfolio/portfolio.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,7 @@ cdef class Portfolio(PortfolioFacade):
if position.instrument_id != instrument_id:
continue # Nothing to calculate

if position.side == PositionSide.FLAT:
if position.realized_pnl is None:
continue # Nothing to calculate

pnl = position.realized_pnl.as_f64_c()
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/portfolio/test_portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1282,11 +1282,11 @@ def test_closing_position_updates_portfolio(self):
# Assert
assert self.portfolio.net_exposures(SIM) == {}
assert self.portfolio.unrealized_pnls(SIM) == {}
assert self.portfolio.realized_pnls(SIM) == {USD: Money(0, USD)}
assert self.portfolio.realized_pnls(SIM) == {USD: Money(6, USD)}
assert self.portfolio.margins_maint(SIM) == {}
assert self.portfolio.net_exposure(AUDUSD_SIM.id) == Money(0, USD)
assert self.portfolio.unrealized_pnl(AUDUSD_SIM.id) == Money(0, USD)
assert self.portfolio.realized_pnl(AUDUSD_SIM.id) == Money(0, USD)
assert self.portfolio.realized_pnl(AUDUSD_SIM.id) == Money(6, USD)
assert self.portfolio.net_position(AUDUSD_SIM.id) == Decimal(0)
assert not self.portfolio.is_net_long(AUDUSD_SIM.id)
assert not self.portfolio.is_net_short(AUDUSD_SIM.id)
Expand Down Expand Up @@ -1421,7 +1421,7 @@ def test_several_positions_with_different_instruments_updates_portfolio(self):

# Assert
assert self.portfolio.unrealized_pnls(SIM) == {USD: Money(-38998.00, USD)}
assert self.portfolio.realized_pnls(SIM) == {USD: Money(-4.00, USD)}
assert self.portfolio.realized_pnls(SIM) == {USD: Money(92.00, USD)}
assert self.portfolio.net_exposures(SIM) == {USD: Money(161002.00, USD)}
assert self.portfolio.net_exposure(AUDUSD_SIM.id) == Money(161002.00, USD)
assert self.portfolio.unrealized_pnl(AUDUSD_SIM.id) == Money(-38998.00, USD)
Expand Down
Loading